crm.rs 4.8 KB

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