crm.rs 4.9 KB

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