2022-09-13 21:57:23 +00:00
|
|
|
#![allow(non_snake_case)]
|
2021-07-16 20:11:25 +00:00
|
|
|
use dioxus::prelude::*;
|
2022-05-12 12:00:43 +00:00
|
|
|
use dioxus_elements::input_data::keyboard_types::Key;
|
2024-01-15 19:54:17 +00:00
|
|
|
use std::collections::HashMap;
|
2022-01-03 05:42:17 +00:00
|
|
|
|
2021-10-11 02:27:08 +00:00
|
|
|
fn main() {
|
2024-01-20 08:11:55 +00:00
|
|
|
launch(app);
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 01:03:33 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
2024-01-20 08:11:55 +00:00
|
|
|
enum FilterState {
|
2021-07-16 20:11:25 +00:00
|
|
|
All,
|
|
|
|
Active,
|
|
|
|
Completed,
|
|
|
|
}
|
|
|
|
|
2024-01-20 08:11:55 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
struct TodoItem {
|
|
|
|
id: u32,
|
|
|
|
checked: bool,
|
|
|
|
contents: String,
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 08:11:55 +00:00
|
|
|
const STYLE: &str = include_str!("./assets/todomvc.css");
|
|
|
|
|
|
|
|
fn app() -> Element {
|
2024-01-16 07:24:59 +00:00
|
|
|
let mut todos = use_signal(|| HashMap::<u32, TodoItem>::new());
|
2024-01-31 01:59:57 +00:00
|
|
|
let filter = use_signal(|| FilterState::All);
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2024-01-15 19:54:17 +00:00
|
|
|
let active_todo_count =
|
2024-01-21 07:32:12 +00:00
|
|
|
use_memo(move || todos.read().values().filter(|item| !item.checked).count());
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2024-01-21 07:32:12 +00:00
|
|
|
let filtered_todos = use_memo(move || {
|
2024-01-16 05:12:44 +00:00
|
|
|
let mut filtered_todos = todos
|
|
|
|
.read()
|
2024-01-15 19:54:17 +00:00
|
|
|
.iter()
|
2024-01-16 05:12:44 +00:00
|
|
|
.filter(|(_, item)| match filter() {
|
2024-01-15 19:54:17 +00:00
|
|
|
FilterState::All => true,
|
|
|
|
FilterState::Active => !item.checked,
|
|
|
|
FilterState::Completed => item.checked,
|
|
|
|
})
|
|
|
|
.map(|f| *f.0)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
filtered_todos.sort_unstable();
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2024-01-15 19:54:17 +00:00
|
|
|
filtered_todos
|
|
|
|
});
|
2023-04-03 01:03:33 +00:00
|
|
|
|
2024-01-31 01:59:57 +00:00
|
|
|
let toggle_all = move |_| {
|
2024-01-20 08:11:55 +00:00
|
|
|
let check = active_todo_count() != 0;
|
|
|
|
for (_, item) in todos.write().iter_mut() {
|
|
|
|
item.checked = check;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2022-01-03 05:42:17 +00:00
|
|
|
section { class: "todoapp",
|
2024-01-20 08:11:55 +00:00
|
|
|
style { {STYLE} }
|
2024-01-15 19:54:17 +00:00
|
|
|
TodoHeader { todos }
|
2024-01-03 20:02:08 +00:00
|
|
|
section { class: "main",
|
2024-01-16 05:12:44 +00:00
|
|
|
if !todos.read().is_empty() {
|
2024-01-11 03:33:34 +00:00
|
|
|
input {
|
|
|
|
id: "toggle-all",
|
|
|
|
class: "toggle-all",
|
|
|
|
r#type: "checkbox",
|
2024-01-20 08:11:55 +00:00
|
|
|
onchange: toggle_all,
|
2024-01-16 07:24:59 +00:00
|
|
|
checked: active_todo_count() == 0,
|
2021-09-24 06:10:54 +00:00
|
|
|
}
|
2024-01-11 03:33:34 +00:00
|
|
|
label { r#for: "toggle-all" }
|
2021-09-24 06:10:54 +00:00
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
ul { class: "todo-list",
|
2024-01-20 08:11:55 +00:00
|
|
|
for id in filtered_todos() {
|
2024-01-15 19:54:17 +00:00
|
|
|
TodoEntry { key: "{id}", id, todos }
|
2024-01-11 03:33:34 +00:00
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
2024-01-16 05:12:44 +00:00
|
|
|
if !todos.read().is_empty() {
|
2024-01-15 19:54:17 +00:00
|
|
|
ListFooter { active_todo_count, todos, filter }
|
2024-01-11 03:33:34 +00:00
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
PageFooter {}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 19:54:17 +00:00
|
|
|
#[component]
|
2024-01-20 08:11:55 +00:00
|
|
|
fn TodoHeader(mut todos: Signal<HashMap<u32, TodoItem>>) -> Element {
|
2024-01-16 07:24:59 +00:00
|
|
|
let mut draft = use_signal(|| "".to_string());
|
2024-01-15 19:54:17 +00:00
|
|
|
let mut todo_id = use_signal(|| 0);
|
|
|
|
|
|
|
|
let onkeydown = move |evt: KeyboardEvent| {
|
2024-01-16 05:12:44 +00:00
|
|
|
if evt.key() == Key::Enter && !draft.read().is_empty() {
|
|
|
|
let id = todo_id();
|
2024-01-15 19:54:17 +00:00
|
|
|
let todo = TodoItem {
|
|
|
|
id,
|
|
|
|
checked: false,
|
|
|
|
contents: draft.to_string(),
|
|
|
|
};
|
|
|
|
todos.write().insert(id, todo);
|
|
|
|
todo_id += 1;
|
|
|
|
draft.set("".to_string());
|
|
|
|
}
|
|
|
|
};
|
2023-10-19 14:26:17 +00:00
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-10-19 14:26:17 +00:00
|
|
|
header { class: "header",
|
2024-01-03 20:02:08 +00:00
|
|
|
h1 { "todos" }
|
|
|
|
input {
|
|
|
|
class: "new-todo",
|
|
|
|
placeholder: "What needs to be done?",
|
|
|
|
value: "{draft}",
|
|
|
|
autofocus: "true",
|
2024-01-15 19:54:17 +00:00
|
|
|
oninput: move |evt| draft.set(evt.value().clone()),
|
|
|
|
onkeydown,
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2024-01-15 19:54:17 +00:00
|
|
|
#[component]
|
2024-01-20 08:11:55 +00:00
|
|
|
fn TodoEntry(mut todos: Signal<HashMap<u32, TodoItem>>, id: u32) -> Element {
|
2024-01-16 07:24:59 +00:00
|
|
|
let mut is_editing = use_signal(|| false);
|
2024-01-21 07:32:12 +00:00
|
|
|
let checked = use_memo(move || todos.read().get(&id).unwrap().checked);
|
|
|
|
let contents = use_memo(move || todos.read().get(&id).unwrap().contents.clone());
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-01-16 18:47:23 +00:00
|
|
|
li { class: if checked() { "completed" }, class: if is_editing() { "editing" },
|
2022-03-05 20:25:09 +00:00
|
|
|
div { class: "view",
|
|
|
|
input {
|
|
|
|
class: "toggle",
|
|
|
|
r#type: "checkbox",
|
2024-01-15 19:54:17 +00:00
|
|
|
id: "cbg-{id}",
|
|
|
|
checked: "{checked}",
|
|
|
|
oninput: move |evt| todos.write().get_mut(&id).unwrap().checked = evt.value().parse().unwrap(),
|
2022-03-05 20:25:09 +00:00
|
|
|
}
|
|
|
|
label {
|
2024-01-15 19:54:17 +00:00
|
|
|
r#for: "cbg-{id}",
|
2024-01-03 20:02:08 +00:00
|
|
|
ondoubleclick: move |_| is_editing.set(true),
|
2022-03-05 20:25:09 +00:00
|
|
|
prevent_default: "onclick",
|
2024-01-15 19:54:17 +00:00
|
|
|
"{contents}"
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
2023-04-03 01:03:33 +00:00
|
|
|
button {
|
|
|
|
class: "destroy",
|
2024-01-15 19:54:17 +00:00
|
|
|
onclick: move |_| { todos.write().remove(&id); },
|
2024-01-03 20:02:08 +00:00
|
|
|
prevent_default: "onclick"
|
2023-04-03 01:03:33 +00:00
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
2024-01-16 05:12:44 +00:00
|
|
|
if is_editing() {
|
2022-03-05 20:25:09 +00:00
|
|
|
input {
|
|
|
|
class: "edit",
|
2024-01-15 19:54:17 +00:00
|
|
|
value: "{contents}",
|
|
|
|
oninput: move |evt| todos.write().get_mut(&id).unwrap().contents = evt.value(),
|
2022-03-05 20:25:09 +00:00
|
|
|
autofocus: "true",
|
|
|
|
onfocusout: move |_| is_editing.set(false),
|
|
|
|
onkeydown: move |evt| {
|
2022-05-12 12:00:43 +00:00
|
|
|
match evt.key() {
|
|
|
|
Key::Enter | Key::Escape | Key::Tab => is_editing.set(false),
|
2022-03-05 20:25:09 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2024-01-15 19:54:17 +00:00
|
|
|
}
|
2022-03-05 20:25:09 +00:00
|
|
|
}
|
2024-01-11 03:33:34 +00:00
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
|
2024-01-15 19:54:17 +00:00
|
|
|
#[component]
|
2024-01-20 08:11:55 +00:00
|
|
|
fn ListFooter(
|
2024-01-16 07:57:10 +00:00
|
|
|
mut todos: Signal<HashMap<u32, TodoItem>>,
|
2024-01-15 19:54:17 +00:00
|
|
|
active_todo_count: ReadOnlySignal<usize>,
|
2024-01-16 07:57:10 +00:00
|
|
|
mut filter: Signal<FilterState>,
|
2024-01-15 19:54:17 +00:00
|
|
|
) -> Element {
|
2024-01-21 07:32:12 +00:00
|
|
|
let show_clear_completed = use_memo(move || todos.read().values().any(|todo| todo.checked));
|
2023-10-19 14:26:17 +00:00
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-10-19 14:26:17 +00:00
|
|
|
footer { class: "footer",
|
|
|
|
span { class: "todo-count",
|
2024-01-03 20:02:08 +00:00
|
|
|
strong { "{active_todo_count} " }
|
2024-01-15 19:54:17 +00:00
|
|
|
span {
|
2024-01-16 07:24:59 +00:00
|
|
|
match active_todo_count() {
|
2024-01-15 19:54:17 +00:00
|
|
|
1 => "item",
|
|
|
|
_ => "items",
|
|
|
|
}
|
|
|
|
" left"
|
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
|
|
|
ul { class: "filters",
|
2024-01-03 20:02:08 +00:00
|
|
|
for (state , state_text , url) in [
|
2024-01-11 03:33:34 +00:00
|
|
|
(FilterState::All, "All", "#/"),
|
|
|
|
(FilterState::Active, "Active", "#/active"),
|
|
|
|
(FilterState::Completed, "Completed", "#/completed"),
|
|
|
|
] {
|
2023-10-19 14:26:17 +00:00
|
|
|
li {
|
|
|
|
a {
|
|
|
|
href: url,
|
2024-01-16 05:12:44 +00:00
|
|
|
class: if filter() == state { "selected" },
|
2024-01-15 19:54:17 +00:00
|
|
|
onclick: move |_| filter.set(state),
|
2023-10-19 14:26:17 +00:00
|
|
|
prevent_default: "onclick",
|
2024-01-11 03:33:34 +00:00
|
|
|
{state_text}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 07:24:59 +00:00
|
|
|
if show_clear_completed() {
|
2024-01-11 03:33:34 +00:00
|
|
|
button {
|
|
|
|
class: "clear-completed",
|
2024-01-15 19:54:17 +00:00
|
|
|
onclick: move |_| todos.write().retain(|_, todo| !todo.checked),
|
2024-01-11 03:33:34 +00:00
|
|
|
"Clear completed"
|
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 08:11:55 +00:00
|
|
|
fn PageFooter() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-10-19 14:26:17 +00:00
|
|
|
footer { class: "info",
|
|
|
|
p { "Double-click to edit a todo" }
|
2024-01-03 20:02:08 +00:00
|
|
|
p {
|
|
|
|
"Created by "
|
|
|
|
a { href: "http://github.com/jkelleyrtp/", "jkelleyrtp" }
|
|
|
|
}
|
|
|
|
p {
|
|
|
|
"Part of "
|
|
|
|
a { href: "http://todomvc.com", "TodoMVC" }
|
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|