"use client"; import { useState, type FormEvent } from "react"; import { useApiList } from "@/hooks/use-api-list"; 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 { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; // Shape returned by GET /api/vendors — snake_case per this repo's // API-boundary convention (see src/lib/vendors.ts's toRecord()). interface VendorRecord { id: string; name: string; contact_email: string; contact_phone: string; address: string; tax_code: string | null; notes: string | null; created_at: string; } export default function VendorsPage() { const { data: vendors, loading, error, refetch } = useApiList( "/api/vendors" ); const [name, setName] = useState(""); const [contactEmail, setContactEmail] = useState(""); const [contactPhone, setContactPhone] = useState(""); const [address, setAddress] = useState(""); const [taxCode, setTaxCode] = useState(""); const [notes, setNotes] = useState(""); const [submitting, setSubmitting] = useState(false); const [submitError, setSubmitError] = useState(null); async function handleSubmit(event: FormEvent) { event.preventDefault(); setSubmitting(true); setSubmitError(null); try { const response = await fetch("/api/vendors", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, contactEmail, contactPhone, address, // taxCode/notes are optional+nullable and must be non-empty // strings if present (src/lib/vendors.ts's createVendorSchema) — // omit the key entirely when the field is left blank, same // pattern as employees page's managerId. ...(taxCode.trim() !== "" ? { taxCode } : {}), ...(notes.trim() !== "" ? { notes } : {}), }), }); let body: unknown; try { body = await response.json(); } catch { body = null; } if (!response.ok) { const message = body !== null && typeof body === "object" && "error" in body && typeof (body as { error: unknown }).error === "string" ? (body as { error: string }).error : "Request failed"; setSubmitError(message); return; } setName(""); setContactEmail(""); setContactPhone(""); setAddress(""); setTaxCode(""); setNotes(""); refetch(); } catch { setSubmitError("Request failed"); } finally { setSubmitting(false); } } return (

Vendors

{loading ? (

Loading...

) : error ? (

{error}

) : ( ID Name Contact Email Contact Phone Address Tax Code Notes Created At {vendors.map((vendor) => ( {vendor.id} {vendor.name} {vendor.contact_email} {vendor.contact_phone} {vendor.address} {vendor.tax_code ?? "-"} {vendor.notes ?? "-"} {vendor.created_at} ))}
)}

New Vendor

setName(event.target.value)} required />
setContactEmail(event.target.value)} required />
setContactPhone(event.target.value)} required />
setAddress(event.target.value)} required />
setTaxCode(event.target.value)} />