crm.rs 4.5 KB

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