crm.rs 4.2 KB

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