crm.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //! Tiny CRM - A simple CRM app using the Router component and global signals
  2. //!
  3. //! This shows how to use the `Router` component to manage different views in your app. It also shows how to use global
  4. //! signals to manage state across the entire app.
  5. //!
  6. //! We could simply pass the state as a prop to each component, but this is a good example of how to use global state
  7. //! in a way that works across pages.
  8. //!
  9. //! We implement a number of important details here too, like focusing inputs, handling form submits, navigating the router,
  10. //! platform-specific configuration, and importing 3rd party CSS libraries.
  11. use dioxus::prelude::*;
  12. fn main() {
  13. dioxus::LaunchBuilder::new()
  14. .with_cfg(desktop!({
  15. use dioxus::desktop::{LogicalSize, WindowBuilder};
  16. dioxus::desktop::Config::default()
  17. .with_window(WindowBuilder::new().with_inner_size(LogicalSize::new(800, 600)))
  18. }))
  19. .launch(|| {
  20. rsx! {
  21. document::Link {
  22. rel: "stylesheet",
  23. href: "https://unpkg.com/purecss@2.0.6/build/pure-min.css",
  24. integrity: "sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5",
  25. crossorigin: "anonymous",
  26. }
  27. document::Link {
  28. rel: "stylesheet",
  29. href: asset!("/examples/assets/crm.css"),
  30. }
  31. document::Link {
  32. rel: "stylesheet",
  33. href: asset!("/examples/assets/crm.css"),
  34. }
  35. h1 { "Dioxus CRM Example" }
  36. Router::<Route> {}
  37. }
  38. });
  39. }
  40. /// We only have one list of clients for the whole app, so we can use a global signal.
  41. static CLIENTS: GlobalSignal<Vec<Client>> = Signal::global(Vec::new);
  42. struct Client {
  43. first_name: String,
  44. last_name: String,
  45. description: String,
  46. }
  47. /// The pages of the app, each with a route
  48. #[derive(Routable, Clone)]
  49. enum Route {
  50. #[route("/")]
  51. List,
  52. #[route("/new")]
  53. New,
  54. #[route("/settings")]
  55. Settings,
  56. }
  57. #[component]
  58. fn List() -> Element {
  59. rsx! {
  60. h2 { "List of Clients" }
  61. Link { to: Route::New, class: "pure-button pure-button-primary", "Add Client" }
  62. Link { to: Route::Settings, class: "pure-button", "Settings" }
  63. for client in CLIENTS.read().iter() {
  64. div { class: "client", style: "margin-bottom: 50px",
  65. p { "Name: {client.first_name} {client.last_name}" }
  66. p { "Description: {client.description}" }
  67. }
  68. }
  69. }
  70. }
  71. #[component]
  72. fn New() -> Element {
  73. let mut first_name = use_signal(String::new);
  74. let mut last_name = use_signal(String::new);
  75. let mut description = use_signal(String::new);
  76. let submit_client = move |_| {
  77. // Write the client
  78. CLIENTS.write().push(Client {
  79. first_name: first_name(),
  80. last_name: last_name(),
  81. description: description(),
  82. });
  83. // And then navigate back to the client list
  84. router().push(Route::List);
  85. };
  86. rsx! {
  87. h2 { "Add new Client" }
  88. form { class: "pure-form pure-form-aligned", onsubmit: submit_client,
  89. fieldset {
  90. div { class: "pure-control-group",
  91. label { r#for: "first_name", "First Name" }
  92. input {
  93. id: "first_name",
  94. r#type: "text",
  95. placeholder: "First Name…",
  96. required: true,
  97. value: "{first_name}",
  98. oninput: move |e| first_name.set(e.value()),
  99. // when the form mounts, focus the first name input
  100. onmounted: move |e| async move {
  101. _ = e.set_focus(true).await;
  102. },
  103. }
  104. }
  105. div { class: "pure-control-group",
  106. label { r#for: "last_name", "Last Name" }
  107. input {
  108. id: "last_name",
  109. r#type: "text",
  110. placeholder: "Last Name…",
  111. required: true,
  112. value: "{last_name}",
  113. oninput: move |e| last_name.set(e.value()),
  114. }
  115. }
  116. div { class: "pure-control-group",
  117. label { r#for: "description", "Description" }
  118. textarea {
  119. id: "description",
  120. placeholder: "Description…",
  121. value: "{description}",
  122. oninput: move |e| description.set(e.value()),
  123. }
  124. }
  125. div { class: "pure-controls",
  126. button {
  127. r#type: "submit",
  128. class: "pure-button pure-button-primary",
  129. "Save"
  130. }
  131. Link {
  132. to: Route::List,
  133. class: "pure-button pure-button-primary red",
  134. "Cancel"
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. #[component]
  142. fn Settings() -> Element {
  143. rsx! {
  144. h2 { "Settings" }
  145. button {
  146. class: "pure-button pure-button-primary red",
  147. onclick: move |_| {
  148. CLIENTS.write().clear();
  149. dioxus::router::router().push(Route::List);
  150. },
  151. "Remove all Clients"
  152. }
  153. Link { to: Route::List, class: "pure-button", "Go back" }
  154. }
  155. }