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>;
|
||||
|
||||
@@ -21,11 +21,6 @@ export const SidebarMenuCustom = (props: SidebarMenuCustomProps) => {
|
||||
url: "projects",
|
||||
icon: Layers,
|
||||
},
|
||||
{
|
||||
title: "Agents",
|
||||
url: "agents",
|
||||
icon: ShieldHalf,
|
||||
},
|
||||
{
|
||||
title: "Statistics",
|
||||
url: "statistics",
|
||||
@@ -44,9 +39,12 @@ export const SidebarMenuCustom = (props: SidebarMenuCustomProps) => {
|
||||
if (currentItem) {
|
||||
setActiveItem(currentItem.title);
|
||||
}
|
||||
else{
|
||||
setActiveItem("");
|
||||
}
|
||||
}, [pathname]);
|
||||
|
||||
const [activeItem, setActiveItem] = useState(items[0].title);
|
||||
const [activeItem, setActiveItem] = useState("");
|
||||
const handleItemClick = (title: string) => {
|
||||
setActiveItem(title);
|
||||
console.log(title)
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
"use client"
|
||||
|
||||
import {useEffect, useState} from "react";
|
||||
import Link from "next/link";
|
||||
import {SidebarMenu, SidebarMenuAction, SidebarMenuButton, SidebarMenuItem} from "@/components/ui/sidebar";
|
||||
import {cn} from "@/lib/utils";
|
||||
import {buttonVariants} from "@/components/ui/button";
|
||||
import {ChartArea, Layers, Settings, ShieldHalf} from "lucide-react";
|
||||
import {usePathname} from "next/navigation";
|
||||
|
||||
export type SidebarMenuAdminProps = {}
|
||||
|
||||
export const SidebarMenuAdmin = (props: SidebarMenuAdminProps) => {
|
||||
|
||||
const BASE_URL = "/dashboard";
|
||||
const pathname = usePathname();
|
||||
// Menu items.
|
||||
const items = [
|
||||
{
|
||||
title: "Agents",
|
||||
url: "agents",
|
||||
icon: ShieldHalf,
|
||||
},
|
||||
{
|
||||
title: "Administration panel",
|
||||
url: "admin",
|
||||
icon: Settings,
|
||||
},
|
||||
]
|
||||
|
||||
useEffect(() => {
|
||||
const currentUrl = pathname;
|
||||
const currentItem = items.find((item) => `${BASE_URL}/${item.url}` === currentUrl);
|
||||
if (currentItem) {
|
||||
setActiveItem(currentItem.title);
|
||||
}else{
|
||||
setActiveItem("");
|
||||
}
|
||||
}, [pathname]);
|
||||
|
||||
const [activeItem, setActiveItem] = useState("");
|
||||
const handleItemClick = (title: string) => {
|
||||
setActiveItem(title);
|
||||
console.log(title)
|
||||
}
|
||||
return (
|
||||
<SidebarMenu>
|
||||
{items.map((item) => (
|
||||
<SidebarMenuItem key={item.title}>
|
||||
<SidebarMenuButton asChild>
|
||||
<Link
|
||||
className={cn(buttonVariants({
|
||||
size: "lg",
|
||||
variant: activeItem === item.title ? "secondary" : 'ghost'
|
||||
}), "justify-start p-0")}
|
||||
href={`${BASE_URL}/${item.url}`}
|
||||
onClick={() => handleItemClick(item.title)}
|
||||
>
|
||||
<item.icon/>
|
||||
<span>{item.title}</span>
|
||||
</Link>
|
||||
</SidebarMenuButton>
|
||||
<SidebarMenuAction
|
||||
className={`peer-data-[active=true]/menu-button:opacity-100 ${activeItem === item.title ? 'active' : ''}`}/>
|
||||
</SidebarMenuItem>
|
||||
))}
|
||||
</SidebarMenu>
|
||||
)
|
||||
}
|
||||
@@ -13,11 +13,13 @@ import {requiredCurrentUser} from "@/auth/current-user";
|
||||
import {SideBarLogo} from "@/components/wrappers/dashboard/sideBar/SideBarLogo/SideBarLogo";
|
||||
import {SideBarFooterCredit} from "@/components/wrappers/dashboard/sideBar/SideBarFooterCredit/SideBarFooterCredit";
|
||||
import {LoggedInButton} from "@/components/wrappers/dashboard/loggedInButton/LoggedInButton";
|
||||
import {db} from "@/db";
|
||||
import {checkAllPermissions} from "@/features/permissions/permissions";
|
||||
import {SidebarMenuAdmin} from "@/components/wrappers/dashboard/sideBar/SideBarMenu/SideBarMenuAdmin";
|
||||
|
||||
export async function AppSidebar() {
|
||||
|
||||
const user = await requiredCurrentUser()
|
||||
|
||||
const organizations = await prisma.organization.findMany({
|
||||
where: {
|
||||
users: {
|
||||
@@ -25,6 +27,7 @@ export async function AppSidebar() {
|
||||
userId: user.id
|
||||
},
|
||||
},
|
||||
deleted: {not: true},
|
||||
},
|
||||
})
|
||||
const defaultOrganization = await prisma.organization.findUnique({
|
||||
@@ -33,6 +36,8 @@ export async function AppSidebar() {
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
return (
|
||||
<Sidebar collapsible="icon">
|
||||
<SidebarHeader>
|
||||
@@ -55,6 +60,14 @@ export async function AppSidebar() {
|
||||
<SidebarMenuCustom/>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
{user.role == "admin" ?
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel>Administration</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenuAdmin/>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
:null}
|
||||
</SidebarContent>
|
||||
<SidebarFooter>
|
||||
<SidebarMenu>
|
||||
|
||||
Reference in New Issue
Block a user