diff --git a/src/components/event-modal.tsx b/src/components/event-modal.tsx index e6c0c39..ce508a7 100644 --- a/src/components/event-modal.tsx +++ b/src/components/event-modal.tsx @@ -140,8 +140,9 @@ export function EventModal({ type, babyId, onClose, onSaved, initialEvent }: Pro // Pump → stock state const [addToStock, setAddToStock] = useState(false); const [pumpStockLocation, setPumpStockLocation] = useState<"room" | "fridge" | "freezer">("fridge"); - const [todayLots, setTodayLots] = useState([]); + const [availableLots, setAvailableLots] = useState([]); const [mergeLotId, setMergeLotId] = useState(null); + const [pumpExpiryCustom, setPumpExpiryCustom] = useState(""); const selectedProfile = profiles.find((p) => p.id === selectedProfileId) ?? null; @@ -162,13 +163,10 @@ export function EventModal({ type, babyId, onClose, onSaved, initialEvent }: Pro .then((r) => r.json()) .then((stocks) => { if (!Array.isArray(stocks)) return; - const today = new Date().toDateString(); - const lots = stocks.filter( - (s: MilkStock & { used?: boolean }) => - !s.used && new Date(s.storedAt).toDateString() === today - ); - lots.sort((a: MilkStock, b: MilkStock) => new Date(b.storedAt).getTime() - new Date(a.storedAt).getTime()); - setTodayLots(lots); + const lots = stocks + .filter((s: MilkStock & { used?: boolean }) => !s.used) + .sort((a: MilkStock, b: MilkStock) => new Date(b.storedAt).getTime() - new Date(a.storedAt).getTime()); + setAvailableLots(lots); }); }, [type, babyId]); @@ -353,17 +351,24 @@ export function EventModal({ type, babyId, onClose, onSaved, initialEvent }: Pro const vol = parseFloat(form.pumpVolume); if (vol > 0) { if (mergeLotId) { - const existing = todayLots.find((l) => l.id === mergeLotId); + const existing = availableLots.find((l) => l.id === mergeLotId); await fetch(`/api/milk/${mergeLotId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ volume: (existing?.volume ?? 0) + vol }), }); } else { + let expiryHours: number | undefined; + if (pumpExpiryCustom) { + const v = parseFloat(pumpExpiryCustom); + expiryHours = pumpStockLocation === "freezer" ? v * 24 * 30 + : pumpStockLocation === "fridge" ? v * 24 + : v; + } await fetch("/api/milk", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ babyId, volume: vol, location: pumpStockLocation }), + body: JSON.stringify({ babyId, volume: vol, location: pumpStockLocation, expiryHours }), }); } } @@ -536,27 +541,38 @@ export function EventModal({ type, babyId, onClose, onSaved, initialEvent }: Pro ))} - {todayLots.length > 0 && ( -
- -
- + {availableLots.map((lot) => ( + - {todayLots.map((lot) => ( - - ))} -
- {mergeLotId && ( -

- Volume sera ajouté à ce lot. Péremption inchangée. -

- )} + ))} +
+ {mergeLotId && ( +

Volume ajouté. Péremption inchangée.

+ )} + + {!mergeLotId && ( +
+ + setPumpExpiryCustom(e.target.value)} + className={inputCls} + placeholder={pumpStockLocation === "freezer" ? "6" : pumpStockLocation === "fridge" ? "4" : "4"} + min="1" + />
)}