crm.rs 4.7 KB

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