34 lines
898 B
TypeScript
34 lines
898 B
TypeScript
import { Chantier } from "@/class/class";
|
|
import { createContext, ReactNode, useContext, useMemo, useState } from "react";
|
|
|
|
type ChantierContextType = {
|
|
chantier: Chantier | null;
|
|
setChantier: (p: Chantier | null) => void;
|
|
};
|
|
|
|
const ChantierContext = createContext<ChantierContextType | null>(null);
|
|
|
|
type ChantierProviderProps = {
|
|
children: ReactNode;
|
|
};
|
|
|
|
export const ChantierProvider = ({ children }: ChantierProviderProps) => {
|
|
const [chantier, setChantier] = useState<Chantier | null>(null);
|
|
|
|
const value = useMemo(() => ({ chantier, setChantier }), [chantier]);
|
|
|
|
return (
|
|
<ChantierContext.Provider value={value}>
|
|
{children}
|
|
</ChantierContext.Provider>
|
|
);
|
|
};
|
|
|
|
export const useChantier = () => {
|
|
const context = useContext(ChantierContext);
|
|
if (!context) {
|
|
throw new Error("useChantier doit être utilisé dans <ChantierProvider>");
|
|
}
|
|
return context;
|
|
};
|