feat: ability to delete a task (#39)

This commit is contained in:
Matouš Volf 2024-09-08 10:07:42 +02:00 committed by GitHub
commit 63ef7c817e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 60 additions and 8 deletions

View File

@ -1,5 +1,4 @@
use crate::models::category::Category; use crate::models::category::Category;
use chrono::NaiveDate;
use dioxus::core_macro::rsx; use dioxus::core_macro::rsx;
use dioxus::dioxus_core::Element; use dioxus::dioxus_core::Element;
use dioxus::prelude::*; use dioxus::prelude::*;

View File

@ -6,7 +6,7 @@ use crate::models::task::Task;
use crate::query::{QueryErrors, QueryKey, QueryValue}; use crate::query::{QueryErrors, QueryKey, QueryValue};
use crate::route::Route; use crate::route::Route;
use crate::server::projects::get_projects; use crate::server::projects::get_projects;
use crate::server::tasks::{create_task, edit_task}; use crate::server::tasks::{create_task, delete_task, edit_task};
use chrono::{Duration}; use chrono::{Duration};
use dioxus::core_macro::{component, rsx}; use dioxus::core_macro::{component, rsx};
use dioxus::dioxus_core::Element; use dioxus::dioxus_core::Element;
@ -336,7 +336,39 @@ pub(crate) fn TaskForm(task: Option<Task>, on_successful_submit: EventHandler<()
_ => None _ => None
}, },
div { div {
class: "flex flex-row justify-end mt-auto", class: "flex flex-row justify-between mt-auto",
button {
r#type: "button",
class: "py-2 px-4 bg-zinc-300/50 rounded-lg",
onclick: move |_| {
let task = task.clone();
async move {
if let Some(task) = task {
if *(task.category()) == Category::Trash {
let _ = delete_task(task.id()).await;
} else {
let new_task = NewTask::new(
task.title().to_owned(),
task.deadline(),
Category::Trash,
task.project_id()
);
let _ = edit_task(task.id(), new_task).await;
}
query_client.invalidate_queries(&[
QueryKey::TasksInCategory(task.category().clone()),
QueryKey::Tasks,
]);
}
on_successful_submit.call(());
}
},
i {
class: "fa-solid fa-trash-can"
}
}
button { button {
r#type: "submit", r#type: "submit",
class: "py-2 px-4 bg-zinc-300/50 rounded-lg", class: "py-2 px-4 bg-zinc-300/50 rounded-lg",

View File

@ -63,8 +63,10 @@ pub struct NewTask {
impl NewTask { impl NewTask {
pub fn new( pub fn new(
title: String, deadline: Option<chrono::NaiveDate>, title: String,
category: Category, project_id: Option<i32>, deadline: Option<chrono::NaiveDate>,
category: Category,
project_id: Option<i32>,
) -> Self { ) -> Self {
Self { title, deadline, category, project_id } Self { title, deadline, category, project_id }
} }

View File

@ -72,6 +72,8 @@ pub(crate) async fn edit_project(project_id: i32, new_project: NewProject)
Ok(updated_project) Ok(updated_project)
} }
// TODO: Get rid of this suppression.
//noinspection DuplicatedCode
#[server] #[server]
pub(crate) async fn delete_project(project_id: i32) pub(crate) async fn delete_project(project_id: i32)
-> Result<(), ServerFnError<ErrorVec<Error>>> { -> Result<(), ServerFnError<ErrorVec<Error>>> {
@ -80,6 +82,7 @@ pub(crate) async fn delete_project(project_id: i32)
let mut connection = establish_database_connection() let mut connection = establish_database_connection()
.map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?; .map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?;
diesel::delete(projects.filter(id.eq(project_id))).execute(&mut connection) diesel::delete(projects.filter(id.eq(project_id))).execute(&mut connection)
.map_err::<ErrorVec<Error>, _>(|error| vec![error.into()].into())?; .map_err::<ErrorVec<Error>, _>(|error| vec![error.into()].into())?;

View File

@ -136,3 +136,19 @@ pub(crate) async fn complete_task(task_id: i32) -> Result<Task, ServerFnError<Er
Ok(updated_task) Ok(updated_task)
} }
// TODO: Get rid of this suppression.
//noinspection DuplicatedCode
#[server]
pub(crate) async fn delete_task(task_id: i32)
-> Result<(), ServerFnError<ErrorVec<Error>>> {
use crate::schema::tasks::dsl::*;
let mut connection = establish_database_connection()
.map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?;
diesel::delete(tasks.filter(id.eq(task_id))).execute(&mut connection)
.map_err::<ErrorVec<Error>, _>(|error| vec![error.into()].into())?;
Ok(())
}