todomvc.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. use std::{
  2. collections::{BTreeMap, BTreeSet},
  3. sync::atomic::AtomicUsize,
  4. };
  5. use dioxus_core::prelude::*;
  6. use dioxus_web::WebsysRenderer;
  7. use uuid::Uuid;
  8. // Entry point
  9. fn main() {
  10. wasm_bindgen_futures::spawn_local(WebsysRenderer::start(|ctx, props| {
  11. ctx.create_context(|| model::TodoManager::new());
  12. ctx.render(rsx! {
  13. div {
  14. TodoList {}
  15. Footer {}
  16. }
  17. })
  18. }))
  19. }
  20. static TodoList: FC<()> = |ctx, props| {
  21. let todos = use_state_new(&ctx, || BTreeMap::<usize, model::TodoItem>::new());
  22. let items = todos.iter().map(|(order, item)| {
  23. rsx!(TodoItem {
  24. // key: "{}",
  25. todo: item
  26. })
  27. });
  28. ctx.render(rsx! {
  29. div {
  30. {items}
  31. }
  32. })
  33. };
  34. #[derive(Debug, PartialEq, Props)]
  35. struct TodoItemsProp<'a> {
  36. todo: &'a model::TodoItem,
  37. }
  38. fn TodoItem(ctx: Context, props: &TodoItemsProp) -> DomTree {
  39. let (editing, set_editing) = use_state(&ctx, || false);
  40. let id = props.todo.id;
  41. ctx.render(rsx! (
  42. li {
  43. div {
  44. "{id}"
  45. }
  46. // {input}
  47. }
  48. ))
  49. }
  50. static Footer: FC<()> = |ctx, props| {
  51. ctx.render(html! {
  52. <footer className="info">
  53. <p>"Double-click to edit a todo"</p>
  54. <p>
  55. "Created by "<a href="http://github.com/jkelleyrtp/">"jkelleyrtp"</a>
  56. </p>
  57. <p>
  58. "Part of "<a href="http://todomvc.com">"TodoMVC"</a>
  59. </p>
  60. </footer>
  61. })
  62. };
  63. // The data model that the todo mvc uses
  64. mod model {
  65. use std::{borrow::BorrowMut, future::Future};
  66. use super::*;
  67. #[derive(Debug, PartialEq, Clone)]
  68. pub struct TodoItem {
  69. pub id: Uuid,
  70. pub checked: bool,
  71. pub contents: String,
  72. }
  73. struct Dispatcher {}
  74. struct AppContext<T: Clone> {
  75. _t: std::rc::Rc<T>,
  76. }
  77. impl<T: Clone> AppContext<T> {
  78. fn dispatch(&self, f: impl FnOnce(&mut T)) {}
  79. fn async_dispatch(&self, f: impl Future<Output = ()>) {}
  80. fn get<G>(&self, f: impl Fn(&T) -> &G) -> &G {
  81. f(&self._t)
  82. }
  83. fn set(&self, orig: &mut std::borrow::Cow<T>) {
  84. let r = orig.to_mut();
  85. }
  86. }
  87. // use im-rc if your contexts are too large to clone!
  88. // or, dangerously mutate and update subscriptions manually
  89. #[derive(Clone)]
  90. pub struct TodoManager {
  91. items: Vec<u32>,
  92. }
  93. impl AppContext<TodoManager> {
  94. fn remove_todo(&self, id: Uuid) {
  95. self.dispatch(|f| {})
  96. }
  97. async fn push_todo(&self, todo: TodoItem) {
  98. self.dispatch(|f| {
  99. //
  100. f.items.push(10);
  101. });
  102. }
  103. fn add_todo(&self) {
  104. // self.dispatch(|f| {});
  105. // let items = self.get(|f| &f.items);
  106. }
  107. }
  108. impl TodoManager {
  109. pub fn new() -> Self {
  110. todo!()
  111. }
  112. pub fn get_todo(&self) -> &TodoItem {
  113. todo!()
  114. }
  115. }
  116. pub struct TodoHandle {}
  117. impl TodoHandle {
  118. fn get_todo(&self, id: Uuid) -> &TodoItem {
  119. todo!()
  120. }
  121. fn add_todo(&self, todo: TodoItem) {}
  122. }
  123. // use_reducer, but exposes the reducer and context to children
  124. fn use_reducer_context() {}
  125. fn use_context_selector() {}
  126. fn use_context<'b, 'c, Root: 'static, Item: 'c>(
  127. ctx: &'b Context<'c>,
  128. f: impl Fn(Root) -> &'c Item,
  129. ) -> &'c Item {
  130. todo!()
  131. }
  132. pub fn use_todo_item<'b, 'c>(ctx: &'b Context<'c>, item: Uuid) -> &'c TodoItem {
  133. todo!()
  134. // ctx.use_hook(|| TodoManager::new(), |hook| {}, cleanup)
  135. }
  136. fn use_todos(ctx: &Context) -> TodoHandle {
  137. todo!()
  138. }
  139. fn use_todo_context(ctx: &Context) -> AppContext<TodoManager> {
  140. todo!()
  141. }
  142. fn test(ctx: Context) {
  143. let todos = use_todos(&ctx);
  144. let todo = todos.get_todo(Uuid::new_v4());
  145. let c = use_todo_context(&ctx);
  146. // todos.add_todo();
  147. }
  148. }