2021-04-01 04:01:42 +00:00
|
|
|
use super::state::{FilterState, TodoItem, FILTER, TODOS};
|
2021-04-02 01:44:18 +00:00
|
|
|
use crate::filtertoggles;
|
2021-04-01 04:01:42 +00:00
|
|
|
use crate::recoil::use_atom;
|
|
|
|
use crate::todoitem::TodoEntry;
|
|
|
|
use dioxus_core::prelude::*;
|
|
|
|
|
|
|
|
pub fn TodoList(ctx: Context, props: &()) -> DomTree {
|
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);
|
|
|
|
|
|
|
|
let list = todos
|
|
|
|
.iter()
|
2021-04-02 01:44:18 +00:00
|
|
|
.filter(|item| match filter {
|
2021-04-01 04:01:42 +00:00
|
|
|
FilterState::All => true,
|
2021-04-02 01:44:18 +00:00
|
|
|
FilterState::Active => !item.checked,
|
|
|
|
FilterState::Completed => item.checked,
|
2021-04-01 04:01:42 +00:00
|
|
|
})
|
|
|
|
.map(|item| {
|
|
|
|
rsx!(TodoEntry {
|
|
|
|
key: "{order}",
|
|
|
|
id: item.id,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{list}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|