crm.rs 4.4 KB

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