Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat: delete a task option #122 (#216)

Matheus Araujo86bacb69cffc
diff --git a/apps/api/src/task/controllers/delete-task.ts b/apps/api/src/task/controllers/delete-task.ts
new file mode 100644
index 000000000..c56190e90
--- /dev/null
+++ b/apps/api/src/task/controllers/delete-task.ts
@@ -0,0 +1,22 @@
+import { eq } from "drizzle-orm";
+import { HTTPException } from "hono/http-exception";
+import db from "../../database";
+import { taskTable } from "../../database/schema";
+
+async function deleteTask(taskId: string) {
+ const task = await db
+ .delete(taskTable)
+ .where(eq(taskTable.id, taskId))
+ .returning()
+ .execute();
+
+ if (!task) {
+ throw new HTTPException(404, {
+ message: "Task not found",
+ });
+ }
+
+ return task;
+}
+
+export default deleteTask;
diff --git a/apps/api/src/task/index.ts b/apps/api/src/task/index.ts
index 74ec5cb8c..05440b7e2 100644
--- a/apps/api/src/task/index.ts
+++ b/apps/api/src/task/index.ts
@@ -2,6 +2,7 @@ import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";
import { z } from "zod";
import createTask from "./controllers/create-task";
+import deleteTask from "./controllers/delete-task";
import exportTasks from "./controllers/export-tasks";
import getTask from "./controllers/get-task";
import getTasks from "./controllers/get-tasks";
@@ -143,6 +144,16 @@ const task = new Hono<{
return c.json(result);
},
- );
+ )
+ .delete(
+ "/:id",
+ zValidator("param", z.object({ id: z.string() })),
+ async (c) => {
+ const { id } = c.req.valid("param");
+
+ const task = await deleteTask(id);
+ return c.json(task);
+ },
+ );
export default task;
diff --git a/apps/web/src/components/task/task-info.tsx b/apps/web/src/components/task/task-info.tsx
index ab6d61c8a..d9853e0fc 100644
--- a/apps/web/src/components/task/task-info.tsx
+++ b/apps/web/src/components/task/task-info.tsx
@@ -1,9 +1,13 @@
+import { Button } from "@/components/ui/button";
import { Form, FormField, FormItem, FormLabel } from "@/components/ui/form";
import { Select } from "@/components/ui/select";
+import useDeleteTask from "@/hooks/mutations/task/use-delete-task";
import useUpdateTask from "@/hooks/mutations/task/use-update-task";
import useGetActiveWorkspaceUsers from "@/hooks/queries/workspace-users/use-active-workspace-users";
import useProjectStore from "@/store/project";
import type Task from "@/types/task";
+import { useQueryClient } from "@tanstack/react-query";
+import { useNavigate } from "@tanstack/react-router";
import { Flag } from "lucide-react";
import { useEffect } from "react";
import { useForm } from "react-hook-form";
@@ -26,11 +30,14 @@ function TaskInfo({
task: Task;
setIsSaving: (isSaving: boolean) => void;
}) {
+ const navigate = useNavigate();
+ const queryClient = useQueryClient();
const { project } = useProjectStore();
const { data: workspaceUsers } = useGetActiveWorkspaceUsers({
workspaceId: project?.workspaceId ?? "",
});
const { mutateAsync: updateTask } = useUpdateTask();
+ const { mutateAsync: deleteTask, isPending: isDeleting } = useDeleteTask();
const form = useForm<z.infer<typeof taskInfoSchema>>({
defaultValues: {
@@ -64,6 +71,29 @@ function TaskInfo({
}
};
+ const handleDeleteTask = async () => {
+ if (!task) return;
+
+ try {
+ await deleteTask(task.id);
+ queryClient.invalidateQueries({
+ queryKey: ["tasks", project?.id ?? ""],
+ });
+ toast.success("Task deleted successfully");
+ navigate({
+ to: "/dashboard/workspace/$workspaceId/project/$projectId/board",
+ params: {
+ workspaceId: project?.workspaceId ?? "",
+ projectId: project?.id ?? "",
+ },
+ });
+ } catch (error) {
+ toast.error(
+ error instanceof Error ? error.message : "Failed to delete task",
+ );
+ }
+ };
+
useEffect(() => {
return () => {
form.reset();
@@ -181,6 +211,12 @@ function TaskInfo({
/>
<TaskLabels taskId={task.id} setIsSaving={setIsSaving} />
</Form>
+ <Button
+ onClick={handleDeleteTask}
+ className="bg-red-600 text-white hover:bg-red-500 dark:bg-red-500 dark:hover:bg-red-400"
+ >
+ {isDeleting ? "Deleting..." : "Delete Task"}
+ </Button>
</div>
);
}
diff --git a/apps/web/src/fetchers/task/delete-task.ts b/apps/web/src/fetchers/task/delete-task.ts
new file mode 100644
index 000000000..8b10db86d
--- /dev/null
+++ b/apps/web/src/fetchers/task/delete-task.ts
@@ -0,0 +1,16 @@
+import { client } from "@kaneo/libs";
+import type { InferRequestType } from "hono/client";
+
+export type DeleteTaskRequest = InferRequestType<
+ (typeof client)["task"][":id"]["$delete"]
+>["param"];
+
+async function deleteTask(taskId: string) {
+ const response = await client.task[":id"].$delete({ param: { id: taskId } });
+
+ const data = await response.json();
+
+ return data;
+}
+
+export default deleteTask;
diff --git a/apps/web/src/hooks/mutations/task/use-delete-task.ts b/apps/web/src/hooks/mutations/task/use-delete-task.ts
new file mode 100644
index 000000000..2b1634d5e
--- /dev/null
+++ b/apps/web/src/hooks/mutations/task/use-delete-task.ts
@@ -0,0 +1,10 @@
+import deleteTask from "@/fetchers/task/delete-task";
+import { useMutation } from "@tanstack/react-query";
+
+function useDeleteTask() {
+ return useMutation({
+ mutationFn: deleteTask,
+ });
+}
+
+export default useDeleteTask;