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