Skip to content

Commit

Permalink
chore: rename setSiteConfigAdmin to setSiteConfigByAdmin
Browse files Browse the repository at this point in the history
  • Loading branch information
dcshzj committed Feb 3, 2025
1 parent eeb75cc commit 31dcf08
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
59 changes: 31 additions & 28 deletions apps/studio/src/pages/sites/[siteId]/admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useIsUserIsomerAdmin } from "~/hooks/useIsUserIsomerAdmin"
import { useQueryParse } from "~/hooks/useQueryParse"
import { useZodForm } from "~/lib/form"
import { type NextPageWithLayout } from "~/lib/types"
import { setSiteConfigAdminSchema } from "~/schemas/site"
import { setSiteConfigByAdminSchema } from "~/schemas/site"
import { AdminSidebarOnlyLayout } from "~/templates/layouts/AdminSidebarOnlyLayout"
import { trpc } from "~/utils/trpc"

Expand Down Expand Up @@ -56,31 +56,6 @@ const SiteAdminPage: NextPageWithLayout = () => {
void router.push(`/sites/${siteId}`)
}

const { mutate, isLoading } = trpc.site.setSiteConfigAdmin.useMutation({
onSuccess: async () => {
// reset({ config, theme, navbar, footer })
await trpcUtils.site.getConfig.invalidate({ id: siteId })
await trpcUtils.site.getTheme.invalidate({ id: siteId })
await trpcUtils.site.getNavbar.invalidate({ id: siteId })
await trpcUtils.site.getFooter.invalidate({ id: siteId })
toast({
title: "Saved site config!",
description: "Check your site in 5-10 minutes to view it live.",
status: "success",
...BRIEF_TOAST_SETTINGS,
})
},
onError: () => {
toast({
title: "Error saving site config!",
description:
"If this persists, please report this issue at [email protected]",
status: "error",
...BRIEF_TOAST_SETTINGS,
})
},
})

const [previousConfig] = trpc.site.getConfig.useSuspenseQuery({
id: siteId,
})
Expand All @@ -94,13 +69,15 @@ const SiteAdminPage: NextPageWithLayout = () => {
id: siteId,
})

// NOTE: Refining the setNotificationSchema here instead of in site.ts since omit does not work after refine
// NOTE: Refining the setSiteConfigByAdminSchema here instead of in site.ts since omit does not work after refine
const {
reset,
watch,
register,
handleSubmit,
formState: { isDirty, errors },
} = useZodForm({
schema: setSiteConfigAdminSchema
schema: setSiteConfigByAdminSchema
.omit({ siteId: true })
.refine((data) => !!data.config, {
message: "Site config must be present",
Expand All @@ -126,6 +103,32 @@ const SiteAdminPage: NextPageWithLayout = () => {
},
})

const { mutate, isLoading } = trpc.site.setSiteConfigByAdmin.useMutation({
onSuccess: async () => {
await trpcUtils.site.getConfig.invalidate({ id: siteId })
await trpcUtils.site.getTheme.invalidate({ id: siteId })
await trpcUtils.site.getNavbar.invalidate({ id: siteId })
await trpcUtils.site.getFooter.invalidate({ id: siteId })
// Reset the form's isDirty but use the latest values provided by the user
reset(watch())
toast({
title: "Saved site config!",
description: "Check your site in 5-10 minutes to view it live.",
status: "success",
...BRIEF_TOAST_SETTINGS,
})
},
onError: () => {
toast({
title: "Error saving site config!",
description:
"If this persists, please report this issue at [email protected]",
status: "error",
...BRIEF_TOAST_SETTINGS,
})
},
})

const [nextUrl, setNextUrl] = useState("")
const isOpen = !!nextUrl

Expand Down
2 changes: 1 addition & 1 deletion apps/studio/src/schemas/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getNameSchema = z.object({

// NOTE: This is a temporary schema for editing the JSON content directly,
// until the proper editing experience is implemented
export const setSiteConfigAdminSchema = z.object({
export const setSiteConfigByAdminSchema = z.object({
siteId: z.number().min(1),
config: z.string(),
theme: z.string(),
Expand Down
6 changes: 3 additions & 3 deletions apps/studio/src/server/modules/site/site.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
getNameSchema,
getNotificationSchema,
setNotificationSchema,
setSiteConfigAdminSchema,
setSiteConfigByAdminSchema,
} from "~/schemas/site"
import { protectedProcedure, router } from "~/server/trpc"
import { safeJsonParse } from "~/utils/safeJsonParse"
Expand Down Expand Up @@ -144,8 +144,8 @@ export const siteRouter = router({

return input
}),
setSiteConfigAdmin: protectedProcedure
.input(setSiteConfigAdminSchema)
setSiteConfigByAdmin: protectedProcedure
.input(setSiteConfigByAdminSchema)
.mutation(async ({ ctx, input }) => {
const { siteId, config, theme, navbar, footer } = input
await validateUserPermissionsForSite({
Expand Down

0 comments on commit 31dcf08

Please sign in to comment.