|
@@ -19,15 +19,15 @@ pub struct TodoItem {
|
|
}
|
|
}
|
|
|
|
|
|
pub fn app(cx: Scope<()>) -> Element {
|
|
pub fn app(cx: Scope<()>) -> Element {
|
|
- let (todos, set_todos) = use_state(&cx, im_rc::HashMap::<u32, TodoItem>::default);
|
|
|
|
- let (filter, set_filter) = use_state(&cx, || FilterState::All);
|
|
|
|
- let (draft, set_draft) = use_state(&cx, || "".to_string());
|
|
|
|
- let (todo_id, set_todo_id) = use_state(&cx, || 0);
|
|
|
|
|
|
+ let todos = use_state(&cx, im_rc::HashMap::<u32, TodoItem>::default);
|
|
|
|
+ let filter = use_state(&cx, || FilterState::All);
|
|
|
|
+ let draft = use_state(&cx, || "".to_string());
|
|
|
|
+ let todo_id = use_state(&cx, || 0);
|
|
|
|
|
|
// Filter the todos based on the filter state
|
|
// Filter the todos based on the filter state
|
|
let mut filtered_todos = todos
|
|
let mut filtered_todos = todos
|
|
.iter()
|
|
.iter()
|
|
- .filter(|(_, item)| match filter {
|
|
|
|
|
|
+ .filter(|(_, item)| match **filter {
|
|
FilterState::All => true,
|
|
FilterState::All => true,
|
|
FilterState::Active => !item.checked,
|
|
FilterState::Active => !item.checked,
|
|
FilterState::Completed => item.checked,
|
|
FilterState::Completed => item.checked,
|
|
@@ -54,25 +54,25 @@ pub fn app(cx: Scope<()>) -> Element {
|
|
placeholder: "What needs to be done?",
|
|
placeholder: "What needs to be done?",
|
|
value: "{draft}",
|
|
value: "{draft}",
|
|
autofocus: "true",
|
|
autofocus: "true",
|
|
- oninput: move |evt| set_draft(evt.value.clone()),
|
|
|
|
|
|
+ oninput: move |evt| draft.set(evt.value.clone()),
|
|
onkeydown: move |evt| {
|
|
onkeydown: move |evt| {
|
|
if evt.key == "Enter" && !draft.is_empty() {
|
|
if evt.key == "Enter" && !draft.is_empty() {
|
|
- set_todos.make_mut().insert(
|
|
|
|
- *todo_id,
|
|
|
|
|
|
+ todos.make_mut().insert(
|
|
|
|
+ **todo_id,
|
|
TodoItem {
|
|
TodoItem {
|
|
- id: *todo_id,
|
|
|
|
|
|
+ id: **todo_id,
|
|
checked: false,
|
|
checked: false,
|
|
- contents: draft.clone(),
|
|
|
|
|
|
+ contents: draft.to_string(),
|
|
},
|
|
},
|
|
);
|
|
);
|
|
- set_todo_id(todo_id + 1);
|
|
|
|
- set_draft("".to_string());
|
|
|
|
|
|
+ *todo_id.make_mut() += 1;
|
|
|
|
+ draft.set("".to_string());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ul { class: "todo-list",
|
|
ul { class: "todo-list",
|
|
- filtered_todos.iter().map(|id| rsx!(todo_entry( key: "{id}", id: *id, set_todos: set_todos )))
|
|
|
|
|
|
+ filtered_todos.iter().map(|id| rsx!(todo_entry( key: "{id}", id: *id, todos: todos )))
|
|
}
|
|
}
|
|
(!todos.is_empty()).then(|| rsx!(
|
|
(!todos.is_empty()).then(|| rsx!(
|
|
footer { class: "footer",
|
|
footer { class: "footer",
|
|
@@ -81,14 +81,14 @@ pub fn app(cx: Scope<()>) -> Element {
|
|
span {"{item_text} left"}
|
|
span {"{item_text} left"}
|
|
}
|
|
}
|
|
ul { class: "filters",
|
|
ul { class: "filters",
|
|
- li { class: "All", a { onclick: move |_| set_filter(FilterState::All), "All" }}
|
|
|
|
- li { class: "Active", a { onclick: move |_| set_filter(FilterState::Active), "Active" }}
|
|
|
|
- li { class: "Completed", a { onclick: move |_| set_filter(FilterState::Completed), "Completed" }}
|
|
|
|
|
|
+ li { class: "All", a { onclick: move |_| filter.set(FilterState::All), "All" }}
|
|
|
|
+ li { class: "Active", a { onclick: move |_| filter.set(FilterState::Active), "Active" }}
|
|
|
|
+ li { class: "Completed", a { onclick: move |_| filter.set(FilterState::Completed), "Completed" }}
|
|
}
|
|
}
|
|
(show_clear_completed).then(|| rsx!(
|
|
(show_clear_completed).then(|| rsx!(
|
|
button {
|
|
button {
|
|
class: "clear-completed",
|
|
class: "clear-completed",
|
|
- onclick: move |_| set_todos.make_mut().retain(|_, todo| !todo.checked),
|
|
|
|
|
|
+ onclick: move |_| todos.make_mut().retain(|_, todo| !todo.checked),
|
|
"Clear completed"
|
|
"Clear completed"
|
|
}
|
|
}
|
|
))
|
|
))
|
|
@@ -106,29 +106,29 @@ pub fn app(cx: Scope<()>) -> Element {
|
|
|
|
|
|
#[derive(Props)]
|
|
#[derive(Props)]
|
|
pub struct TodoEntryProps<'a> {
|
|
pub struct TodoEntryProps<'a> {
|
|
- set_todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
|
|
|
|
|
+ todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
|
id: u32,
|
|
id: u32,
|
|
}
|
|
}
|
|
|
|
|
|
pub fn todo_entry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
|
|
pub fn todo_entry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
|
|
- let (is_editing, set_is_editing) = use_state(&cx, || false);
|
|
|
|
|
|
+ let is_editing = use_state(&cx, || false);
|
|
|
|
|
|
- let todos = cx.props.set_todos.get();
|
|
|
|
|
|
+ let todos = cx.props.todos.get();
|
|
let todo = &todos[&cx.props.id];
|
|
let todo = &todos[&cx.props.id];
|
|
let completed = if todo.checked { "completed" } else { "" };
|
|
let completed = if todo.checked { "completed" } else { "" };
|
|
- let editing = if *is_editing { "editing" } else { "" };
|
|
|
|
|
|
+ let editing = if **is_editing { "editing" } else { "" };
|
|
|
|
|
|
rsx!(cx, li {
|
|
rsx!(cx, li {
|
|
class: "{completed} {editing}",
|
|
class: "{completed} {editing}",
|
|
div { class: "view",
|
|
div { class: "view",
|
|
input { class: "toggle", r#type: "checkbox", id: "cbg-{todo.id}", checked: "{todo.checked}",
|
|
input { class: "toggle", r#type: "checkbox", id: "cbg-{todo.id}", checked: "{todo.checked}",
|
|
onchange: move |evt| {
|
|
onchange: move |evt| {
|
|
- cx.props.set_todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
|
|
|
|
|
|
+ cx.props.todos.make_mut()[&cx.props.id].checked = evt.value.parse().unwrap();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
label {
|
|
label {
|
|
r#for: "cbg-{todo.id}",
|
|
r#for: "cbg-{todo.id}",
|
|
- onclick: move |_| set_is_editing(true),
|
|
|
|
|
|
+ onclick: move |_| is_editing.set(true),
|
|
"{todo.contents}"
|
|
"{todo.contents}"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -136,12 +136,12 @@ pub fn todo_entry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
|
|
input {
|
|
input {
|
|
class: "edit",
|
|
class: "edit",
|
|
value: "{todo.contents}",
|
|
value: "{todo.contents}",
|
|
- oninput: move |evt| cx.props.set_todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
|
|
|
|
|
|
+ oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].contents = evt.value.clone(),
|
|
autofocus: "true",
|
|
autofocus: "true",
|
|
- onfocusout: move |_| set_is_editing(false),
|
|
|
|
|
|
+ onfocusout: move |_| is_editing.set(false),
|
|
onkeydown: move |evt| {
|
|
onkeydown: move |evt| {
|
|
match evt.key.as_str() {
|
|
match evt.key.as_str() {
|
|
- "Enter" | "Escape" | "Tab" => set_is_editing(false),
|
|
|
|
|
|
+ "Enter" | "Escape" | "Tab" => is_editing.set(false),
|
|
_ => {}
|
|
_ => {}
|
|
}
|
|
}
|
|
},
|
|
},
|