crm.rs 5.3 KB

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