feat: ability to complete a task #35

Merged
matous-volf merged 6 commits from feat/task-complete into main 2024-09-07 12:58:36 +00:00
Showing only changes of commit a12d558839 - Show all commits

View File

@ -3,70 +3,98 @@ use crate::models::task::Task;
use dioxus::core_macro::rsx;
use dioxus::dioxus_core::Element;
use dioxus::prelude::*;
use dioxus_query::prelude::use_query_client;
use crate::query::{QueryErrors, QueryKey, QueryValue};
use crate::server::tasks::complete_task;
#[component]
pub(crate) fn TaskList(tasks: Vec<Task>, class: Option<&'static str>) -> Element {
let query_client = use_query_client::<QueryValue, QueryErrors, QueryKey>();
let mut task_being_edited = use_context::<Signal<Option<Task>>>();
rsx! {
div {
class: format!("flex flex-col {}", class.unwrap_or("")),
for task in tasks {
div {
key: "{task.id()}",
class: format!(
"px-8 pt-5 {} flex flex-row gap-4 select-none {}",
if task.deadline().is_some() {
"pb-0.5"
} else if let Category::Calendar { time, .. } = task.category() {
if time.is_some() {
{tasks.iter().cloned().map(|task| {
let task_clone = task.clone();
rsx! {
div {
key: "{task.id()}",
class: format!(
"px-8 pt-5 {} flex flex-row gap-4 select-none {}",
if task.deadline().is_some() {
"pb-0.5"
} else if let Category::Calendar { time, .. } = task.category() {
if time.is_some() {
"pb-0.5"
} else {
"pb-5"
}
} else {
"pb-5"
}
} else {
"pb-5"
},
if task_being_edited().is_some_and(|t| t.id() == task.id()) {
"bg-zinc-700"
} else { "" }
),
onclick: move |_| task_being_edited.set(Some(task.clone())),
i {
class: "fa-regular fa-square text-3xl text-zinc-600",
},
div {
class: "flex flex-col",
div {
class: "mt-1 grow font-medium",
{task.title()}
},
div {
class: "flex flex-row gap-3",
if let Some(deadline) = task.deadline() {
div {
class: "text-sm text-zinc-400",
i {
class: "fa-solid fa-bomb"
},
{deadline.format(" %m. %d.").to_string()}
},
if task_being_edited().is_some_and(|t| t.id() == task.id()) {
"bg-zinc-700"
} else { "" }
),
onclick: move |_| task_being_edited.set(Some(task.clone())),
i {
class: format!(
"{} text-3xl text-zinc-500",
if *(task_clone.category()) == Category::Done {
"fa solid fa-square-check"
} else {
"fa-regular fa-square"
}
),
onclick: move |event| {
// To prevent editing the task.
event.stop_propagation();
let task = task_clone.clone();
async move {
let completed_task = complete_task(task.id()).await;
query_client.invalidate_queries(&[
QueryKey::Tasks,
QueryKey::TasksInCategory(
completed_task.unwrap().category().clone()
),
]);
}
}
if let Category::Calendar { time, .. } = task.category() {
if let Some(calendar_time) = time {
},
div {
class: "flex flex-col",
div {
class: "mt-1 grow font-medium",
{task.title()}
},
div {
coderabbitai[bot] commented 2024-09-07 12:55:28 +00:00 (Migrated from github.com)
Review

Refactoring of task rendering and event handling logic.

The refactoring of the task rendering logic using map and clone improves readability and immutability. The conditional rendering for task properties and CSS class determination is well-implemented and concise.

The asynchronous onclick event handler for task completion is a significant enhancement. It correctly uses async move to handle the task completion without blocking the UI, followed by invalidating queries to update the UI state. This is a robust implementation that leverages modern Rust asynchronous programming practices.

Consider adding error handling for the complete_task function within the async move block to manage potential failures gracefully.

**Refactoring of task rendering and event handling logic.** The refactoring of the task rendering logic using `map` and `clone` improves readability and immutability. The conditional rendering for task properties and CSS class determination is well-implemented and concise. The asynchronous `onclick` event handler for task completion is a significant enhancement. It correctly uses `async move` to handle the task completion without blocking the UI, followed by invalidating queries to update the UI state. This is a robust implementation that leverages modern Rust asynchronous programming practices. Consider adding error handling for the `complete_task` function within the `async move` block to manage potential failures gracefully. <!-- This is an auto-generated comment by CodeRabbit -->
class: "flex flex-row gap-3",
if let Some(deadline) = task.deadline() {
div {
class: "text-sm text-zinc-400",
i {
class: "fa-solid fa-clock"
class: "fa-solid fa-bomb"
},
{calendar_time.time().format(" %k:%M").to_string()}
{deadline.format(" %m. %d.").to_string()}
}
}
if let Category::Calendar { time, .. } = task.category() {
if let Some(calendar_time) = time {
div {
class: "text-sm text-zinc-400",
i {
class: "fa-solid fa-clock"
},
{calendar_time.time().format(" %k:%M").to_string()}
}
}
}
}
}
}
}
}
})}
}
}
}