list.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. use dioxus_core as dioxus;
  2. use dioxus_core::prelude::*;
  3. use dioxus_web::WebsysRenderer;
  4. use std::collections::BTreeMap;
  5. fn main() {
  6. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  7. console_error_panic_hook::set_once();
  8. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App));
  9. }
  10. static APP_STYLE: &'static str = include_str!("./todomvc/style.css");
  11. #[derive(PartialEq, Clone, Copy)]
  12. pub enum FilterState {
  13. All,
  14. Active,
  15. Completed,
  16. }
  17. #[derive(Debug, PartialEq, Clone)]
  18. pub struct TodoItem {
  19. pub id: uuid::Uuid,
  20. pub checked: bool,
  21. pub contents: String,
  22. }
  23. static App: FC<()> = |cx| {
  24. let (draft, set_draft) = use_state(&cx, || "".to_string());
  25. let (filter, set_filter) = use_state(&cx, || FilterState::All);
  26. let todos = use_state_new(&cx, || BTreeMap::<uuid::Uuid, TodoItem>::new());
  27. cx.render(rsx!(
  28. div {
  29. id: "app"
  30. div {
  31. header {
  32. class: "header"
  33. h1 {"todos"}
  34. button {
  35. "press me"
  36. onclick: move |evt| {
  37. let contents = draft.clone();
  38. todos.modify(|f| {
  39. let id = uuid::Uuid::new_v4();
  40. f.insert(id.clone(), TodoItem {
  41. id,
  42. checked: false,
  43. contents
  44. });
  45. })
  46. }
  47. }
  48. input {
  49. class: "new-todo"
  50. placeholder: "What needs to be done?"
  51. oninput: move |evt| set_draft(evt.value())
  52. }
  53. }
  54. { // list
  55. todos
  56. .iter()
  57. .filter(|(id, item)| match filter {
  58. FilterState::All => true,
  59. FilterState::Active => !item.checked,
  60. FilterState::Completed => item.checked,
  61. })
  62. .map(|(id, todo)| {
  63. rsx!{
  64. li {
  65. key: "{id}"
  66. "{todo.contents}"
  67. input {
  68. class: "toggle"
  69. type: "checkbox"
  70. "{todo.checked}"
  71. }
  72. }
  73. }
  74. })
  75. }
  76. // filter toggle (show only if the list isn't empty)
  77. {(!todos.is_empty()).then(||
  78. rsx!{
  79. footer {
  80. span {
  81. strong {"10"}
  82. span {"0 items left"}
  83. }
  84. ul {
  85. class: "filters"
  86. {[
  87. ("All", "", FilterState::All),
  88. ("Active", "active", FilterState::Active),
  89. ("Completed", "completed", FilterState::Completed),
  90. ]
  91. .iter()
  92. .map(|(name, path, filter)| {
  93. rsx!(
  94. li {
  95. class: "{name}"
  96. a {
  97. href: "{path}"
  98. onclick: move |_| set_filter(filter.clone())
  99. "{name}"
  100. }
  101. }
  102. )
  103. })
  104. }}
  105. }
  106. }
  107. )}
  108. }
  109. footer {
  110. class: "info"
  111. p {"Double-click to edit a todo"}
  112. p {
  113. "Created by "
  114. a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }
  115. }
  116. p {
  117. "Part of "
  118. a { "TodoMVC", href: "http://todomvc.com" }
  119. }
  120. }
  121. }
  122. ))
  123. };
  124. pub fn FilterToggles(cx: Context<()>) -> VNode {
  125. // let reducer = recoil::use_callback(&cx, || ());
  126. // let items_left = recoil::use_atom_family(&cx, &TODOS, uuid::Uuid::new_v4());
  127. let toggles = [
  128. ("All", "", FilterState::All),
  129. ("Active", "active", FilterState::Active),
  130. ("Completed", "completed", FilterState::Completed),
  131. ]
  132. .iter()
  133. .map(|(name, path, _filter)| {
  134. rsx!(
  135. li {
  136. class: "{name}"
  137. a {
  138. href: "{path}"
  139. // onclick: move |_| reducer.set_filter(&filter)
  140. "{name}"
  141. }
  142. }
  143. )
  144. });
  145. // todo
  146. let item_text = "";
  147. let items_left = "";
  148. cx.render(rsx! {
  149. footer {
  150. span {
  151. strong {"{items_left}"}
  152. span {"{item_text} left"}
  153. }
  154. ul {
  155. class: "filters"
  156. {toggles}
  157. }
  158. }
  159. })
  160. }