crm.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //! Tiny CRM: A port of the Yew CRM example to Dioxus.
  2. use dioxus::prelude::*;
  3. use dioxus::router::prelude::*;
  4. fn main() {
  5. LaunchBuilder::new()
  6. .with_cfg(desktop!({
  7. use dioxus::desktop::{LogicalSize, WindowBuilder};
  8. dioxus::desktop::Config::default()
  9. .with_window(WindowBuilder::new().with_inner_size(LogicalSize::new(800, 600)))
  10. }))
  11. .launch(|| {
  12. rsx! {
  13. link {
  14. rel: "stylesheet",
  15. href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css",
  16. integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5",
  17. crossorigin: "anonymous"
  18. }
  19. style { {r#" .red { background-color: rgb(202, 60, 60) !important; } "#} }
  20. h1 { "Dioxus CRM Example" }
  21. Router::<Route> {}
  22. }
  23. });
  24. }
  25. /// We only have one list of clients for the whole app, so we can use a global signal.
  26. static CLIENTS: GlobalSignal<Vec<Client>> = Signal::global(Vec::new);
  27. struct Client {
  28. first_name: String,
  29. last_name: String,
  30. description: String,
  31. }
  32. #[derive(Routable, Clone)]
  33. enum Route {
  34. #[route("/")]
  35. ClientList,
  36. #[route("/new")]
  37. ClientAdd,
  38. #[route("/settings")]
  39. Settings,
  40. }
  41. #[component]
  42. fn ClientList() -> Element {
  43. rsx! {
  44. h2 { "List of Clients" }
  45. Link { to: Route::ClientAdd, class: "pure-button pure-button-primary", "Add Client" }
  46. Link { to: Route::Settings, class: "pure-button", "Settings" }
  47. for client in CLIENTS.read().iter() {
  48. div { class: "client", style: "margin-bottom: 50px",
  49. p { "Name: {client.first_name} {client.last_name}" }
  50. p { "Description: {client.description}" }
  51. }
  52. }
  53. }
  54. }
  55. #[component]
  56. fn ClientAdd() -> Element {
  57. let mut first_name = use_signal(String::new);
  58. let mut last_name = use_signal(String::new);
  59. let mut description = use_signal(String::new);
  60. let submit_client = move |_: FormEvent| {
  61. // Write the client
  62. CLIENTS.write().push(Client {
  63. first_name: first_name(),
  64. last_name: last_name(),
  65. description: description(),
  66. });
  67. // And then navigate back to the client list
  68. dioxus::router::router().push(Route::ClientList);
  69. };
  70. rsx! {
  71. h2 { "Add new Client" }
  72. form { class: "pure-form pure-form-aligned", onsubmit: submit_client,
  73. fieldset {
  74. div { class: "pure-control-group",
  75. label { r#for: "first_name", "First Name" }
  76. input {
  77. id: "first_name",
  78. r#type: "text",
  79. placeholder: "First Name…",
  80. required: "",
  81. value: "{first_name}",
  82. oninput: move |e| first_name.set(e.value()),
  83. // when the form mounts, focus the first name input
  84. onmounted: move |e| async move {
  85. _ = e.inner().set_focus(true).await;
  86. },
  87. }
  88. }
  89. div { class: "pure-control-group",
  90. label { "for": "last_name", "Last Name" }
  91. input {
  92. id: "last_name",
  93. r#type: "text",
  94. placeholder: "Last Name…",
  95. required: "",
  96. value: "{last_name}",
  97. oninput: move |e| last_name.set(e.value())
  98. }
  99. }
  100. div { class: "pure-control-group",
  101. label { "for": "description", "Description" }
  102. textarea {
  103. id: "description",
  104. placeholder: "Description…",
  105. value: "{description}",
  106. oninput: move |e| description.set(e.value())
  107. }
  108. }
  109. div { class: "pure-controls",
  110. button { r#type: "submit", class: "pure-button pure-button-primary", "Save" }
  111. Link { to: Route::ClientList, class: "pure-button pure-button-primary red", "Cancel" }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. #[component]
  118. fn Settings() -> Element {
  119. rsx! {
  120. h2 { "Settings" }
  121. button {
  122. class: "pure-button pure-button-primary red",
  123. onclick: move |_| {
  124. CLIENTS.write().clear();
  125. dioxus::router::router().push(Route::ClientList);
  126. },
  127. "Remove all Clients"
  128. }
  129. Link { to: Route::ClientList, class: "pure-button", "Go back" }
  130. }
  131. }