2021-07-16 20:11:25 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-01-03 05:42:17 +00:00
|
|
|
|
2021-10-11 02:27:08 +00:00
|
|
|
fn main() {
|
2022-01-02 23:35:38 +00:00
|
|
|
dioxus::desktop::launch(app);
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum FilterState {
|
|
|
|
All,
|
|
|
|
Active,
|
|
|
|
Completed,
|
|
|
|
}
|
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2021-07-16 20:11:25 +00:00
|
|
|
pub struct TodoItem {
|
|
|
|
pub id: u32,
|
|
|
|
pub checked: bool,
|
|
|
|
pub contents: String,
|
|
|
|
}
|
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
pub fn app(cx: Scope<()>) -> Element {
|
|
|
|
let todos = use_state(&cx, || im_rc::HashMap::<u32, TodoItem>::default());
|
2021-12-25 22:18:05 +00:00
|
|
|
let filter = use_state(&cx, || FilterState::All);
|
2022-01-03 05:42:17 +00:00
|
|
|
let draft = use_state(&cx, || "".to_string());
|
|
|
|
let mut todo_id = use_state(&cx, || 0);
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
// Filter the todos based on the filter state
|
|
|
|
let mut filtered_todos = todos
|
2021-07-16 20:11:25 +00:00
|
|
|
.iter()
|
2022-01-03 05:42:17 +00:00
|
|
|
.filter(|(_, item)| match *filter {
|
2021-07-16 20:11:25 +00:00
|
|
|
FilterState::All => true,
|
|
|
|
FilterState::Active => !item.checked,
|
|
|
|
FilterState::Completed => item.checked,
|
|
|
|
})
|
2022-01-03 05:42:17 +00:00
|
|
|
.map(|f| *f.0)
|
2021-07-16 20:11:25 +00:00
|
|
|
.collect::<Vec<_>>();
|
2022-01-03 05:42:17 +00:00
|
|
|
filtered_todos.sort_unstable();
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
let show_clear_completed = todos.values().any(|todo| todo.checked);
|
|
|
|
let items_left = filtered_todos.len();
|
2021-07-16 20:11:25 +00:00
|
|
|
let item_text = match items_left {
|
|
|
|
1 => "item",
|
|
|
|
_ => "items",
|
|
|
|
};
|
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
cx.render(rsx!{
|
|
|
|
section { class: "todoapp",
|
2022-01-02 23:35:38 +00:00
|
|
|
style { [include_str!("./assets/todomvc.css")] }
|
|
|
|
div {
|
2022-01-03 05:42:17 +00:00
|
|
|
header { class: "header",
|
|
|
|
h1 {"todos"}
|
2022-01-02 23:35:38 +00:00
|
|
|
input {
|
|
|
|
class: "new-todo",
|
|
|
|
placeholder: "What needs to be done?",
|
|
|
|
value: "{draft}",
|
2022-01-03 05:42:17 +00:00
|
|
|
autofocus: "true",
|
2022-01-02 23:35:38 +00:00
|
|
|
oninput: move |evt| draft.set(evt.value.clone()),
|
2022-01-03 05:42:17 +00:00
|
|
|
onkeydown: move |evt| {
|
|
|
|
if evt.key == "Enter" {
|
|
|
|
if !draft.is_empty() {
|
|
|
|
todos.modify().insert(
|
|
|
|
*todo_id,
|
|
|
|
TodoItem {
|
|
|
|
id: *todo_id,
|
|
|
|
checked: false,
|
|
|
|
contents: draft.get().clone(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
todo_id += 1;
|
|
|
|
draft.set("".to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 06:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
ul { class: "todo-list",
|
|
|
|
filtered_todos.iter().map(|id| rsx!(todo_entry( key: "{id}", id: *id, todos: todos )))
|
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
(!todos.is_empty()).then(|| rsx!(
|
2022-01-03 05:42:17 +00:00
|
|
|
footer { class: "footer",
|
2022-01-03 06:20:05 +00:00
|
|
|
span { class: "todo-count",
|
|
|
|
strong {"{items_left} "}
|
|
|
|
span {"{item_text} left"}
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
|
|
|
ul { class: "filters",
|
2022-01-03 05:42:17 +00:00
|
|
|
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" }}
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
(show_clear_completed).then(|| rsx!(
|
2022-01-03 06:20:05 +00:00
|
|
|
button {
|
|
|
|
class: "clear-completed",
|
2022-01-03 05:42:17 +00:00
|
|
|
onclick: move |_| todos.modify().retain(|_, todo| todo.checked == false),
|
|
|
|
"Clear completed"
|
|
|
|
}
|
|
|
|
))
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
footer { class: "info",
|
|
|
|
p {"Double-click to edit a todo"}
|
|
|
|
p { "Created by ", a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }}
|
|
|
|
p { "Part of ", a { href: "http://todomvc.com", "TodoMVC" }}
|
|
|
|
}
|
|
|
|
})
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
#[derive(Props)]
|
|
|
|
pub struct TodoEntryProps<'a> {
|
|
|
|
todos: UseState<'a, im_rc::HashMap<u32, TodoItem>>,
|
|
|
|
id: u32,
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 05:42:17 +00:00
|
|
|
pub fn todo_entry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
|
|
|
|
let todo = &cx.props.todos[&cx.props.id];
|
2021-12-25 22:18:05 +00:00
|
|
|
let is_editing = use_state(&cx, || false);
|
2022-01-03 05:42:17 +00:00
|
|
|
let completed = if todo.checked { "completed" } else { "" };
|
2022-01-10 13:46:23 +00:00
|
|
|
let editing = if *is_editing.get() { "editing" } else { "" };
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2022-01-10 13:46:23 +00:00
|
|
|
rsx!(cx, li {
|
|
|
|
class: "{completed} {editing}",
|
|
|
|
onclick: move |_| is_editing.set(true),
|
|
|
|
onfocusout: move |_| is_editing.set(false),
|
2022-01-03 05:42:17 +00:00
|
|
|
div { class: "view",
|
|
|
|
input { class: "toggle", r#type: "checkbox", id: "cbg-{todo.id}", checked: "{todo.checked}",
|
|
|
|
onchange: move |evt| {
|
|
|
|
cx.props.todos.modify()[&cx.props.id].checked = evt.value.parse().unwrap();
|
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
label { r#for: "cbg-{todo.id}", pointer_events: "none", "{todo.contents}" }
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2022-01-10 13:46:23 +00:00
|
|
|
is_editing.then(|| rsx!{
|
|
|
|
input {
|
|
|
|
class: "edit",
|
|
|
|
value: "{todo.contents}",
|
|
|
|
oninput: move |evt| cx.props.todos.modify()[&cx.props.id].contents = evt.value.clone(),
|
|
|
|
autofocus: "true",
|
|
|
|
onkeydown: move |evt| {
|
|
|
|
match evt.key.as_str() {
|
|
|
|
"Enter" | "Escape" | "Tab" => is_editing.set(false),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2021-09-24 06:10:54 +00:00
|
|
|
})
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|