todomvcsingle.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. let contents = "";
  105. ctx.render(rsx! (
  106. li {
  107. "{todo.id}"
  108. input {
  109. class: "toggle"
  110. type: "checkbox"
  111. "{todo.checked}"
  112. }
  113. {is_editing.then(|| rsx!(
  114. input {
  115. value: "{contents}"
  116. }
  117. ))}
  118. }
  119. ))
  120. }
  121. pub fn FilterToggles(ctx: Context, props: &()) -> DomTree {
  122. let reducer = recoil::use_callback(&ctx, || ());
  123. let items_left = recoil::use_atom_family(&ctx, &TODOS, uuid::Uuid::new_v4());
  124. let toggles = [
  125. ("All", "", FilterState::All),
  126. ("Active", "active", FilterState::Active),
  127. ("Completed", "completed", FilterState::Completed),
  128. ]
  129. .iter()
  130. .map(|(name, path, filter)| {
  131. rsx!(
  132. li {
  133. class: "{name}"
  134. a {
  135. href: "{path}"
  136. onclick: move |_| reducer.set_filter(&filter)
  137. "{name}"
  138. }
  139. }
  140. )
  141. });
  142. // todo
  143. let item_text = "";
  144. let items_left = "";
  145. ctx.render(rsx! {
  146. footer {
  147. span {
  148. strong {"{items_left}"}
  149. span {"{item_text} left"}
  150. }
  151. ul {
  152. class: "filters"
  153. {toggles}
  154. }
  155. }
  156. })
  157. }
  158. pub use recoil::*;
  159. mod recoil {
  160. use dioxus_core::context::Context;
  161. pub struct RecoilContext<T: 'static> {
  162. _inner: T,
  163. }
  164. impl<T: 'static> RecoilContext<T> {
  165. /// Get the value of an atom. Returns a reference to the underlying data.
  166. pub fn get(&self) {}
  167. /// Replace an existing value with a new value
  168. ///
  169. /// This does not replace the value instantly, and all calls to "get" within the current scope will return
  170. pub fn set(&self) {}
  171. // Modify lets you modify the value in place. However, because there's no previous value around to compare
  172. // the new one with, we are unable to memoize the change. As such, all downsteam users of this Atom will
  173. // be updated, causing all subsrcibed components to re-render.
  174. //
  175. // This is fine for most values, but might not be performant when dealing with collections. For collections,
  176. // use the "Family" variants as these will stay memoized for inserts, removals, and modifications.
  177. //
  178. // Note - like "set" this won't propogate instantly. Once all "gets" are dropped, only then will we run the
  179. pub fn modify(&self) {}
  180. }
  181. pub fn use_callback<'a, G>(c: &Context<'a>, f: impl Fn() -> G) -> &'a RecoilContext<G> {
  182. todo!()
  183. }
  184. pub fn use_atom<T: PartialEq, O>(c: &Context, t: &'static Atom<T>) -> O {
  185. todo!()
  186. }
  187. pub fn use_batom<T: PartialEq, O>(c: &Context, t: impl Readable) -> O {
  188. todo!()
  189. }
  190. pub trait Readable {}
  191. impl<T: PartialEq> Readable for &'static Atom<T> {}
  192. impl<K: PartialEq, V: PartialEq> Readable for &'static AtomFamily<K, V> {}
  193. pub fn use_atom_family<'a, K: PartialEq, V: PartialEq>(
  194. c: &Context<'a>,
  195. t: &'static AtomFamily<K, V>,
  196. g: K,
  197. ) -> &'a V {
  198. todo!()
  199. }
  200. pub use atoms::{atom, Atom};
  201. pub use atoms::{atom_family, AtomFamily};
  202. mod atoms {
  203. use super::*;
  204. pub struct AtomBuilder<T: PartialEq> {
  205. pub key: String,
  206. pub manual_init: Option<Box<dyn Fn() -> T>>,
  207. _never: std::marker::PhantomData<T>,
  208. }
  209. impl<T: PartialEq> AtomBuilder<T> {
  210. pub fn new() -> Self {
  211. Self {
  212. key: uuid::Uuid::new_v4().to_string(),
  213. manual_init: None,
  214. _never: std::marker::PhantomData {},
  215. }
  216. }
  217. pub fn init<A: Fn() -> T + 'static>(&mut self, f: A) {
  218. self.manual_init = Some(Box::new(f));
  219. }
  220. pub fn set_key(&mut self, _key: &'static str) {}
  221. }
  222. pub struct atom<T: PartialEq>(pub fn(&mut AtomBuilder<T>) -> T);
  223. pub type Atom<T: PartialEq> = atom<T>;
  224. pub struct AtomFamilyBuilder<K, V> {
  225. _never: std::marker::PhantomData<(K, V)>,
  226. }
  227. pub struct atom_family<K: PartialEq, V: PartialEq>(pub fn(&mut AtomFamilyBuilder<K, V>));
  228. pub type AtomFamily<K: PartialEq, V: PartialEq> = atom_family<K, V>;
  229. }
  230. pub use selectors::selector;
  231. mod selectors {
  232. pub struct SelectorBuilder<Out, const Built: bool> {
  233. _p: std::marker::PhantomData<Out>,
  234. }
  235. impl<O> SelectorBuilder<O, false> {
  236. pub fn getter(self, f: impl Fn(()) -> O) -> SelectorBuilder<O, true> {
  237. todo!()
  238. // std::rc::Rc::pin(value)
  239. // todo!()
  240. }
  241. }
  242. pub struct selector<O>(pub fn(SelectorBuilder<O, false>) -> SelectorBuilder<O, true>);
  243. }
  244. }