refactoring some files.

This commit is contained in:
killian-larcher
2024-12-16 20:01:30 +01:00
parent 31ee81c10f
commit 4f12a2b078
26 changed files with 160 additions and 70 deletions
@@ -7,6 +7,8 @@ import {Button} from "@/components/ui/button";
import {Form, FormControl, FormField, FormItem, FormLabel, FormMessage, useZodForm} from "@/components/ui/form";
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";
export type createOrganizationModalProps = {
children: any
@@ -17,12 +19,16 @@ export function CreateOrganizationModal(props: createOrganizationModalProps) {
const {children} = props;
const router = useRouter()
const form = useZodForm({
schema: OrganizationSchema,
});
const mutation = useMutation({
mutationFn: async (values: OrganizationSchema) => {
const result = await createOrganizationAction(values)
router.refresh()
}
})
@@ -61,6 +67,20 @@ export function CreateOrganizationModal(props: createOrganizationModalProps) {
</FormItem>
)}
/>
<FormField
control={form.control}
name="slug"
defaultValue=""
render={({field}) => (
<FormItem>
<FormLabel>Slug</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage/>
</FormItem>
)}
/>
</Form>
</div>
@@ -4,7 +4,7 @@ import {useEffect, useState} from "react";
import {ComboBox} from "@/components/wrappers/common/combobox";
import {Organization} from "@prisma/client";
import {getCurrentOrganizationSlug, setCurrentOrganizationSlug} from "@/features/dashboard/organization-cookie";
import {getCurrentOrganizationId, setCurrentOrganizationId} from "@/features/dashboard/organization-cookie";
import {useSidebar} from "@/components/ui/sidebar";
export type organizationComboBoxProps = {
@@ -17,42 +17,42 @@ export function OrganizationCombobox(props: organizationComboBoxProps) {
// const {organizationId, moveToAnotherOrganization} = useStore((state) => state);
const [organizationSlug, setOrganizationSlug] = useState<string>()
const [organizationId, setOrganizationId] = useState<string>()
const {organizations, defaultOrganization} = props
useEffect(() => {
getCurrentOrganizationSlug().then(slug => {
getCurrentOrganizationId().then(id => {
if (slug == "") {
setOrganizationSlug(defaultOrganization.slug)
setCurrentOrganizationSlug(defaultOrganization.slug)
if (id == "") {
setOrganizationId(defaultOrganization.id)
setCurrentOrganizationId(defaultOrganization.id)
} else {
setOrganizationSlug(slug)
const organization = organizations.find(organization => organization.slug === slug)
setOrganizationId(id)
const organization = organizations.find(organization => organization.id === id)
if (!organization) {
setOrganizationSlug(defaultOrganization.slug)
setCurrentOrganizationSlug(defaultOrganization.slug)
setOrganizationId(defaultOrganization.id)
setCurrentOrganizationId(defaultOrganization.id)
}
}
})
}, [organizationSlug])
}, [organizationId])
const values = organizations.map(organization => {
return ({
value: organization.slug,
value: organization.id,
label: organization.name,
})
})
const onValueChange = (slug: string) => {
if (organizationSlug !== slug) {
setOrganizationSlug(slug)
setCurrentOrganizationSlug(slug)
const onValueChange = (id: string) => {
if (organizationId !== id) {
setOrganizationId(id)
setCurrentOrganizationId(id)
}
}
const { state, isMobile } = useSidebar();
const {state, isMobile} = useSidebar();
return (
@@ -61,7 +61,7 @@ export function OrganizationCombobox(props: organizationComboBoxProps) {
<ComboBox
sideBar
values={values}
defaultValue={organizationSlug}
defaultValue={organizationId}
onValueChange={onValueChange}/>
)}
@@ -0,0 +1,66 @@
"use server"
import {ActionError, userAction} from "@/safe-actions";
import {prisma} from "@/prisma";
import {OrganizationSchema} from "@/components/wrappers/dashboard/organization/organization.schema";
import {ServerActionResult} from "@/types/action-type";
import {Organization} from "@prisma/client";
const verifySlugUniqueness = async (slug: string) => {
const slugExists = await prisma.organization.count({
where: {
slug: slug
},
})
if (slugExists) {
throw new ActionError("Slug already exists.");
}
}
export const createOrganizationAction = userAction
.schema(OrganizationSchema)
.action(async ({parsedInput, ctx}): Promise<ServerActionResult<Organization>> => {
try {
await verifySlugUniqueness(parsedInput.slug)
const organization = await prisma.organization.create({
data: {
...parsedInput
}
})
await prisma.userOrganization.create({
data: {
userId: ctx.user.id,
organizationId: organization.id
},
});
return {
success: true,
value: organization,
actionSuccess: {
message: "Organization has been successfully created.",
messageParams: {organizationId: organization.id},
},
};
} catch (error) {
console.error("Error creating organization:", error);
return {
success: false,
actionError: {
message: "Failed to create organization.",
status: 500,
cause: error.message ?? "Unknown error",
messageParams: {message: "Error creating the organization"},
},
};
}
});
@@ -3,6 +3,7 @@ import {z} from "zod";
export const OrganizationSchema = z.object({
name: z.string(),
slug: z.string(),
});
export type OrganizationSchema = z.infer<typeof OrganizationSchema>;