Skip to content

Commit

Permalink
allow renaming site
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorVation committed May 19, 2023
1 parent d7b57b4 commit 4483d8a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 56 deletions.
93 changes: 41 additions & 52 deletions app/(introducty)/api/sites/[siteId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,58 +55,47 @@ export async function DELETE(
}
}

// const patchRequestSchema = z.object({
// params: z.object({
// gradientId: z.number(),
// backgroundType: z.string(),
// solid: z.string(),
// }),
// });
// export async function PATCH(
// req: Request,
// context: z.infer<typeof routeContextSchema>
// ) {
// try {
// // Validate route params.
// const { params } = routeContextSchema.parse(context);

// // Check if the user has access to this post.
// if (!(await verifyCurrentUserHasAccessToSite(params.siteId))) {
// return new Response(null, { status: 403 });
// }

// // Get the request body and validate it.
// const json = await req.json();
// const { gradientId, backgroundType, solid } =
// patchRequestSchema.parse(json).params;

// // Update the post.
// const supabase = createRouteHandlerSupabaseClient<Database>({
// headers,
// cookies,
// });
// const { error: updateError } = await supabase
// .from("site_design")
// .update({
// gradient_id: gradientId,
// background_type: backgroundType,
// solid,
// })
// .eq("id", params.siteId);

// if (updateError) {
// console.error(updateError);
// return new Response(null, { status: 500 });
// }
// return new Response(null, { status: 200 });
// } catch (error) {
// if (error instanceof z.ZodError) {
// return new Response(JSON.stringify(error.issues), { status: 422 });
// }

// return new Response(null, { status: 500 });
// }
// }
const patchRequestSchema = z.object({
siteName: z.string(),
hideBranding: z.boolean(),
});
export async function PATCH(
req: Request,
context: z.infer<typeof routeContextSchema>
) {
try {
// Validate route params.
const { params } = routeContextSchema.parse(context);

// Get the request body and validate it.
const json = await req.json();
const { siteName } = patchRequestSchema.parse(json);

// Update the post.
const supabase = createRouteHandlerSupabaseClient<Database>({
headers,
cookies,
});
const { error: updateError } = await supabase
.from("sites")
.update({
site_name: siteName,
})
.eq("id", params.siteId);

if (updateError) {
console.error(updateError);
return new Response(null, { status: 500 });
}
return new Response(null, { status: 200 });
} catch (error) {
if (error instanceof z.ZodError) {
return new Response(JSON.stringify(error.issues), { status: 422 });
}

return new Response(null, { status: 500 });
}
}

export async function verifyCurrentUserHasAccessToSite(siteId: string) {
const authUser = await getUser();
Expand Down
20 changes: 16 additions & 4 deletions app/(introducty)/editor/[siteId]/SiteSettingsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Props = {

type Inputs = {
siteName: string;
hideBranding: boolean;
};

async function copyLinkToClipboard(e: React.SyntheticEvent, siteName: string) {
Expand All @@ -51,8 +52,19 @@ export default function SiteSettingsCard({ siteName, siteId }: Props) {
formState: { isSubmitting },
} = useForm<Inputs>({ defaultValues: { siteName } });

function updateSite(data: Inputs) {
toast("Editing settings coming soon!");
async function updateSite(data: Inputs) {
const res = await fetch(`/api/sites/${siteId}`, {
method: "PATCH",
body: JSON.stringify({ siteName: data.siteName, hideBranding: false }),
});
if (!res.ok) {
if (res.status === 422) return toast.error(await res.text());
console.error(res);
toast.error("Error, site design wasn't updated. Please try again");
} else {
router.refresh();
toast.success(`Updated site design!`);
}
}

return (
Expand Down Expand Up @@ -106,9 +118,9 @@ export default function SiteSettingsCard({ siteName, siteId }: Props) {
</div>

<div className="flex items-center space-x-2">
<Switch id="hide-branding" disabled />
<Switch id="hide-branding" {...register("hideBranding")} />
<Label
htmlFor="hide-branding"
htmlFor="hideBranding"
className="text-secondary-foreground"
>
<Badge className="mr-2" variant="outline">
Expand Down

0 comments on commit 4483d8a

Please sign in to comment.