feat: ability to delete a task #39
@ -1,5 +1,4 @@
|
||||
use crate::models::category::Category;
|
||||
use chrono::NaiveDate;
|
||||
use dioxus::core_macro::rsx;
|
||||
use dioxus::dioxus_core::Element;
|
||||
use dioxus::prelude::*;
|
||||
|
@ -6,7 +6,7 @@ use crate::models::task::Task;
|
||||
use crate::query::{QueryErrors, QueryKey, QueryValue};
|
||||
use crate::route::Route;
|
||||
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 dioxus::core_macro::{component, rsx};
|
||||
use dioxus::dioxus_core::Element;
|
||||
@ -336,7 +336,39 @@ pub(crate) fn TaskForm(task: Option<Task>, on_successful_submit: EventHandler<()
|
||||
_ => None
|
||||
},
|
||||
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 {
|
||||
r#type: "submit",
|
||||
class: "py-2 px-4 bg-zinc-300/50 rounded-lg",
|
||||
|
@ -63,8 +63,10 @@ pub struct NewTask {
|
||||
|
||||
impl NewTask {
|
||||
pub fn new(
|
||||
title: String, deadline: Option<chrono::NaiveDate>,
|
||||
category: Category, project_id: Option<i32>,
|
||||
title: String,
|
||||
deadline: Option<chrono::NaiveDate>,
|
||||
category: Category,
|
||||
project_id: Option<i32>,
|
||||
) -> Self {
|
||||
Self { title, deadline, category, project_id }
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ pub(crate) async fn get_projects()
|
||||
|
||||
#[server]
|
||||
pub(crate) async fn edit_project(project_id: i32, new_project: NewProject)
|
||||
-> Result<Project, ServerFnError<ErrorVec<ProjectError>>> {
|
||||
-> Result<Project, ServerFnError<ErrorVec<ProjectError>>> {
|
||||
use crate::schema::projects::dsl::*;
|
||||
|
||||
new_project.validate()
|
||||
@ -72,14 +72,17 @@ pub(crate) async fn edit_project(project_id: i32, new_project: NewProject)
|
||||
Ok(updated_project)
|
||||
}
|
||||
|
||||
// TODO: Get rid of this suppression.
|
||||
//noinspection DuplicatedCode
|
||||
#[server]
|
||||
pub(crate) async fn delete_project(project_id: i32)
|
||||
-> Result<(), ServerFnError<ErrorVec<Error>>> {
|
||||
-> Result<(), ServerFnError<ErrorVec<Error>>> {
|
||||
use crate::schema::projects::dsl::*;
|
||||
|
||||
let mut connection = establish_database_connection()
|
||||
.map_err::<ErrorVec<Error>, _>(|_| vec![Error::ServerInternal].into())?;
|
||||
|
||||
|
||||
diesel::delete(projects.filter(id.eq(project_id))).execute(&mut connection)
|
||||
.map_err::<ErrorVec<Error>, _>(|error| vec![error.into()].into())?;
|
||||
|
||||
|
@ -136,3 +136,19 @@ pub(crate) async fn complete_task(task_id: i32) -> Result<Task, ServerFnError<Er
|
||||
|
||||
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(())
|
||||
}
|
||||
![]() Approve the implementation of The function **Approve the implementation of `delete_task`.**
The function `delete_task` is implemented correctly and uses standard practices for database operations and error handling in Rust. However, consider adding more specific error handling for different types of database errors to improve the robustness of the function.
<!-- This is an auto-generated comment by CodeRabbit -->
|
||||
|
Loading…
x
Reference in New Issue
Block a user
Acknowledge the TODO comment regarding code suppression.
The TODO comment about removing code suppression for duplicated code is noted. It's important to prioritize this task to improve code maintainability and reduce technical debt.
Would you like me to help identify areas of code duplication and suggest refactoring opportunities?