crm.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(cx: Scope) -> Element {
  26. use_shared_state_provider::<ClientContext>(cx, 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(cx: Scope) -> Element {
  47. let clients = use_shared_state::<ClientContext>(cx).unwrap();
  48. cx.render(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. clients.read().iter().map(|client| rsx! {
  53. div {
  54. class: "client",
  55. style: "margin-bottom: 50px",
  56. p { "Name: {client.first_name} {client.last_name}" }
  57. p { "Description: {client.description}" }
  58. }
  59. })
  60. })
  61. }
  62. #[component]
  63. fn ClientAdd(cx: Scope) -> Element {
  64. let clients = use_shared_state::<ClientContext>(cx).unwrap();
  65. let first_name = use_state(cx, String::new);
  66. let last_name = use_state(cx, String::new);
  67. let description = use_state(cx, String::new);
  68. cx.render(rsx! {
  69. h2 { "Add new Client" }
  70. form {
  71. class: "pure-form pure-form-aligned",
  72. onsubmit: move |_| {
  73. let mut clients = clients.write();
  74. clients
  75. .push(Client {
  76. first_name: first_name.to_string(),
  77. last_name: last_name.to_string(),
  78. description: description.to_string(),
  79. });
  80. dioxus_router::router().push(Route::ClientList {});
  81. },
  82. fieldset {
  83. div { class: "pure-control-group",
  84. label { "for": "first_name", "First Name" }
  85. input {
  86. id: "first_name",
  87. "type": "text",
  88. placeholder: "First Name…",
  89. required: "",
  90. value: "{first_name}",
  91. oninput: move |e| first_name.set(e.value())
  92. }
  93. }
  94. div { class: "pure-control-group",
  95. label { "for": "last_name", "Last Name" }
  96. input {
  97. id: "last_name",
  98. "type": "text",
  99. placeholder: "Last Name…",
  100. required: "",
  101. value: "{last_name}",
  102. oninput: move |e| last_name.set(e.value())
  103. }
  104. }
  105. div { class: "pure-control-group",
  106. label { "for": "description", "Description" }
  107. textarea {
  108. id: "description",
  109. placeholder: "Description…",
  110. value: "{description}",
  111. oninput: move |e| description.set(e.value())
  112. }
  113. }
  114. div { class: "pure-controls",
  115. button { "type": "submit", class: "pure-button pure-button-primary", "Save" }
  116. Link { to: Route::ClientList {}, class: "pure-button pure-button-primary red", "Cancel" }
  117. }
  118. }
  119. }
  120. })
  121. }
  122. #[component]
  123. fn Settings(cx: Scope) -> Element {
  124. let clients = use_shared_state::<ClientContext>(cx).unwrap();
  125. cx.render(rsx! {
  126. h2 { "Settings" }
  127. button {
  128. class: "pure-button pure-button-primary red",
  129. onclick: move |_| {
  130. let mut clients = clients.write();
  131. clients.clear();
  132. },
  133. "Remove all Clients"
  134. }
  135. Link { to: Route::ClientList {}, class: "pure-button", "Go back" }
  136. })
  137. }