"use client"; import { MapPin, MoreVertical, Pencil, Plus, Save, Trash2, X, } from "lucide-react"; import { useState } from "react"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { cn } from "@/lib/utils"; interface Address { id: string; name: string; street: string; city: string; state: string; zip: string; country: string; phone: string; isDefault: boolean; type: "خانه" | "سرکار" | "سایر"; } const DEFAULT_ADDRESSES: Address[] = [ { id: "1", name: "علی رضایی", street: "123 Main Street, Apt 4B", city: "New York", state: "NY", zip: "10001", country: "ایران", phone: "(212) 555-1234", isDefault: true, type: "خانه", }, { id: "2", name: "علی رضایی", street: "456 Business Ave, Suite 100", city: "New York", state: "NY", zip: "10018", country: "ایران", phone: "(212) 555-5678", isDefault: false, type: "سرکار", }, ]; interface AddressBook1Props { addresses?: Address[]; className?: string; } const AddressBook1 = ({ addresses: initialAddresses = DEFAULT_ADDRESSES, className, }: AddressBook1Props) => { const [addresses, setAddresses] = useState(initialAddresses); const [selectedId, setSelectedId] = useState( addresses.find((a) => a.isDefault)?.id || addresses[0]?.id, ); const [editingId, setEditingId] = useState(null); const [isAddingNew, setIsAddingNew] = useState(false); const [editForm, setEditForm] = useState>({}); const startEditing = (address: Address) => { setEditingId(address.id); setEditForm({ ...address }); }; const cancelEditing = () => { setEditingId(null); setEditForm({}); }; const saveEdit = () => { if ( editingId && editForm.name && editForm.street && editForm.city && editForm.state && editForm.zip && editForm.country && editForm.phone && editForm.type ) { setAddresses((prev) => prev.map((addr) => addr.id === editingId ? ({ ...addr, ...editForm, } as Address) : addr, ), ); setEditingId(null); setEditForm({}); } }; const startAddingNew = () => { setIsAddingNew(true); setEditForm({ name: "", street: "", city: "", state: "", zip: "", country: "ایران", phone: "", type: "خانه", isDefault: false, }); }; const cancelAddingNew = () => { setIsAddingNew(false); setEditForm({}); }; const saveNewAddress = () => { if ( editForm.name && editForm.street && editForm.city && editForm.state && editForm.zip && editForm.country && editForm.phone && editForm.type ) { const newAddress: Address = { id: Date.now().toString(), name: editForm.name, street: editForm.street, city: editForm.city, state: editForm.state, zip: editForm.zip, country: editForm.country, phone: editForm.phone, type: editForm.type as "خانه" | "سرکار" | "سایر", isDefault: addresses.length === 0, }; setAddresses((prev) => [...prev, newAddress]); setIsAddingNew(false); setEditForm({}); if (addresses.length === 0) { setSelectedId(newAddress.id); } } }; const deleteAddress = (id: string) => { setAddresses((prev) => prev.filter((addr) => addr.id !== id)); if (selectedId === id && addresses.length > 1) { setSelectedId(addresses.find((a) => a.id !== id)?.id || ""); } if (editingId === id) { setEditingId(null); setEditForm({}); } }; return (
{/* Header */}

آدرس‌های من

آدرس‌های ذخیره‌شده خود را مدیریت کنید.

{/* Address List */}
{addresses.map((address) => ( editingId !== address.id && setSelectedId(address.id) } > {editingId === address.id ? (

ویرایش آدرس

setEditForm({ ...editForm, name: e.target.value }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, phone: e.target.value, }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, street: e.target.value, }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, city: e.target.value }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, state: e.target.value, }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, zip: e.target.value }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, country: e.target.value, }) } onClick={(e) => e.stopPropagation()} />
) : (
{address.name} {address.isDefault && ( پیش‌فرض )} {address.type}
{ e.stopPropagation(); startEditing(address); }} > ویرایش { e.stopPropagation(); deleteAddress(address.id); }} > حذف

{address.street}

{address.city}, {address.state} {address.zip}

{address.country}

{address.phone}

)}
))} {isAddingNew && (

افزودن آدرس جدید

setEditForm({ ...editForm, name: e.target.value }) } />
setEditForm({ ...editForm, phone: e.target.value }) } />
setEditForm({ ...editForm, street: e.target.value }) } />
setEditForm({ ...editForm, city: e.target.value }) } />
setEditForm({ ...editForm, state: e.target.value }) } />
setEditForm({ ...editForm, zip: e.target.value }) } />
setEditForm({ ...editForm, country: e.target.value, }) } />
)}
{addresses.length === 0 && (

آدرسی ذخیره نشد

برای سریع‌تر شدن فرایند خرید، یک آدرس جدید اضافه کنید.

)}
); }; export { AddressBook1 };