infoalloggi-monorepo/apps/infoalloggi/src/providers/ServizioProvider.tsx

108 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { createContext, useContext } from "react";
import toast from "react-hot-toast";
2025-08-04 17:45:44 +02:00
import type { AnnunciId } from "~/schemas/public/Annunci";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import type { ServizioAnnunciUpdate } from "~/schemas/public/ServizioAnnunci";
2025-08-04 17:45:44 +02:00
import type { UsersId } from "~/schemas/public/Users";
import type { ServizioData } from "~/server/controllers/servizio.controller";
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
type ServizioProviderProps = {
isAdmin: boolean;
userId: UsersId;
servizioId: ServizioServizioId;
servizio: ServizioData;
children: React.ReactNode;
};
2025-08-04 17:45:44 +02:00
type ServizioContextProps = {
2025-08-28 18:27:07 +02:00
isAdmin: boolean;
userId: UsersId;
servizioId: ServizioServizioId;
servizio: ServizioData;
2025-08-04 17:45:44 +02:00
};
const ServizioContext = createContext<ServizioContextProps | null>(null);
export const ServizioProvider = ({
2025-08-28 18:27:07 +02:00
children,
...props
}: ServizioProviderProps) => {
2025-08-28 18:27:07 +02:00
return (
<ServizioContext.Provider value={{ ...props }}>
2025-08-28 18:27:07 +02:00
{children}
</ServizioContext.Provider>
);
2025-08-04 17:45:44 +02:00
};
export const useServizio = () => {
2025-08-28 18:27:07 +02:00
const servizioContext = useContext(ServizioContext);
if (!servizioContext) {
throw new Error("useServizio must be used within a ServizioProvider");
}
return servizioContext;
2025-08-04 17:45:44 +02:00
};
type ServizioAnnuncioProviderProps = {
data: ServizioData["annunci"][number];
servizioId: ServizioServizioId;
userId: UsersId;
children: React.ReactNode;
};
2025-08-04 17:45:44 +02:00
type ServizioAnnuncioContextProps = {
2025-08-28 18:27:07 +02:00
data: ServizioData["annunci"][number];
annuncioId: AnnunciId;
updateServizioAnnunci: (data: ServizioAnnunciUpdate) => Promise<void>;
2025-08-04 17:45:44 +02:00
};
const ServizioAnnuncioContext =
2025-08-28 18:27:07 +02:00
createContext<ServizioAnnuncioContextProps | null>(null);
2025-08-04 17:45:44 +02:00
export const ServizioAnnuncioProvider = ({
2025-08-28 18:27:07 +02:00
data,
servizioId,
userId,
2025-08-28 18:27:07 +02:00
children,
}: ServizioAnnuncioProviderProps) => {
const utils = api.useUtils();
const { mutateAsync } = api.servizio.updateServizioAnnunci.useMutation({
onError: (error) => {
toast.error(error.message);
},
onSuccess: async () => {
await utils.servizio.getAllServizioAnnunci.invalidate({
userId: userId,
});
await utils.servizio.getServizio.invalidate({
servizioId,
});
toast.success("Dati salvati con successo");
},
});
const updateServizioAnnunci = async (upd: ServizioAnnunciUpdate) => {
await mutateAsync({
annuncioId: data.id,
servizioId,
data: upd,
});
};
2025-08-28 18:27:07 +02:00
return (
<ServizioAnnuncioContext.Provider
value={{ annuncioId: data.annunci_id, data, updateServizioAnnunci }}
2025-08-28 18:27:07 +02:00
>
{children}
</ServizioAnnuncioContext.Provider>
);
2025-08-04 17:45:44 +02:00
};
export const useServizioAnnuncio = () => {
2025-08-28 18:27:07 +02:00
const servizioAnnuncioContext = useContext(ServizioAnnuncioContext);
if (!servizioAnnuncioContext) {
throw new Error(
"useServizioAnnuncio must be used within a ServizioAnnuncioProvider",
);
}
return servizioAnnuncioContext;
2025-08-04 17:45:44 +02:00
};