124 lines
3.4 KiB
TypeScript
124 lines
3.4 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
createContext,
|
|
useContext,
|
|
useState,
|
|
useEffect,
|
|
useCallback,
|
|
type ReactNode,
|
|
} from "react";
|
|
import type { Stabilimento, Magazzino } from "@/src/types/stabilimento";
|
|
|
|
interface StabilimentoContextValue {
|
|
stabilimenti: Stabilimento[];
|
|
magazzini: Magazzino[];
|
|
selectedStabilimento: Stabilimento | null;
|
|
selectedMagazzino: Magazzino | null;
|
|
loading: boolean;
|
|
setSelectedStabilimento: (stabilimento: Stabilimento | null) => void;
|
|
setSelectedMagazzino: (magazzino: Magazzino | null) => void;
|
|
filteredMagazzini: Magazzino[];
|
|
resetSelection: () => void;
|
|
reload: () => Promise<void>;
|
|
}
|
|
|
|
const StabilimentoContext = createContext<StabilimentoContextValue | null>(null);
|
|
|
|
export function StabilimentoProvider({ children }: { children: ReactNode }) {
|
|
const [stabilimenti, setStabilimenti] = useState<Stabilimento[]>([]);
|
|
const [magazzini, setMagazzini] = useState<Magazzino[]>([]);
|
|
const [selectedStabilimento, setSelectedStabilimentoState] = useState<Stabilimento | null>(null);
|
|
const [selectedMagazzino, setSelectedMagazzinoState] = useState<Magazzino | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const reload = useCallback(async () => {
|
|
try {
|
|
const [stabRes, magRes] = await Promise.all([
|
|
fetch("/api/stabilimenti"),
|
|
fetch("/api/magazzini"),
|
|
]);
|
|
|
|
if (stabRes.ok) {
|
|
const stabData = await stabRes.json();
|
|
const list: Stabilimento[] = stabData.stabilimenti || [];
|
|
setStabilimenti(list);
|
|
|
|
setSelectedStabilimentoState((prev) => {
|
|
if (!prev) return prev;
|
|
return list.find((s) => s.id === prev.id) ?? null;
|
|
});
|
|
}
|
|
|
|
if (magRes.ok) {
|
|
const magData = await magRes.json();
|
|
const list: Magazzino[] = magData.magazzini || [];
|
|
setMagazzini(list);
|
|
|
|
setSelectedMagazzinoState((prev) => {
|
|
if (!prev) return prev;
|
|
return list.find((m) => m.id === prev.id) ?? null;
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error("Errore caricamento stabilimenti/magazzini:", error);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
async function fetchData() {
|
|
setLoading(true);
|
|
await reload();
|
|
setLoading(false);
|
|
}
|
|
|
|
fetchData();
|
|
}, [reload]);
|
|
|
|
const setSelectedStabilimento = useCallback((stabilimento: Stabilimento | null) => {
|
|
setSelectedStabilimentoState(stabilimento);
|
|
setSelectedMagazzinoState(null);
|
|
}, []);
|
|
|
|
const setSelectedMagazzino = useCallback((magazzino: Magazzino | null) => {
|
|
setSelectedMagazzinoState(magazzino);
|
|
}, []);
|
|
|
|
const resetSelection = useCallback(() => {
|
|
setSelectedStabilimentoState(null);
|
|
setSelectedMagazzinoState(null);
|
|
}, []);
|
|
|
|
const filteredMagazzini = selectedStabilimento
|
|
? magazzini.filter((m) => m.id_stabilimento === selectedStabilimento.id)
|
|
: [];
|
|
|
|
return (
|
|
<StabilimentoContext.Provider
|
|
value={{
|
|
stabilimenti,
|
|
magazzini,
|
|
selectedStabilimento,
|
|
selectedMagazzino,
|
|
loading,
|
|
setSelectedStabilimento,
|
|
setSelectedMagazzino,
|
|
filteredMagazzini,
|
|
resetSelection,
|
|
reload,
|
|
}}
|
|
>
|
|
{children}
|
|
</StabilimentoContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useStabilimento() {
|
|
const context = useContext(StabilimentoContext);
|
|
|
|
if (!context) {
|
|
throw new Error("useStabilimento deve essere usato dentro StabilimentoProvider");
|
|
}
|
|
|
|
return context;
|
|
}
|