todomvcsingle.rs 8.0 KB

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