"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import type { CustomerDetail, CustomerInput, Currency } from "@/lib/types"; import { createCustomer, updateCustomer } from "@/lib/api"; /** ISO date (yyyy-mm-dd) for a date input, from an API date string. */ function toDateInput(v: string | null | undefined): string { if (!v) return ""; const d = new Date(v); return isNaN(d.getTime()) ? "" : d.toISOString().slice(0, 10); } function numOrUndef(v: string | number | null | undefined): number | undefined { if (v === null || v === undefined || v === "") return undefined; const n = Number(v); return isNaN(n) ? undefined : n; } type Values = { name: string; addressLine1: string; addressLine2: string; city: string; state: string; zipCode: string; country: string; phone: string; mobile: string; fax: string; email: string; identificationType: string; identificationNumber: string; identificationExpiration: string; customerSince: string; preferredCurrency: Currency; minimumBalance: string; feeAmount: string; status: boolean; notes: string; }; function initial(c?: CustomerDetail): Values { return { name: c?.name ?? "", addressLine1: c?.addressLine1 ?? "", addressLine2: c?.addressLine2 ?? "", city: c?.city ?? "", state: c?.state ?? "", zipCode: c?.zipCode ?? "", country: c?.country ?? "", phone: c?.phone ?? "", mobile: c?.mobile ?? "", fax: c?.fax ?? "", email: c?.email ?? "", identificationType: c?.identificationType ?? "", identificationNumber: c?.identificationNumber ?? "", identificationExpiration: toDateInput(c?.identificationExpiration), customerSince: toDateInput(c?.customerSince), preferredCurrency: (c?.preferredCurrency as Currency) ?? "USD", minimumBalance: c?.minimumBalance != null ? String(c.minimumBalance) : "", feeAmount: c?.feeAmount != null ? String(c.feeAmount) : "", status: c?.status ?? true, notes: c?.notes ?? "", }; } /** Empty string -> undefined so we don't send blanks as real values. */ function s(v: string): string | undefined { const t = v.trim(); return t === "" ? undefined : t; } /** * Shared create/edit form. When `customer` is given it edits (PATCH), otherwise * it creates (POST). Redirects to the customer's detail page on success. */ export function CustomerForm({ customer }: { customer?: CustomerDetail }) { const router = useRouter(); const editing = !!customer; const [v, setV] = useState(() => initial(customer)); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); function set(key: K, val: Values[K]) { setV((prev) => ({ ...prev, [key]: val })); } async function submit(e: React.FormEvent) { e.preventDefault(); setSaving(true); setError(null); const payload: CustomerInput = { name: v.name.trim(), addressLine1: s(v.addressLine1), addressLine2: s(v.addressLine2), city: s(v.city), state: s(v.state), zipCode: s(v.zipCode), country: s(v.country), phone: s(v.phone), mobile: s(v.mobile), fax: s(v.fax), email: s(v.email), identificationType: s(v.identificationType), identificationNumber: s(v.identificationNumber), identificationExpiration: s(v.identificationExpiration), customerSince: s(v.customerSince), preferredCurrency: v.preferredCurrency, minimumBalance: numOrUndef(v.minimumBalance), feeAmount: numOrUndef(v.feeAmount), status: v.status, notes: s(v.notes), }; try { const saved = editing ? await updateCustomer(customer!.id, payload) : await createCustomer(payload); router.push(`/clientes/${saved.id}`); } catch (e2) { setError((e2 as Error)?.message ?? "No se pudo guardar el cliente."); setSaving(false); } } return (
{error &&
{error}
}

Identidad

set("name", e.target.value)} /> set("email", e.target.value)} /> set("phone", e.target.value)} /> set("mobile", e.target.value)} /> set("fax", e.target.value)} /> set("customerSince", e.target.value)} />

Domicilio

set("addressLine1", e.target.value)} /> set("addressLine2", e.target.value)} /> set("city", e.target.value)} /> set("state", e.target.value)} /> set("zipCode", e.target.value)} /> set("country", e.target.value)} />

Identificación y cuenta

set("identificationType", e.target.value)} /> set("identificationNumber", e.target.value)} /> set("identificationExpiration", e.target.value)} /> set("minimumBalance", e.target.value)} /> set("feeAmount", e.target.value)} /> set("status", e.target.checked)} />