feat: ability to delete a task #39

Merged
matous-volf merged 4 commits from feat/task-delete into main 2024-09-08 08:07:42 +00:00
5 changed files with 60 additions and 8 deletions

View File

@ -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::*;

View File

@ -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",

View File

@ -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 }
}

View File

@ -72,6 +72,8 @@ 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>>> {
@ -80,6 +82,7 @@ pub(crate) async fn delete_project(project_id: i32)
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())?;

View File

@ -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.
coderabbitai[bot] commented 2024-09-08 07:59:56 +00:00 (Migrated from github.com)
Review

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?

**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? <!-- This is an auto-generated comment by CodeRabbit -->
//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(())
}
coderabbitai[bot] commented 2024-09-08 07:59:56 +00:00 (Migrated from github.com)
Review

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.

**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 -->