Project delete.

This commit is contained in:
charles-gauthereau
2024-12-17 19:42:02 +01:00
parent 0b644f7987
commit 43161b8523
7 changed files with 82 additions and 17 deletions
@@ -39,6 +39,7 @@ export default async function RoutePage(props: PageParams<{ projectId: string }>
</PageTitle> </PageTitle>
<PageActions className="justify-between"> <PageActions className="justify-between">
<ButtonDeleteProject <ButtonDeleteProject
projectId={projectId}
text={"Delete Project"} text={"Delete Project"}
/> />
</PageActions> </PageActions>
@@ -0,0 +1,8 @@
/*
Warnings:
- Added the required column `isArchived` to the `projects` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "projects" ADD COLUMN "isArchived" BOOLEAN NOT NULL;
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "projects" ALTER COLUMN "isArchived" SET DEFAULT false;
+5 -5
View File
@@ -102,11 +102,11 @@ model UserOrganization {
} }
model Project { model Project {
id String @id @default(cuid()) id String @id @default(cuid())
slug String @unique slug String @unique
name String name String
createdAt DateTime @default(now()) @map("created_at") createdAt DateTime @default(now()) @map("created_at")
isArchived Boolean @default(false)
organizationId String @map("organization_id") organizationId String @map("organization_id")
organization Organization @relation(fields: [organizationId], references: [id]) organization Organization @relation(fields: [organizationId], references: [id])
@@ -2,29 +2,36 @@
import {Trash2} from "lucide-react"; import {Trash2} from "lucide-react";
import {ButtonWithConfirm} from "@/components/wrappers/common/button/button-with-confirm"; import {ButtonWithConfirm} from "@/components/wrappers/common/button/button-with-confirm";
import {useMutation} from "@tanstack/react-query";
import {deleteProjectAction} from "@/components/wrappers/dashboard/projects/ButtonDeleteProject/delete-project.action";
import {useRouter} from "next/navigation";
import {toast} from "sonner";
export type ButtonDeleteProjectProps = { export type ButtonDeleteProjectProps = {
text? : string text? : string
projectId: string
} }
export const ButtonDeleteProject = (props: ButtonDeleteProjectProps) => { export const ButtonDeleteProject = (props: ButtonDeleteProjectProps) => {
const router = useRouter()
// const mutation = useMutation({ const mutation = useMutation({
// mutationFn: () => deleteUserAction(""), mutationFn: () => deleteProjectAction(props.projectId),
// onSuccess: async () => { onSuccess: async (result) => {
// await signOutAction(); router.push("/dashboard/projects")
// }, if(result.data.success) {
// }) toast.success(result.data.actionSuccess.message);
}
},
})
return ( return (
<ButtonWithConfirm <ButtonWithConfirm
text={props.text ? props.text : ""} text={props.text ? props.text : ""}
onClick={() => { onClick={() => {
// mutation.mutate() mutation.mutate()
console.log("ok")
}} }}
variant={"destructive"} variant={"destructive"}
// isPending={mutation.isPending} isPending={mutation.isPending}
className="gap-2" className="gap-2"
icon={<Trash2/>} icon={<Trash2/>}
/> />
@@ -0,0 +1,47 @@
"use server"
import {userAction} from "@/safe-actions";
import {z} from "zod";
import {v4 as uuidv4} from "uuid";
import {prisma} from "@/prisma";
import {ServerActionResult} from "@/types/action-type";
import {Projects} from "@prisma/client";
export const deleteProjectAction = userAction
.schema(z.string())
.action(async ({parsedInput, ctx}): Promise<ServerActionResult<Projects>> => {
try {
const uuid = uuidv4()
const project = await prisma.project.update({
where: {
id: parsedInput
},
data:{
isArchived: true,
slug: uuid
}
})
return {
success: true,
value: project,
actionSuccess: {
message: "Projects has been successfully archived.",
messageParams: {projectName: parsedInput.data.name},
},
};
} catch (error) {
return {
success: false,
actionError: {
message: "Failed to archived Projects.",
status: 500, // Optional: Use a meaningful status code
cause: error instanceof Error ? error.message : "Unknown error",
messageParams: {projectName: parsedInput.data.name},
},
};
}
});
@@ -42,7 +42,7 @@ export const createProjectAction = userAction
success: true, success: true,
value: project, value: project,
actionSuccess: { actionSuccess: {
message: "ProjectsForm has been successfully created.", message: "Projects has been successfully created.",
messageParams: {projectName: parsedInput.data.name}, messageParams: {projectName: parsedInput.data.name},
}, },
}; };
@@ -50,7 +50,7 @@ export const createProjectAction = userAction
return { return {
success: false, success: false,
actionError: { actionError: {
message: "Failed to create ProjectsForm.", message: "Failed to create Projects.",
status: 500, // Optional: Use a meaningful status code status: 500, // Optional: Use a meaningful status code
cause: error instanceof Error ? error.message : "Unknown error", cause: error instanceof Error ? error.message : "Unknown error",
messageParams: {projectName: parsedInput.data.name}, messageParams: {projectName: parsedInput.data.name},