"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Badge } from "@/components/ui/badge"; import { useToast } from "@/hooks/use-toast"; import { Plus, Pencil } from "lucide-react"; export type LocationOpt = { id: string; name: string }; export type ItemOpt = { id: string; name: string }; export type LabelOpt = { id: string; name: string; color: string | null }; export type ItemFields = { id?: string; name: string; description: string; quantity: string; unit: string; locationId: string; parentItemId: string; brand: string; modelNumber: string; serial: string; value: string; purchaseDate: string; purchaseFrom: string; warrantyExpiry: string; warrantyDetails: string; lifetimeWarranty: boolean; insured: boolean; barcode: string; notes: string; labelIds: string[]; }; export function blankItem(): ItemFields { return { name: "", description: "", quantity: "1", unit: "", locationId: "", parentItemId: "", brand: "", modelNumber: "", serial: "", value: "", purchaseDate: "", purchaseFrom: "", warrantyExpiry: "", warrantyDetails: "", lifetimeWarranty: false, insured: false, barcode: "", notes: "", labelIds: [], }; } const NONE = "__none__"; function ItemDialog({ trigger, title, initial, locations, items, labels: initialLabels, }: { trigger: React.ReactNode; title: string; initial: ItemFields; locations: LocationOpt[]; items: ItemOpt[]; labels: LabelOpt[]; }) { const [open, setOpen] = useState(false); const [f, setF] = useState(initial); const [labels, setLabels] = useState(initialLabels); const [newLabel, setNewLabel] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const router = useRouter(); const { toast } = useToast(); function set(k: K, v: ItemFields[K]) { setF((p) => ({ ...p, [k]: v })); } function toggleLabel(id: string) { setF((p) => ({ ...p, labelIds: p.labelIds.includes(id) ? p.labelIds.filter((x) => x !== id) : [...p.labelIds, id] })); } async function addLabel() { const name = newLabel.trim(); if (!name) return; const res = await fetch("/api/v1/labels", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name }), }); if (res.ok) { const lab = await res.json(); setLabels((p) => (p.some((l) => l.id === lab.id) ? p : [...p, lab])); toggleLabel(lab.id); setNewLabel(""); } } // Items selectable as a parent — exclude self. const parentOptions = items.filter((i) => i.id !== f.id); async function submit(e: React.FormEvent) { e.preventDefault(); if (!f.name.trim()) { setError("Please give the item a name."); return; } setError(""); setBusy(true); const payload = { name: f.name.trim(), description: f.description.trim() || undefined, quantity: f.quantity ? Number(f.quantity) : undefined, unit: f.unit.trim() || undefined, locationId: f.locationId || null, parentItemId: f.parentItemId || null, brand: f.brand.trim() || undefined, modelNumber: f.modelNumber.trim() || undefined, serial: f.serial.trim() || undefined, value: f.value ? Number(f.value) : null, purchaseDate: f.purchaseDate || null, purchaseFrom: f.purchaseFrom.trim() || undefined, warrantyExpiry: f.warrantyExpiry || null, warrantyDetails: f.warrantyDetails.trim() || undefined, lifetimeWarranty: f.lifetimeWarranty, insured: f.insured, barcode: f.barcode.trim() || undefined, notes: f.notes.trim() || undefined, labelIds: f.labelIds, }; const res = await fetch(f.id ? `/api/v1/items/${f.id}` : "/api/v1/items", { method: f.id ? "PATCH" : "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); setBusy(false); if (!res.ok) { const err = await res.json().catch(() => ({})); toast({ title: "Error", description: err.error ?? "Could not save.", variant: "destructive" }); return; } toast({ title: f.id ? "Saved!" : "Item added!" }); setOpen(false); router.refresh(); } return ( <> { setF(initial); setLabels(initialLabels); setOpen(true); }}>{trigger} {title}
set("name", e.target.value)} placeholder="Cordless drill" /> {error &&

{error}

}
set("quantity", e.target.value)} />
set("unit", e.target.value)} placeholder="each" />
set("brand", e.target.value)} placeholder="DeWalt" />
set("modelNumber", e.target.value)} />
set("serial", e.target.value)} />
set("value", e.target.value)} />
set("purchaseDate", e.target.value)} />
set("warrantyExpiry", e.target.value)} disabled={f.lifetimeWarranty} />
set("purchaseFrom", e.target.value)} placeholder="Home Depot, Amazon, gift…" />