"use client"; import { useEffect, useState } from "react"; import { CountrySelect } from "@/components/CountrySelect"; type Address = { id: string; first_name: string; last_name: string; address_1: string; address_2?: string; city: string; province?: string; postal_code: string; country_code: string; phone?: string; }; const inputClass = "no-ring w-full rounded-xl border border-lightline px-6 py-3 shadow-lg text-lighttext transition-all duration-200 ease-in-out placeholder:text-lightsec hover:border-lightline-hover focus:border-lightline-focus dark:border-darkline dark:text-darktext dark:placeholder:text-darksec dark:hover:border-darkline-hover dark:focus:border-darkline-focus"; const emptyForm = { first_name: "", last_name: "", address_1: "", address_2: "", city: "", province: "", postal_code: "", country_code: "nl", phone: "", }; export function AddressesTab() { const [address, setAddress] = useState
(null); const [loading, setLoading] = useState(true); const [editing, setEditing] = useState(false); const [form, setForm] = useState(emptyForm); const [submitting, setSubmitting] = useState(false); const [error, setError] = useState(null); useEffect(() => { fetchAddress(); }, []); async function fetchAddress() { try { const res = await fetch("/api/account/addresses"); if (res.ok) { const data = await res.json(); const addresses: Address[] = data.addresses ?? []; setAddress(addresses[0] ?? null); } } catch { // Silently fail } finally { setLoading(false); } } function startEdit() { if (address) { setForm({ first_name: address.first_name ?? "", last_name: address.last_name ?? "", address_1: address.address_1 ?? "", address_2: address.address_2 ?? "", city: address.city ?? "", province: address.province ?? "", postal_code: address.postal_code ?? "", country_code: address.country_code ?? "nl", phone: address.phone ?? "", }); } else { setForm(emptyForm); } setEditing(true); setError(null); } function cancelForm() { setEditing(false); setForm(emptyForm); setError(null); } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setSubmitting(true); setError(null); const url = address ? `/api/account/addresses/${address.id}` : "/api/account/addresses"; try { const res = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(form), }); if (!res.ok) { const data = await res.json().catch(() => ({})); throw new Error(data.error ?? "Failed to save address"); } cancelForm(); await fetchAddress(); } catch (err) { setError(err instanceof Error ? err.message : "Something went wrong"); } finally { setSubmitting(false); } } function updateField(field: string, value: string) { setForm((prev) => ({ ...prev, [field]: value })); } if (loading) { return

Loading address…

; } // Display mode: show the saved address (or empty state) with an Edit / Add button if (!editing) { if (address) { return (

{address.first_name} {address.last_name}

{address.address_1} {address.address_2 ? `, ${address.address_2}` : ""}

{address.city} {address.province ? `, ${address.province}` : ""}, {address.postal_code},{" "} {address.country_code.toUpperCase()}

{address.phone && (

{address.phone}

)}
); } return (

No address saved. Add one to speed up checkout.

); } // Edit / Add form return (

{address ? "Edit Address" : "Add Address"}

updateField("first_name", e.target.value)} className={inputClass} /> updateField("last_name", e.target.value)} className={inputClass} />
updateField("address_1", e.target.value)} className={inputClass} /> updateField("address_2", e.target.value)} className={inputClass} />
updateField("city", e.target.value)} className={inputClass} /> updateField("province", e.target.value)} className={inputClass} /> updateField("postal_code", e.target.value)} className={inputClass} />
updateField("country_code", code)} className={inputClass} /> updateField("phone", e.target.value)} className={inputClass} />
{error && (
{error}
)}
); }