mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Getting organization by slug, adding this in url. And working on permissions.
This commit is contained in:
+4
-4
@@ -4,21 +4,21 @@ import {
|
||||
deleteOrganizationAction,
|
||||
} from "@/components/wrappers/dashboard/organization/organization.action";
|
||||
import {useMutation} from "@tanstack/react-query";
|
||||
import {setCurrentOrganizationId} from "@/features/dashboard/organization-cookie";
|
||||
import {setCurrentOrganizationSlug} from "@/features/dashboard/organization-cookie";
|
||||
import {useRouter} from "next/navigation";
|
||||
|
||||
export type DeleteOrganizationButtonProps = {
|
||||
organizationId: string;
|
||||
organizationSlug: string;
|
||||
}
|
||||
|
||||
export const DeleteOrganizationButton = (props: DeleteOrganizationButtonProps) => {
|
||||
const router = useRouter();
|
||||
const mutation = useMutation({
|
||||
mutationFn: () => deleteOrganizationAction(props.organizationId),
|
||||
mutationFn: () => deleteOrganizationAction(props.organizationSlug),
|
||||
onSuccess: async (result) => {
|
||||
console.log(result);
|
||||
if(result.data.success) {
|
||||
await setCurrentOrganizationId("default")
|
||||
await setCurrentOrganizationSlug("default")
|
||||
router.push("/dashboard")
|
||||
}
|
||||
},
|
||||
|
||||
@@ -80,7 +80,12 @@ export function CreateOrganizationModal(props: createOrganizationModalProps) {
|
||||
<FormItem>
|
||||
<FormLabel>Slug</FormLabel>
|
||||
<FormControl>
|
||||
<Input {...field} />
|
||||
<Input {...field}
|
||||
onChange={(e) => {
|
||||
const value = e.target.value.replaceAll(" ", "-").toLowerCase()
|
||||
field.onChange(value)
|
||||
}}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage/>
|
||||
</FormItem>
|
||||
|
||||
@@ -4,8 +4,12 @@ import {useEffect, useState} from "react";
|
||||
|
||||
import {ComboBox} from "@/components/wrappers/common/combobox";
|
||||
import {Organization} from "@prisma/client";
|
||||
import {getCurrentOrganizationId, setCurrentOrganizationId} from "@/features/dashboard/organization-cookie";
|
||||
import {
|
||||
getCurrentOrganizationSlug,
|
||||
setCurrentOrganizationSlug
|
||||
} from "@/features/dashboard/organization-cookie";
|
||||
import {useSidebar} from "@/components/ui/sidebar";
|
||||
import {useRouter} from "next/navigation";
|
||||
|
||||
export type organizationComboBoxProps = {
|
||||
organizations: Organization[]
|
||||
@@ -14,42 +18,43 @@ export type organizationComboBoxProps = {
|
||||
|
||||
|
||||
export function OrganizationCombobox(props: organizationComboBoxProps) {
|
||||
|
||||
const router = useRouter()
|
||||
// const {organizationId, moveToAnotherOrganization} = useStore((state) => state);
|
||||
|
||||
const [organizationId, setOrganizationId] = useState<string>()
|
||||
const [organizationSlug, setOrganizationSlug] = useState<string>()
|
||||
|
||||
const {organizations, defaultOrganization} = props
|
||||
|
||||
useEffect(() => {
|
||||
getCurrentOrganizationId().then(id => {
|
||||
getCurrentOrganizationSlug().then(slug => {
|
||||
|
||||
if (id == "") {
|
||||
setOrganizationId(defaultOrganization.id)
|
||||
setCurrentOrganizationId(defaultOrganization.id)
|
||||
if (slug == "") {
|
||||
setOrganizationSlug(defaultOrganization.id)
|
||||
setCurrentOrganizationSlug(defaultOrganization.slug)
|
||||
} else {
|
||||
setOrganizationId(id)
|
||||
const organization = organizations.find(organization => organization.id === id)
|
||||
setOrganizationSlug(slug)
|
||||
const organization = organizations.find(organization => organization.slug === slug)
|
||||
if (!organization) {
|
||||
setOrganizationId(defaultOrganization.id)
|
||||
setCurrentOrganizationId(defaultOrganization.id)
|
||||
setOrganizationSlug(defaultOrganization.id)
|
||||
setCurrentOrganizationSlug(defaultOrganization.slug)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
}, [organizationId])
|
||||
}, [organizationSlug])
|
||||
|
||||
const values = organizations.map(organization => {
|
||||
return ({
|
||||
value: organization.id,
|
||||
value: organization.slug,
|
||||
label: organization.name,
|
||||
})
|
||||
})
|
||||
|
||||
const onValueChange = (id: string) => {
|
||||
if (organizationId !== id) {
|
||||
setOrganizationId(id)
|
||||
setCurrentOrganizationId(id)
|
||||
const onValueChange = (slug: string) => {
|
||||
if (organizationSlug !== slug) {
|
||||
setOrganizationSlug(slug)
|
||||
setCurrentOrganizationSlug(slug)
|
||||
router.replace("/dashboard")
|
||||
}
|
||||
}
|
||||
const {state, isMobile} = useSidebar();
|
||||
@@ -61,7 +66,7 @@ export function OrganizationCombobox(props: organizationComboBoxProps) {
|
||||
<ComboBox
|
||||
sideBar
|
||||
values={values}
|
||||
defaultValue={organizationId}
|
||||
defaultValue={organizationSlug}
|
||||
onValueChange={onValueChange}/>
|
||||
)}
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ export const deleteOrganizationAction = userAction
|
||||
const uuid = uuidv4()
|
||||
const organization = await db.organization.findFirst({
|
||||
where: {
|
||||
id: parsedInput,
|
||||
slug: parsedInput,
|
||||
}
|
||||
})
|
||||
console.log(organization);
|
||||
|
||||
@@ -2,9 +2,11 @@ import {z} from "zod";
|
||||
|
||||
|
||||
export const OrganizationSchema = z.object({
|
||||
name: z.string().min(5).max(40),
|
||||
slug: z.string().regex(/^[a-zA-Z0-9_-]*$/).min(5).max(20),
|
||||
|
||||
name: z.string().min(5, 'Name must be at least 5 characters long').max(40, 'Name must be at most 40 characters long'),
|
||||
slug: z.string()
|
||||
.regex(/^[a-zA-Z0-9_-]*$/, 'Slug can only contain letters, numbers, underscores, and hyphens')
|
||||
.min(5, 'Slug must be at least 5 characters long')
|
||||
.max(20, 'Slug must be at most 20 characters long'),
|
||||
});
|
||||
|
||||
export type OrganizationSchema = z.infer<typeof OrganizationSchema>;
|
||||
|
||||
@@ -8,11 +8,13 @@ import {buttonVariants} from "@/components/ui/button";
|
||||
import {ChartArea, Layers, Settings, ShieldHalf} from "lucide-react";
|
||||
import {usePathname} from "next/navigation";
|
||||
|
||||
export type SidebarMenuCustomProps = {}
|
||||
export type SidebarMenuCustomProps = {
|
||||
currentOrganizationSlug: string
|
||||
}
|
||||
|
||||
export const SidebarMenuCustom = (props: SidebarMenuCustomProps) => {
|
||||
|
||||
const BASE_URL = "/dashboard";
|
||||
const BASE_URL = `/dashboard/${props.currentOrganizationSlug}`;
|
||||
const pathname = usePathname();
|
||||
// Menu items.
|
||||
const items = [
|
||||
|
||||
@@ -9,17 +9,17 @@ import {
|
||||
import {SidebarMenuCustom} from "@/components/wrappers/dashboard/sideBar/SideBarMenu/SideBarMenu";
|
||||
import {OrganizationCombobox} from "@/components/wrappers/dashboard/organization/organization-combobox";
|
||||
import {prisma} from "@/prisma";
|
||||
import {requiredCurrentUser} from "@/auth/current-user";
|
||||
import {currentUser, 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";
|
||||
import {getCurrentOrganizationSlug} from "@/features/dashboard/organization-cookie";
|
||||
|
||||
export async function AppSidebar() {
|
||||
|
||||
const user = await requiredCurrentUser()
|
||||
const currentOrganizationSlug = await getCurrentOrganizationSlug()
|
||||
const organizations = await prisma.organization.findMany({
|
||||
where: {
|
||||
users: {
|
||||
@@ -57,7 +57,7 @@ export async function AppSidebar() {
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel>Application</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenuCustom/>
|
||||
<SidebarMenuCustom currentOrganizationSlug={currentOrganizationSlug}/>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
{user.role == "admin" ?
|
||||
|
||||
@@ -5,10 +5,10 @@ import {cookies} from 'next/headers';
|
||||
|
||||
const COOKIE_NAME = 'PORTABASE_ORGANIZATION_SLUG';
|
||||
|
||||
export async function getCurrentOrganizationId() {
|
||||
export async function getCurrentOrganizationSlug() {
|
||||
return (await cookies()).get(COOKIE_NAME)?.value || "";
|
||||
}
|
||||
|
||||
export async function setCurrentOrganizationId(slug: string) {
|
||||
export async function setCurrentOrganizationSlug(slug: string) {
|
||||
return (await cookies()).set(COOKIE_NAME, slug).get(COOKIE_NAME)?.value;
|
||||
}
|
||||
@@ -22384,7 +22384,6 @@ paths:
|
||||
description: Create a new Organization
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"201":
|
||||
description: Successful operation
|
||||
@@ -22430,7 +22429,6 @@ paths:
|
||||
description: Create several Organization
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"201":
|
||||
description: Successful operation
|
||||
@@ -22476,7 +22474,6 @@ paths:
|
||||
description: Find one unique Organization
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -22532,7 +22529,6 @@ paths:
|
||||
description: Find the first Organization matching the given condition
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -22588,7 +22584,6 @@ paths:
|
||||
description: Find a list of Organization
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -22646,7 +22641,6 @@ paths:
|
||||
description: Update a Organization
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -22692,7 +22686,6 @@ paths:
|
||||
description: Update Organizations matching the given condition
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -22738,7 +22731,6 @@ paths:
|
||||
description: Upsert a Organization
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -22894,7 +22886,6 @@ paths:
|
||||
description: Find a list of Organization
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -22952,7 +22943,6 @@ paths:
|
||||
description: Aggregate Organizations
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
@@ -23008,7 +22998,6 @@ paths:
|
||||
description: Group Organizations by fields
|
||||
tags:
|
||||
- organization
|
||||
security: []
|
||||
responses:
|
||||
"200":
|
||||
description: Successful operation
|
||||
|
||||
Reference in New Issue
Block a user