From ab416959889ccc94ba9b026eef9a858ec76531a4 Mon Sep 17 00:00:00 2001 From: Owen Schwartz Date: Sun, 13 Oct 2024 22:58:47 -0400 Subject: [PATCH] Add site context? --- src/app/[orgId]/sites/[siteId]/layout.tsx | 26 ++++++++++++----------- src/components/appearance-form.tsx | 5 +++++ src/hooks/useSiteContext.ts | 7 ++++++ 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 src/hooks/useSiteContext.ts diff --git a/src/app/[orgId]/sites/[siteId]/layout.tsx b/src/app/[orgId]/sites/[siteId]/layout.tsx index 373b94ca..da421e1e 100644 --- a/src/app/[orgId]/sites/[siteId]/layout.tsx +++ b/src/app/[orgId]/sites/[siteId]/layout.tsx @@ -4,7 +4,10 @@ import Image from "next/image" import { Separator } from "@/components/ui/separator" import { SidebarNav } from "@/components/sidebar-nav" import SiteProvider from "@app/providers/SiteProvider" -import api from "@app/api" +import { internal } from "@app/api" +import { cookies } from "next/headers" +import { GetSiteResponse } from "@server/routers/site" +import { AxiosResponse } from "axios" export const metadata: Metadata = { title: "Forms", @@ -40,20 +43,19 @@ interface SettingsLayoutProps { } export default async function SettingsLayout({ children, params }: SettingsLayoutProps) { - const res = await api - .get(`/site/${params.siteId}`, {}) - .catch((e) => { - console.error("Failed to fetch site", e); + const sessionId = cookies().get("session")?.value ?? null; + const res = await internal + .get>(`/site/${params.siteId}`, { + headers: { + Cookie: `session=${sessionId}`, + }, }); - console.log(res); - - console.log(params.siteId); - - // const site = res!.data.data; + if (!res || res.status !== 200) { + return
Failed to load site
; + } - // console.log(site); - const site: any = {}; + const site = res.data.data; return ( <> diff --git a/src/components/appearance-form.tsx b/src/components/appearance-form.tsx index 9a81f37a..95ac2d2d 100644 --- a/src/components/appearance-form.tsx +++ b/src/components/appearance-form.tsx @@ -18,6 +18,7 @@ import { FormMessage, } from "@/components/ui/form" import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group" +import { useSiteContext } from "@app/hooks/useSiteContext" const appearanceFormSchema = z.object({ theme: z.enum(["light", "dark"], { @@ -37,6 +38,10 @@ const defaultValues: Partial = { } export function AppearanceForm() { + const site = useSiteContext(); + + console.log(site); + const form = useForm({ resolver: zodResolver(appearanceFormSchema), defaultValues, diff --git a/src/hooks/useSiteContext.ts b/src/hooks/useSiteContext.ts new file mode 100644 index 00000000..d944dc4c --- /dev/null +++ b/src/hooks/useSiteContext.ts @@ -0,0 +1,7 @@ +import { SiteContext } from "@app/contexts/siteContext"; +import { useContext } from "react"; + +export function useSiteContext() { + const site = useContext(SiteContext); + return site; +}