diff --git a/apps/web/src/pages/[organization]/settings/organization.tsx b/apps/web/src/pages/[organization]/settings/organization.tsx index 7dd8f6cf..c4df4c6d 100644 --- a/apps/web/src/pages/[organization]/settings/organization.tsx +++ b/apps/web/src/pages/[organization]/settings/organization.tsx @@ -8,30 +8,42 @@ import { SettingsLayout } from "@/components/layouts/SettingsLayout"; import { toast } from "@/components/ui/use-toast"; import { createServerSideProps } from "@/server/getServerSideProps"; import { useOrganizationParams } from "@/hooks/useOrganizationParams"; +import { z } from "zod"; +import { InputError } from "@/components/forms/InputError"; +import { useRouter } from "next/router"; export const getServerSideProps = createServerSideProps(); +const validator = z.object({ + id: z.string().min(2), + name: z.string().min(2), +}); + +type IForm = z.infer; + export default function Organization() { + const router = useRouter() const params = useOrganizationParams(); const query = api.organization.get.useQuery({ slug: params.organization, }); const mutation = api.organization.update.useMutation({ - onSuccess() { + onSuccess(res) { toast({ title: "Organization updated", description: "Your organization has been updated.", }); query.refetch(); + router.replace(`/${res.slug}/settings/organization`) }, onError: handleError, }); const data = query.data; - const { register, handleSubmit, reset, formState, getValues } = useForm({ + const { register, handleSubmit, reset, formState } = useForm({ defaultValues: { + id: "", name: "", - slug: "", }, }); @@ -45,7 +57,9 @@ export default function Organization() { return (
mutation.mutate(values))} + onSubmit={handleSubmit((values) => { + mutation.mutate(values) + })} className="flex flex-col divide-y divide-border" > - + ]}> ({ resolver: zodResolver(validator), defaultValues: { name: "", diff --git a/apps/web/src/server/api/routers/organization.ts b/apps/web/src/server/api/routers/organization.ts index 7e043bb9..0700d653 100644 --- a/apps/web/src/server/api/routers/organization.ts +++ b/apps/web/src/server/api/routers/organization.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import { createTRPCRouter, protectedProcedure } from "@/server/api/trpc"; import { db } from "@/server/db"; import { getOrganizationBySlug } from "@/server/services/organization.service"; +import { slug } from "@/utils/slug"; export const organizationRouter = createTRPCRouter({ first: protectedProcedure.query(({ ctx }) => { @@ -28,17 +29,18 @@ export const organizationRouter = createTRPCRouter({ update: protectedProcedure .input( z.object({ + id: z.string(), name: z.string(), - slug: z.string(), }), ) .mutation(({ input }) => { return db.organization.update({ where: { - slug: input.slug, + id: input.id, }, data: { name: input.name, + slug: slug(input.name) }, }); }), diff --git a/apps/web/src/server/getServerSideProps.ts b/apps/web/src/server/getServerSideProps.ts index 6c7c97eb..8eff24fe 100644 --- a/apps/web/src/server/getServerSideProps.ts +++ b/apps/web/src/server/getServerSideProps.ts @@ -3,6 +3,7 @@ import { type GetServerSidePropsResult, } from "next"; import { getServerAuthSession } from "./auth"; +import { db } from "./db"; export function createServerSideProps( cb?: (context: GetServerSidePropsContext) => Promise, @@ -10,7 +11,7 @@ export function createServerSideProps( return async function getServerSideProps( context: GetServerSidePropsContext, ): Promise> { - const session = await getServerAuthSession(context); + const session = await getServerAuthSession(context); if(!session) { return { @@ -21,6 +22,23 @@ export function createServerSideProps( } } + if(context.params?.organization) { + const organization = await db.user.findFirst({ + where: { + id: session.user.id, + organization: { + slug: context.params.organization as string + } + } + }) + + if(!organization) { + return { + notFound: true, + } + } + } + const res = await (typeof cb === "function" ? cb(context) : Promise.resolve({}));