"use client"; import { Building, Check, Home, Plus, Save, Star, 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 { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { cn } from "@/lib/utils"; interface Address { id: string; label: string; name: string; street: string; city: string; state: string; zip: string; country: string; isDefault: boolean; type: "home" | "work"; } const DEFAULT_ADDRESSES: Address[] = [ { id: "1", label: "خانه", name: "علی رضایی", street: "123 Main Street, Apt 4B", city: "New York", state: "NY", zip: "10001", country: "United States", isDefault: true, type: "home", }, { id: "2", label: "دفتر", name: "علی رضایی", street: "456 Business Ave, Suite 100", city: "New York", state: "NY", zip: "10018", country: "United States", isDefault: false, type: "work", }, ]; interface AddressBook2Props { addresses?: Address[]; title?: string; className?: string; } const AddressBook2 = ({ addresses: initialAddresses = DEFAULT_ADDRESSES, title = "آدرس‌های ارسال", className, }: AddressBook2Props) => { 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 typeIcons = { home: , work: , }; 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.type && editForm.label ) { setAddresses((prev) => prev.map((addr) => addr.id === editingId ? ({ ...addr, ...editForm, } as Address) : addr, ), ); setEditingId(null); setEditForm({}); } }; const startAddingNew = () => { setIsAddingNew(true); setEditForm({ label: "", name: "", street: "", city: "", state: "", zip: "", country: "ایران", type: "home", isDefault: addresses.length === 0, }); }; const cancelAddingNew = () => { setIsAddingNew(false); setEditForm({}); }; const saveNewAddress = () => { if ( editForm.name && editForm.street && editForm.city && editForm.state && editForm.zip && editForm.country && editForm.type && editForm.label ) { const newAddress: Address = { id: Date.now().toString(), label: editForm.label, name: editForm.name, street: editForm.street, city: editForm.city, state: editForm.state, zip: editForm.zip, country: editForm.country, type: editForm.type as "home" | "work", isDefault: addresses.length === 0, }; setAddresses((prev) => [...prev, newAddress]); setIsAddingNew(false); setEditForm({}); if (addresses.length === 0) { setSelectedId(newAddress.id); } } }; return (
{/* Header */}

{title}

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

ویرایش آدرس

setEditForm({ ...editForm, label: e.target.value }) } onClick={(e) => e.stopPropagation()} />
setEditForm({ ...editForm, name: 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()} />
) : ( <> {/* Selected Indicator */} {selectedId === address.id && (
)} {/* Type Icon & Label */}
{typeIcons[address.type]}
{address.label} {address.isDefault && ( پیش‌فرض )}
{/* Address Details */}

{address.name}

{address.street}

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

{/* Actions */}
{!address.isDefault && ( )}
)}
))} {/* افزودن جدید Card */} {isAddingNew ? (

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

setEditForm({ ...editForm, label: e.target.value }) } />
setEditForm({ ...editForm, name: 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, }) } />
) : (

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

آدرس خود را برای ارسال سفارش‌ها ذخیره کنید.

)}
); }; export { AddressBook2 };