1
0

list.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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<()> = |ctx, _| {
  24. let (draft, set_draft) = use_state(&ctx, || "".to_string());
  25. let (filter, set_filter) = use_state(&ctx, || FilterState::All);
  26. let (is_editing, set_is_editing) = use_state(&ctx, || false);
  27. // let (todos, set_todos) = use_state(&ctx, || BTreeMap::<String, TodoItem>::new());
  28. // let todos = use_ref(&ctx, || BTreeMap::<String, TodoItem>::new());
  29. let todos = use_state_new(&ctx, || BTreeMap::<uuid::Uuid, TodoItem>::new());
  30. // let blah = "{draft}"
  31. ctx.render(rsx!(
  32. div {
  33. id: "app"
  34. style { "{APP_STYLE}" }
  35. div {
  36. header {
  37. class: "header"
  38. h1 {"todos"}
  39. button {
  40. "press me"
  41. onclick: move |evt| {
  42. let contents = draft.clone();
  43. todos.modify(|f| {
  44. let id = uuid::Uuid::new_v4();
  45. f.insert(id.clone(), TodoItem {
  46. id,
  47. checked: false,
  48. contents
  49. });
  50. })
  51. }
  52. }
  53. input {
  54. class: "new-todo"
  55. placeholder: "What needs to be done?"
  56. // value: "{draft}"
  57. oninput: move |evt| set_draft(evt.value)
  58. }
  59. }
  60. { // list
  61. todos
  62. .iter()
  63. .filter(|(id, item)| match filter {
  64. FilterState::All => true,
  65. FilterState::Active => !item.checked,
  66. FilterState::Completed => item.checked,
  67. })
  68. .map(|(id, todo)| {
  69. rsx!{
  70. li {
  71. "{todo.contents}"
  72. input {
  73. class: "toggle"
  74. type: "checkbox"
  75. "{todo.checked}"
  76. }
  77. // {is_editing.then(|| rsx!(
  78. // input {
  79. // value: "{contents}"
  80. // }
  81. // ))}
  82. }
  83. }
  84. })
  85. }
  86. // filter toggle (show only if the list isn't empty)
  87. {(!todos.is_empty()).then(||
  88. rsx!{
  89. footer {
  90. span {
  91. strong {"10"}
  92. span {"0 items left"}
  93. }
  94. ul {
  95. class: "filters"
  96. {[
  97. ("All", "", FilterState::All),
  98. ("Active", "active", FilterState::Active),
  99. ("Completed", "completed", FilterState::Completed),
  100. ]
  101. .iter()
  102. .map(|(name, path, filter)| {
  103. rsx!(
  104. li {
  105. class: "{name}"
  106. a {
  107. href: "{path}"
  108. onclick: move |_| set_filter(filter.clone())
  109. "{name}"
  110. }
  111. }
  112. )
  113. })
  114. }}
  115. }
  116. }
  117. )}
  118. }
  119. footer {
  120. class: "info"
  121. p {"Double-click to edit a todo"}
  122. p {
  123. "Created by "
  124. a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }
  125. }
  126. p {
  127. "Part of "
  128. a { "TodoMVC", href: "http://todomvc.com" }
  129. }
  130. }
  131. }
  132. ))
  133. };
  134. pub fn FilterToggles(ctx: Context, props: &()) -> DomTree {
  135. // let reducer = recoil::use_callback(&ctx, || ());
  136. // let items_left = recoil::use_atom_family(&ctx, &TODOS, uuid::Uuid::new_v4());
  137. let toggles = [
  138. ("All", "", FilterState::All),
  139. ("Active", "active", FilterState::Active),
  140. ("Completed", "completed", FilterState::Completed),
  141. ]
  142. .iter()
  143. .map(|(name, path, _filter)| {
  144. rsx!(
  145. li {
  146. class: "{name}"
  147. a {
  148. href: "{path}"
  149. // onclick: move |_| reducer.set_filter(&filter)
  150. "{name}"
  151. }
  152. }
  153. )
  154. });
  155. // todo
  156. let item_text = "";
  157. let items_left = "";
  158. ctx.render(rsx! {
  159. footer {
  160. span {
  161. strong {"{items_left}"}
  162. span {"{item_text} left"}
  163. }
  164. ul {
  165. class: "filters"
  166. {toggles}
  167. }
  168. }
  169. })
  170. }