mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working on create organization and delete.
This commit is contained in:
+38
@@ -0,0 +1,38 @@
|
||||
"use client"
|
||||
import {ButtonWithConfirm} from "@/components/wrappers/common/button/button-with-confirm";
|
||||
import {
|
||||
deleteOrganizationAction,
|
||||
} from "@/components/wrappers/dashboard/organization/organization.action";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {setCurrentOrganizationId} from "@/features/dashboard/organization-cookie";
|
||||
import {useRouter} from "next/navigation";
|
||||
|
||||
export type DeleteOrganizationButtonProps = {
|
||||
organizationId: string;
|
||||
}
|
||||
|
||||
export const DeleteOrganizationButton = (props: DeleteOrganizationButtonProps) => {
|
||||
const router = useRouter();
|
||||
const mutation = useMutation({
|
||||
mutationFn: () => deleteOrganizationAction(props.organizationId),
|
||||
onSuccess: async (result) => {
|
||||
console.log(result);
|
||||
if(result.data.success) {
|
||||
await setCurrentOrganizationId("default")
|
||||
router.push("/dashboard")
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
|
||||
return(
|
||||
<ButtonWithConfirm
|
||||
onClick={() => {
|
||||
mutation.mutate()
|
||||
}}
|
||||
isPending={mutation.isPending}
|
||||
text="Delete Organization"
|
||||
variant="destructive"/>
|
||||
)
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import {Input} from "@/components/ui/input";
|
||||
import {OrganizationSchema} from "@/components/wrappers/dashboard/organization/organization.schema";
|
||||
import {createOrganizationAction} from "@/components/wrappers/dashboard/organization/organization.action";
|
||||
import {useRouter} from "next/navigation";
|
||||
import {useState} from "react";
|
||||
|
||||
export type createOrganizationModalProps = {
|
||||
children: any
|
||||
@@ -16,6 +17,7 @@ export type createOrganizationModalProps = {
|
||||
|
||||
|
||||
export function CreateOrganizationModal(props: createOrganizationModalProps) {
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
const {children} = props;
|
||||
|
||||
@@ -27,14 +29,16 @@ export function CreateOrganizationModal(props: createOrganizationModalProps) {
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: async (values: OrganizationSchema) => {
|
||||
console.log(values)
|
||||
const result = await createOrganizationAction(values)
|
||||
setOpen(false);
|
||||
router.refresh()
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return (
|
||||
<Dialog>
|
||||
<Dialog open={open} onOpenChange={setOpen}>
|
||||
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
@@ -50,6 +54,7 @@ export function CreateOrganizationModal(props: createOrganizationModalProps) {
|
||||
<Form form={form}
|
||||
className="flex flex-col gap-4"
|
||||
onSubmit={async (values) => {
|
||||
console.log(values)
|
||||
await mutation.mutateAsync(values);
|
||||
}}
|
||||
>
|
||||
@@ -81,14 +86,14 @@ export function CreateOrganizationModal(props: createOrganizationModalProps) {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<DialogFooter>
|
||||
<div className="flex items-center justify-between w-full">
|
||||
<Button type="submit">Create</Button>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
</Form>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<div className="flex items-center justify-between w-full">
|
||||
<Button type="submit">Create</Button>
|
||||
</div>
|
||||
</DialogFooter>
|
||||
|
||||
</DialogContent>
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@ import {prisma} from "@/prisma";
|
||||
import {OrganizationSchema} from "@/components/wrappers/dashboard/organization/organization.schema";
|
||||
import {ServerActionResult} from "@/types/action-type";
|
||||
import {Organization} from "@prisma/client";
|
||||
import {z} from "zod";
|
||||
import {v4 as uuidv4} from "uuid";
|
||||
import {db} from "@/db";
|
||||
|
||||
|
||||
const verifySlugUniqueness = async (slug: string) => {
|
||||
@@ -63,4 +66,57 @@ export const createOrganizationAction = userAction
|
||||
};
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
export const deleteOrganizationAction = userAction
|
||||
.schema(z.string())
|
||||
.action(async ({parsedInput, ctx}): Promise<ServerActionResult<Organization>> => {
|
||||
console.log(parsedInput);
|
||||
try {
|
||||
|
||||
const uuid = uuidv4()
|
||||
const organization = await db.organization.findFirst({
|
||||
where: {
|
||||
id: parsedInput,
|
||||
}
|
||||
})
|
||||
console.log(organization);
|
||||
const organizationUpdated = await db.organization.update({
|
||||
where: {
|
||||
id: parsedInput,
|
||||
},
|
||||
data: {
|
||||
name: `${organization.name}-${uuid}`,
|
||||
slug: `${organization.slug}-${uuid}`,
|
||||
deleted : true
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
return {
|
||||
success: true,
|
||||
value: organizationUpdated,
|
||||
actionSuccess: {
|
||||
message: "Organization has been successfully deleted.",
|
||||
messageParams: {organizationId: organizationUpdated.id},
|
||||
},
|
||||
};
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error deleting organization:", error);
|
||||
return {
|
||||
success: false,
|
||||
actionError: {
|
||||
message: "Failed to delete organization.",
|
||||
status: 500,
|
||||
cause: error.message ?? "Unknown error",
|
||||
messageParams: {message: "Error deleting the organization"},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
});
|
||||
@@ -2,8 +2,9 @@ import {z} from "zod";
|
||||
|
||||
|
||||
export const OrganizationSchema = z.object({
|
||||
name: z.string(),
|
||||
slug: z.string(),
|
||||
name: z.string().min(5).max(40),
|
||||
slug: z.string().regex(/^[a-zA-Z0-9_-]*$/).min(5).max(20),
|
||||
|
||||
});
|
||||
|
||||
export type OrganizationSchema = z.infer<typeof OrganizationSchema>;
|
||||
|
||||
Reference in New Issue
Block a user