"use client"; import { useMemo, useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { createAddress, type AddressInput } from "@/lib/api/addresses"; import type { AddressLabel, City, Neighborhood } from "@/types/api"; const LABEL_PRESETS: { value: AddressLabel; text: string }[] = [ { value: "other", text: "سایر" }, { value: "work", text: "محل کار" }, { value: "home", text: "خانه جدید" }, ]; export function AddressDetailsForm({ fullAddress, latitude, longitude, cityUuid, neighborhoodUuid, cities, neighborhoods, next, }: { fullAddress: string; latitude: number; longitude: number; /** Inferred from the customer's current location cookie, if they'd set one manually. */ cityUuid: string | null; neighborhoodUuid: string | null; cities: City[]; neighborhoods: Neighborhood[]; /** Set when this add-address detour started from checkout — see handleSubmit. */ next: string | null; }) { const router = useRouter(); const [address, setAddress] = useState(fullAddress); const [label, setLabel] = useState("home"); const [title, setTitle] = useState("خانه جدید"); const [plaque, setPlaque] = useState(""); const [floor, setFloor] = useState(""); const [unit, setUnit] = useState(""); const [recipientName, setRecipientName] = useState(""); const [recipientPhone, setRecipientPhone] = useState(""); const [manualCityUuid, setManualCityUuid] = useState(cityUuid ?? ""); const [manualNeighborhoodUuid, setManualNeighborhoodUuid] = useState(neighborhoodUuid ?? ""); const [error, setError] = useState(null); const [isPending, startTransition] = useTransition(); // city_uuid is a hard requirement on the backend (OpenAPI: Address.city_uuid is // required for writes) but isn't part of this screen's design — it's normally // inferred from the customer's current location cookie. Only show a picker for // it when that inference failed (e.g. they're in "geo" mode, or never set one). const needsCityPicker = !cityUuid; const filteredNeighborhoods = useMemo( () => neighborhoods.filter((n) => n.city.uuid === manualCityUuid), [neighborhoods, manualCityUuid], ); function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); const finalCityUuid = cityUuid ?? manualCityUuid; if (!finalCityUuid) { setError("شهر مشخص نیست — لطفاً محله خود را از صفحه اصلی انتخاب کنید یا شهر را زیر مشخص کنید."); return; } startTransition(async () => { const input: AddressInput = { label, title, full_address: address, plaque, floor, unit, recipient_name: recipientName, recipient_phone: recipientPhone, city_uuid: finalCityUuid, neighborhood_uuid: (neighborhoodUuid ?? manualNeighborhoodUuid) || undefined, latitude, longitude, // Arriving here from checkout means they had no address at all yet — make // this one the default immediately so it's actually used for the order, // rather than sending them on to /addresses to select it a second time. // A plain profile-driven "add another address" leaves the existing default alone. is_default: next !== null, }; const result = await createAddress(input); if (result.ok) { router.push(next ?? "/addresses"); } else { setError(result.message); } }); } function selectPreset(preset: (typeof LABEL_PRESETS)[number]) { setLabel(preset.value); setTitle(preset.text); } return (
آدرس کامل setAddress(e.target.value)} required className="h-12 rounded-xl border border-gray-300 bg-white px-4 text-[14px] text-foreground outline-none focus:border-primary-action" />
عنوان آدرس
{LABEL_PRESETS.map((preset) => ( ))}
پلاک setPlaque(e.target.value)} className="h-12 rounded-xl border border-gray-300 bg-white px-3 text-center text-[14px] text-foreground outline-none focus:border-primary-action" />
طبقه setFloor(e.target.value)} className="h-12 rounded-xl border border-gray-300 bg-white px-3 text-center text-[14px] text-foreground outline-none focus:border-primary-action" />
واحد setUnit(e.target.value)} className="h-12 rounded-xl border border-gray-300 bg-white px-3 text-center text-[14px] text-foreground outline-none focus:border-primary-action" />
نام گیرنده setRecipientName(e.target.value)} required className="h-12 rounded-xl border border-gray-300 bg-white px-4 text-[14px] text-foreground outline-none focus:border-primary-action" />
شماره تماس setRecipientPhone(e.target.value)} placeholder="09123456789" required className="h-12 rounded-xl border border-gray-300 bg-white px-4 text-[14px] text-foreground outline-none placeholder:text-gray-400 focus:border-primary-action" />
{needsCityPicker && (

شهر شما مشخص نیست — این فقط لازم است چون هنوز محله‌ای از صفحه اصلی انتخاب نکرده‌اید.

)} {error &&

{error}

}
); }