todomvcsingle.rs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #![allow(non_snake_case)]
  2. use dioxus_core::prelude::*;
  3. use dioxus_web::WebsysRenderer;
  4. static APP_STYLE: &'static str = include_str!("./todomvc/style.css");
  5. pub static TODOS: AtomFamily<uuid::Uuid, TodoItem> = atom_family(|_| {});
  6. pub static FILTER: Atom<FilterState> = atom(|_| FilterState::All);
  7. pub static SHOW_ALL_TODOS: selector<bool> = selector(|g| g.getter(|f| false));
  8. fn main() {
  9. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(|ctx, props| {
  10. ctx.render(rsx! {
  11. div {
  12. id: "app",
  13. style { "{APP_STYLE}" }
  14. TodoList {}
  15. Footer {}
  16. }
  17. })
  18. }));
  19. }
  20. pub fn TodoList(ctx: Context, props: &()) -> DomTree {
  21. let (draft, set_draft) = use_state(&ctx, || "".to_string());
  22. let (todos, _) = use_state(&ctx, || Vec::<TodoItem>::new());
  23. let filter = use_atom(&ctx, &FILTER);
  24. ctx.render(rsx! {
  25. div {
  26. header {
  27. class: "header"
  28. h1 {"todos"}
  29. input {
  30. class: "new-todo"
  31. placeholder: "What needs to be done?"
  32. value: "{draft}"
  33. oninput: move |evt| set_draft(evt.value)
  34. }
  35. }
  36. { // list
  37. todos
  38. .iter()
  39. .filter(|item| match filter {
  40. FilterState::All => true,
  41. FilterState::Active => !item.checked,
  42. FilterState::Completed => item.checked,
  43. })
  44. .map(|item| {
  45. rsx!(TodoEntry {
  46. key: "{order}",
  47. id: item.id,
  48. })
  49. })
  50. }
  51. // filter toggle (show only if the list isn't empty)
  52. {(!todos.is_empty()).then(||
  53. rsx!( FilterToggles {})
  54. )}
  55. }
  56. })
  57. }
  58. #[derive(PartialEq, Props)]
  59. pub struct TodoEntryProps {
  60. id: uuid::Uuid,
  61. }
  62. pub fn TodoEntry(ctx: Context, props: &TodoEntryProps) -> DomTree {
  63. let (is_editing, set_is_editing) = use_state(&ctx, || false);
  64. let todo = use_atom_family(&ctx, &TODOS, props.id);
  65. let contents = "";
  66. ctx.render(rsx! (
  67. li {
  68. "{todo.id}"
  69. input {
  70. class: "toggle"
  71. type: "checkbox"
  72. "{todo.checked}"
  73. }
  74. {is_editing.then(|| rsx!(
  75. input {
  76. value: "{contents}"
  77. }
  78. ))}
  79. }
  80. ))
  81. }
  82. pub fn FilterToggles(ctx: Context, props: &()) -> DomTree {
  83. let reducer = recoil::use_callback(&ctx, || ());
  84. let items_left = recoil::use_atom_family(&ctx, &TODOS, uuid::Uuid::new_v4());
  85. let toggles = [
  86. ("All", "", FilterState::All),
  87. ("Active", "active", FilterState::Active),
  88. ("Completed", "completed", FilterState::Completed),
  89. ]
  90. .iter()
  91. .map(|(name, path, filter)| {
  92. rsx!(
  93. li {
  94. class: "{name}"
  95. a {
  96. href: "{path}"
  97. onclick: move |_| reducer.set_filter(&filter)
  98. "{name}"
  99. }
  100. }
  101. )
  102. });
  103. // todo
  104. let item_text = "";
  105. let items_left = "";
  106. ctx.render(rsx! {
  107. footer {
  108. span {
  109. strong {"{items_left}"}
  110. span {"{item_text} left"}
  111. }
  112. ul {
  113. class: "filters"
  114. {toggles}
  115. }
  116. }
  117. })
  118. }
  119. pub fn Footer(ctx: Context, props: &()) -> DomTree {
  120. ctx.render(rsx! {
  121. footer {
  122. class: "info"
  123. p {"Double-click to edit a todo"}
  124. p {
  125. "Created by "
  126. a { "jkelleyrtp", href: "http://github.com/jkelleyrtp/" }
  127. }
  128. p {
  129. "Part of "
  130. a { "TodoMVC", href: "http://todomvc.com" }
  131. }
  132. }
  133. })
  134. }
  135. #[derive(PartialEq)]
  136. pub enum FilterState {
  137. All,
  138. Active,
  139. Completed,
  140. }
  141. #[derive(Debug, PartialEq, Clone)]
  142. pub struct TodoItem {
  143. pub id: uuid::Uuid,
  144. pub checked: bool,
  145. pub contents: String,
  146. }
  147. impl RecoilContext<()> {
  148. pub fn add_todo(&self, contents: String) {}
  149. pub fn remove_todo(&self, id: &uuid::Uuid) {}
  150. pub fn select_all_todos(&self) {}
  151. pub fn toggle_todo(&self, id: &uuid::Uuid) {}
  152. pub fn clear_completed(&self) {}
  153. pub fn set_filter(&self, filter: &FilterState) {}
  154. }
  155. pub use recoil::*;
  156. mod recoil {
  157. use dioxus_core::context::Context;
  158. pub struct RecoilContext<T: 'static> {
  159. _inner: T,
  160. }
  161. impl<T: 'static> RecoilContext<T> {
  162. /// Get the value of an atom. Returns a reference to the underlying data.
  163. pub fn get(&self) {}
  164. /// Replace an existing value with a new value
  165. ///
  166. /// This does not replace the value instantly, and all calls to "get" within the current scope will return
  167. pub fn set(&self) {}
  168. // Modify lets you modify the value in place. However, because there's no previous value around to compare
  169. // the new one with, we are unable to memoize the change. As such, all downsteam users of this Atom will
  170. // be updated, causing all subsrcibed components to re-render.
  171. //
  172. // This is fine for most values, but might not be performant when dealing with collections. For collections,
  173. // use the "Family" variants as these will stay memoized for inserts, removals, and modifications.
  174. //
  175. // Note - like "set" this won't propogate instantly. Once all "gets" are dropped, only then will we run the
  176. pub fn modify(&self) {}
  177. }
  178. pub fn use_callback<'a, G>(c: &Context<'a>, f: impl Fn() -> G) -> &'a RecoilContext<G> {
  179. todo!()
  180. }
  181. pub fn use_atom<T: PartialEq, O>(c: &Context, t: &'static Atom<T>) -> O {
  182. todo!()
  183. }
  184. pub fn use_batom<T: PartialEq, O>(c: &Context, t: impl Readable) -> O {
  185. todo!()
  186. }
  187. pub trait Readable {}
  188. impl<T: PartialEq> Readable for &'static Atom<T> {}
  189. impl<K: PartialEq, V: PartialEq> Readable for &'static AtomFamily<K, V> {}
  190. pub fn use_atom_family<'a, K: PartialEq, V: PartialEq>(
  191. c: &Context<'a>,
  192. t: &'static AtomFamily<K, V>,
  193. g: K,
  194. ) -> &'a V {
  195. todo!()
  196. }
  197. pub use atoms::{atom, Atom};
  198. pub use atoms::{atom_family, AtomFamily};
  199. mod atoms {
  200. use super::*;
  201. pub struct AtomBuilder<T: PartialEq> {
  202. pub key: String,
  203. pub manual_init: Option<Box<dyn Fn() -> T>>,
  204. _never: std::marker::PhantomData<T>,
  205. }
  206. impl<T: PartialEq> AtomBuilder<T> {
  207. pub fn new() -> Self {
  208. Self {
  209. key: uuid::Uuid::new_v4().to_string(),
  210. manual_init: None,
  211. _never: std::marker::PhantomData {},
  212. }
  213. }
  214. pub fn init<A: Fn() -> T + 'static>(&mut self, f: A) {
  215. self.manual_init = Some(Box::new(f));
  216. }
  217. pub fn set_key(&mut self, _key: &'static str) {}
  218. }
  219. pub struct atom<T: PartialEq>(pub fn(&mut AtomBuilder<T>) -> T);
  220. pub type Atom<T: PartialEq> = atom<T>;
  221. pub struct AtomFamilyBuilder<K, V> {
  222. _never: std::marker::PhantomData<(K, V)>,
  223. }
  224. pub struct atom_family<K: PartialEq, V: PartialEq>(pub fn(&mut AtomFamilyBuilder<K, V>));
  225. pub type AtomFamily<K: PartialEq, V: PartialEq> = atom_family<K, V>;
  226. }
  227. pub use selectors::selector;
  228. mod selectors {
  229. pub struct SelectorBuilder<Out, const Built: bool> {
  230. _p: std::marker::PhantomData<Out>,
  231. }
  232. impl<O> SelectorBuilder<O, false> {
  233. pub fn getter(self, f: impl Fn(()) -> O) -> SelectorBuilder<O, true> {
  234. todo!()
  235. // std::rc::Rc::pin(value)
  236. // todo!()
  237. }
  238. }
  239. pub struct selector<O>(pub fn(SelectorBuilder<O, false>) -> SelectorBuilder<O, true>);
  240. }
  241. }