2021-04-05 01:47:53 +00:00
|
|
|
use crate::{
|
|
|
|
filtertoggles,
|
|
|
|
recoil::use_atom,
|
|
|
|
state::{FilterState, TodoItem, FILTER, TODOS},
|
|
|
|
todoitem::TodoEntry,
|
|
|
|
};
|
2021-04-01 04:01:42 +00:00
|
|
|
use dioxus_core::prelude::*;
|
|
|
|
|
2021-06-01 22:33:15 +00:00
|
|
|
pub fn TodoList(ctx: Context<()>) -> VNode {
|
2021-04-02 01:44:18 +00:00
|
|
|
let (draft, set_draft) = use_state(&ctx, || "".to_string());
|
|
|
|
let (todos, _) = use_state(&ctx, || Vec::<TodoItem>::new());
|
2021-04-01 04:01:42 +00:00
|
|
|
let filter = use_atom(&ctx, &FILTER);
|
|
|
|
|
|
|
|
ctx.render(rsx! {
|
|
|
|
div {
|
|
|
|
header {
|
|
|
|
class: "header"
|
|
|
|
h1 {"todos"}
|
|
|
|
input {
|
|
|
|
class: "new-todo"
|
|
|
|
placeholder: "What needs to be done?"
|
2021-04-02 01:44:18 +00:00
|
|
|
value: "{draft}"
|
|
|
|
oninput: move |evt| set_draft(evt.value)
|
2021-04-01 04:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 01:47:53 +00:00
|
|
|
{ // list
|
|
|
|
todos
|
|
|
|
.iter()
|
|
|
|
.filter(|item| match filter {
|
|
|
|
FilterState::All => true,
|
|
|
|
FilterState::Active => !item.checked,
|
|
|
|
FilterState::Completed => item.checked,
|
|
|
|
})
|
|
|
|
.map(|item| {
|
|
|
|
rsx!(TodoEntry {
|
|
|
|
key: "{order}",
|
|
|
|
id: item.id,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2021-04-01 04:01:42 +00:00
|
|
|
|
|
|
|
// filter toggle (show only if the list isn't empty)
|
|
|
|
{(!todos.is_empty()).then(||
|
2021-04-02 01:44:18 +00:00
|
|
|
rsx!( filtertoggles::FilterToggles {})
|
|
|
|
)}
|
2021-04-01 04:01:42 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|