mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-04 00:53:49 +00:00
Fix naming and add update to site provider?
This commit is contained in:
@@ -1,16 +1,44 @@
|
||||
"use client";
|
||||
|
||||
import { SiteContext } from "@app/contexts/siteContext";
|
||||
import api from "@app/api";
|
||||
import SiteContext from "@app/contexts/siteContext";
|
||||
import { toast } from "@app/hooks/use-toast";
|
||||
import { GetSiteResponse } from "@server/routers/site/getSite";
|
||||
import { ReactNode } from "react";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { useState } from "react";
|
||||
|
||||
type LandingProviderProps = {
|
||||
interface SiteProviderProps {
|
||||
children: React.ReactNode;
|
||||
site: GetSiteResponse | null;
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
export function SiteProvider({ site, children }: LandingProviderProps) {
|
||||
return <SiteContext.Provider value={site}>{children}</SiteContext.Provider>;
|
||||
}
|
||||
|
||||
export default SiteProvider;
|
||||
export function SiteProvider({ children, site: serverSite }: SiteProviderProps) {
|
||||
const [site, setSite] = useState<GetSiteResponse | null>(serverSite);
|
||||
|
||||
const updateSite = async (updatedSite: Partial<GetSiteResponse>) => {
|
||||
try {
|
||||
if (!site) {
|
||||
throw new Error("No site to update");
|
||||
}
|
||||
|
||||
const res = await api.post<AxiosResponse<GetSiteResponse>>(
|
||||
`site/${site.siteId}`,
|
||||
updatedSite,
|
||||
);
|
||||
setSite(res.data.data);
|
||||
toast({
|
||||
title: "Site updated!",
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
toast({
|
||||
title: "Error updating site...",
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
return <SiteContext.Provider value={{ site, updateSite }}>{children}</SiteContext.Provider>;
|
||||
}
|
||||
|
||||
export default SiteProvider;
|
||||
Reference in New Issue
Block a user