36 lines
932 B
TypeScript
36 lines
932 B
TypeScript
import { Chantier, Chef, exempleChantier } 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;
|
|
}; |