list.rs 6.0 KB

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