"use client"; import { type FormEvent, type ReactNode, useState } from "react"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import { Dialog, DialogClose, DialogDescription, DialogFooter, DialogHeader, DialogPanel, DialogPopup, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import type { InventoryInput } from "@/lib/inventory"; import { notify } from "@/lib/toast"; function Field({ label, children }: { label: string; children: ReactNode }) { return ( ); } // "Add item" dialog for the pharmacy inventory. Only the medication name is // required (matches the backend schema); the rest is optional descriptive + // stock detail. The new item is handed back via onAdd, which persists it // through the inventory API and prepends it to the page list. export function AddInventoryDialog({ open, onOpenChange, onAdd, }: { open: boolean; onOpenChange: (open: boolean) => void; onAdd: (input: InventoryInput) => void; }) { const { t } = useTranslation(); const [name, setName] = useState(""); const [form, setForm] = useState(""); const [strength, setStrength] = useState(""); const [unit, setUnit] = useState(""); const [stockQuantity, setStockQuantity] = useState("0"); const [reorderThreshold, setReorderThreshold] = useState("0"); const [location, setLocation] = useState(""); const [expiresAt, setExpiresAt] = useState(""); const reset = () => { setName(""); setForm(""); setStrength(""); setUnit(""); setStockQuantity("0"); setReorderThreshold("0"); setLocation(""); setExpiresAt(""); }; const submit = (event: FormEvent) => { event.preventDefault(); const trimmed = name.trim(); if (!trimmed) { notify.error( t("inventory.dialog.nameRequiredTitle"), t("inventory.dialog.nameRequiredBody"), ); return; } onAdd({ name: trimmed, form: form.trim(), strength: strength.trim(), unit: unit.trim(), stockQuantity: Number.parseInt(stockQuantity, 10) || 0, reorderThreshold: Number.parseInt(reorderThreshold, 10) || 0, location: location.trim(), expiresAt: expiresAt || null, }); notify.success(t("inventory.dialog.addedTitle"), trimmed); reset(); onOpenChange(false); }; return ( { onOpenChange(o); if (!o) reset(); }} open={open} > {t("inventory.dialog.title")} {t("inventory.dialog.description")}
setName(event.target.value)} placeholder={t("inventory.dialog.namePlaceholder")} value={name} />
setForm(event.target.value)} placeholder={t("inventory.dialog.formPlaceholder")} value={form} /> setStrength(event.target.value)} placeholder={t("inventory.dialog.strengthPlaceholder")} value={strength} />
setStockQuantity(event.target.value)} type="number" value={stockQuantity} /> setUnit(event.target.value)} placeholder={t("inventory.dialog.unitPlaceholder")} value={unit} /> setReorderThreshold(event.target.value)} type="number" value={reorderThreshold} />
setLocation(event.target.value)} placeholder={t("inventory.dialog.locationPlaceholder")} value={location} /> setExpiresAt(event.target.value)} type="date" value={expiresAt} />
}> {t("inventory.dialog.cancel")}
); }