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;
|
2022-01-03 05:42:17 +00:00
|
|
|
|
2021-10-11 02:27:08 +00:00
|
|
|
fn main() {
|
2022-07-09 19:15:20 +00:00
|
|
|
dioxus_desktop::launch(app);
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 01:03:33 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
2021-07-16 20:11:25 +00:00
|
|
|
pub enum FilterState {
|
|
|
|
All,
|
|
|
|
Active,
|
|
|
|
Completed,
|
|
|
|
}
|
|
|
|
|
2022-09-13 23:22:27 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, 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 {
|
2022-12-06 00:47:04 +00:00
|
|
|
let todos = use_state(cx, im_rc::HashMap::<u32, TodoItem>::default);
|
|
|
|
let filter = use_state(cx, || FilterState::All);
|
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-03-01 07:50:03 +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
|
|
|
|
2023-04-03 01:03:33 +00:00
|
|
|
let active_todo_count = todos.values().filter(|item| !item.checked).count();
|
|
|
|
let active_todo_text = match active_todo_count {
|
2021-07-16 20:11:25 +00:00
|
|
|
1 => "item",
|
|
|
|
_ => "items",
|
|
|
|
};
|
|
|
|
|
2023-04-03 01:03:33 +00:00
|
|
|
let show_clear_completed = todos.values().any(|todo| todo.checked);
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
2022-01-03 05:42:17 +00:00
|
|
|
section { class: "todoapp",
|
2022-09-13 21:57:23 +00:00
|
|
|
style { include_str!("./assets/todomvc.css") }
|
2024-01-03 20:02:08 +00:00
|
|
|
TodoHeader { todos: todos }
|
|
|
|
section { class: "main",
|
2023-04-03 01:03:33 +00:00
|
|
|
if !todos.is_empty() {
|
|
|
|
rsx! {
|
|
|
|
input {
|
|
|
|
id: "toggle-all",
|
|
|
|
class: "toggle-all",
|
|
|
|
r#type: "checkbox",
|
|
|
|
onchange: move |_| {
|
|
|
|
let check = active_todo_count != 0;
|
|
|
|
for (_, item) in todos.make_mut().iter_mut() {
|
|
|
|
item.checked = check;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
checked: if active_todo_count == 0 { "true" } else { "false" },
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
2023-04-03 01:03:33 +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",
|
2023-04-03 01:03:33 +00:00
|
|
|
filtered_todos.iter().map(|id| rsx!(TodoEntry {
|
|
|
|
key: "{id}",
|
|
|
|
id: *id,
|
|
|
|
todos: todos,
|
|
|
|
}))
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
2022-01-02 23:35:38 +00:00
|
|
|
(!todos.is_empty()).then(|| rsx!(
|
2023-10-19 14:26:17 +00:00
|
|
|
ListFooter {
|
|
|
|
active_todo_count: active_todo_count,
|
|
|
|
active_todo_text: active_todo_text,
|
|
|
|
show_clear_completed: show_clear_completed,
|
|
|
|
todos: todos,
|
|
|
|
filter: filter,
|
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 {}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
pub struct TodoHeaderProps<'a> {
|
|
|
|
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn TodoHeader<'a>(cx: Scope<'a, TodoHeaderProps<'a>>) -> Element {
|
|
|
|
let draft = use_state(cx, || "".to_string());
|
|
|
|
let todo_id = use_state(cx, || 0);
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
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",
|
|
|
|
oninput: move |evt| {
|
2024-01-05 01:02:00 +00:00
|
|
|
draft.set(evt.value().clone());
|
2024-01-03 20:02:08 +00:00
|
|
|
},
|
|
|
|
onkeydown: move |evt| {
|
|
|
|
if evt.key() == Key::Enter && !draft.is_empty() {
|
|
|
|
cx.props
|
|
|
|
.todos
|
|
|
|
.make_mut()
|
|
|
|
.insert(
|
|
|
|
**todo_id,
|
|
|
|
TodoItem {
|
|
|
|
id: **todo_id,
|
|
|
|
checked: false,
|
|
|
|
contents: draft.to_string(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
*todo_id.make_mut() += 1;
|
|
|
|
draft.set("".to_string());
|
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
|
|
|
})
|
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> {
|
2022-03-01 07:50:03 +00:00
|
|
|
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
2022-01-03 05:42:17 +00:00
|
|
|
id: u32,
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-13 03:01:03 +00:00
|
|
|
pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element {
|
2022-12-06 00:47:04 +00:00
|
|
|
let is_editing = use_state(cx, || false);
|
2022-01-26 02:41:40 +00:00
|
|
|
|
2022-03-01 07:50:03 +00:00
|
|
|
let todos = cx.props.todos.get();
|
2022-01-26 02:41:40 +00:00
|
|
|
let todo = &todos[&cx.props.id];
|
2022-01-03 05:42:17 +00:00
|
|
|
let completed = if todo.checked { "completed" } else { "" };
|
2022-03-01 07:50:03 +00:00
|
|
|
let editing = if **is_editing { "editing" } else { "" };
|
2021-07-16 20:11:25 +00:00
|
|
|
|
2022-03-05 20:25:09 +00:00
|
|
|
cx.render(rsx!{
|
2024-01-03 20:02:08 +00:00
|
|
|
li { class: "{completed} {editing}",
|
2022-03-05 20:25:09 +00:00
|
|
|
div { class: "view",
|
|
|
|
input {
|
|
|
|
class: "toggle",
|
|
|
|
r#type: "checkbox",
|
|
|
|
id: "cbg-{todo.id}",
|
|
|
|
checked: "{todo.checked}",
|
|
|
|
oninput: move |evt| {
|
2023-09-01 20:38:55 +00:00
|
|
|
cx.props.todos.make_mut()[&cx.props.id].checked = evt.value().parse().unwrap();
|
2022-03-05 20:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
label {
|
|
|
|
r#for: "cbg-{todo.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",
|
|
|
|
"{todo.contents}"
|
2022-01-03 05:42:17 +00:00
|
|
|
}
|
2023-04-03 01:03:33 +00:00
|
|
|
button {
|
|
|
|
class: "destroy",
|
2024-01-03 20:02:08 +00:00
|
|
|
onclick: move |_| {
|
|
|
|
cx.props.todos.make_mut().remove(&todo.id);
|
|
|
|
},
|
|
|
|
prevent_default: "onclick"
|
2023-04-03 01:03:33 +00:00
|
|
|
}
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
2022-03-05 20:25:09 +00:00
|
|
|
is_editing.then(|| rsx!{
|
|
|
|
input {
|
|
|
|
class: "edit",
|
|
|
|
value: "{todo.contents}",
|
2023-09-01 20:38:55 +00:00
|
|
|
oninput: move |evt| cx.props.todos.make_mut()[&cx.props.id].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
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2022-01-02 23:35:38 +00:00
|
|
|
}
|
2021-09-24 06:10:54 +00:00
|
|
|
})
|
2021-07-16 20:11:25 +00:00
|
|
|
}
|
2023-10-19 14:26:17 +00:00
|
|
|
|
|
|
|
#[derive(Props)]
|
|
|
|
pub struct ListFooterProps<'a> {
|
|
|
|
todos: &'a UseState<im_rc::HashMap<u32, TodoItem>>,
|
|
|
|
active_todo_count: usize,
|
|
|
|
active_todo_text: &'a str,
|
|
|
|
show_clear_completed: bool,
|
|
|
|
filter: &'a UseState<FilterState>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ListFooter<'a>(cx: Scope<'a, ListFooterProps<'a>>) -> Element {
|
|
|
|
let active_todo_count = cx.props.active_todo_count;
|
|
|
|
let active_todo_text = cx.props.active_todo_text;
|
|
|
|
|
|
|
|
let selected = |state| {
|
|
|
|
if *cx.props.filter == state {
|
|
|
|
"selected"
|
|
|
|
} else {
|
|
|
|
"false"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
footer { class: "footer",
|
|
|
|
span { class: "todo-count",
|
2024-01-03 20:02:08 +00:00
|
|
|
strong { "{active_todo_count} " }
|
|
|
|
span { "{active_todo_text} 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 [
|
|
|
|
(FilterState::All, "All", "#/"),
|
|
|
|
(FilterState::Active, "Active", "#/active"),
|
|
|
|
(FilterState::Completed, "Completed", "#/completed"),
|
|
|
|
] {
|
2023-10-19 14:26:17 +00:00
|
|
|
li {
|
|
|
|
a {
|
|
|
|
href: url,
|
|
|
|
class: selected(state),
|
|
|
|
onclick: move |_| cx.props.filter.set(state),
|
|
|
|
prevent_default: "onclick",
|
|
|
|
state_text
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cx.props.show_clear_completed {
|
|
|
|
cx.render(rsx! {
|
|
|
|
button {
|
|
|
|
class: "clear-completed",
|
|
|
|
onclick: move |_| cx.props.todos.make_mut().retain(|_, todo| !todo.checked),
|
|
|
|
"Clear completed"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn PageFooter(cx: Scope) -> Element {
|
|
|
|
cx.render(rsx! {
|
|
|
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|