crm.rs 5.2 KB

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