import { createFileRoute } from '@tanstack/react-router' import { trpc } from '../lib/trpc-client' import { useState } from 'react' import { MeScreenHeader } from '../components/me/MeScreenHeader' import { AddressForm } from '../components/AddressForm' import { BottomDialog } from 'web-components' import type { UserAddress } from '@packages/shared' import { MapPin, Plus, AlertTriangle } from 'lucide-react' export const Route = createFileRoute('/me/addresses')({ component: AddressesPage }) // Address view — required core of the shared UserAddress + optional geo fields type Address = Pick< UserAddress, 'id' | 'regionId' | 'name' | 'phone' | 'addressLine1' | 'addressLine2' | 'city' | 'state' | 'isDefault' > & { latitude?: number | null longitude?: number | null googleMapsUrl?: string | null } const formatAddress = (addr: Address) => { return `${addr.addressLine1}${addr.addressLine2 ? `, ${addr.addressLine2}` : ''}, ${addr.city}, ${addr.state}` } function AddressCard({ address, onEdit, onDelete, onSetDefault, isDeleting, }: { address: Address onEdit: (address: Address) => void onDelete: (id: number) => void onSetDefault: (id: number) => void isDeleting?: boolean }) { return (

{address.name}

{address.isDefault && (

Default

)}

📞 {address.phone}

📍 {formatAddress(address)}

{!address.isDefault && ( )}
) } function AddressesPage() { const [modalVisible, setModalVisible] = useState(false) const [editingAddress, setEditingAddress] = useState
(null) const [deletingId, setDeletingId] = useState(null) const { data, isLoading, error, refetch } = trpc.user.address.getUserAddresses.useQuery() const addresses = (data?.data || []) as Address[] const updateAddressMutation = trpc.user.address.updateAddress.useMutation() const deleteAddressMutation = trpc.user.address.deleteAddress.useMutation() const handleAddAddress = () => { setEditingAddress(null) setModalVisible(true) } const handleEditAddress = (address: Address) => { setEditingAddress(address) setModalVisible(true) } const handleDeleteAddress = (id: number) => { const ok = window.confirm('Are you sure you want to delete this address?') if (!ok) return setDeletingId(id) deleteAddressMutation.mutate( { id }, { onSuccess: () => { setDeletingId(null) refetch() alert('Address deleted successfully') }, onError: (error: any) => { setDeletingId(null) alert(error.message || 'Failed to delete address') }, } ) } const handleSetDefault = (id: number) => { const addressToUpdate = addresses.find((addr) => addr.id === id) if (!addressToUpdate) return if (addressToUpdate.regionId == null) { alert('This address has no region. Please edit it and choose a region first.') return } updateAddressMutation.mutate( { id: addressToUpdate.id, regionId: addressToUpdate.regionId, name: addressToUpdate.name, phone: addressToUpdate.phone, addressLine1: addressToUpdate.addressLine1, addressLine2: addressToUpdate.addressLine2 || undefined, city: addressToUpdate.city, state: addressToUpdate.state, isDefault: true, // Keep the stored coordinates so the bound is not re-derived from a fallback. latitude: addressToUpdate.latitude ?? undefined, longitude: addressToUpdate.longitude ?? undefined, }, { onSuccess: () => { refetch() alert('Default address updated') }, onError: (error: any) => { alert(error.message || 'Failed to update default address') }, } ) } const handleAddressSubmit = () => { setModalVisible(false) setEditingAddress(null) refetch() } return (
{isLoading ? (

Loading addresses...

) : error ? (

Failed to load addresses. Please try again.

) : (
{/* Header row */}

My Addresses

{addresses.length === 0 ? (

No addresses found

Add your first delivery address

) : (
{addresses.map((address) => ( ))}
)}
)} {/* Add/Edit bottom sheet */} setModalVisible(false)}>
) }