freshyo/apps/user-ui/components/CheckoutAddressDisplay.tsx
2026-10-09 00:58:12 +05:30

76 lines
2.9 KiB
TypeScript

import React from 'react'
import { View } from 'react-native'
import MaterialIcons from '@expo/vector-icons/MaterialIcons'
import { tw, MyText } from 'common-ui'
import { trpc } from '@/src/trpc-client'
import { useAuth } from '@/src/contexts/AuthContext'
import { useAddressStore } from '@/src/store/addressStore'
/**
* Read-only delivery address card for the checkout page. The address is chosen
* on the cart page (CheckoutAddressSelector); checkout only shows it.
*/
const CheckoutAddressDisplay: React.FC = () => {
const { isAuthenticated } = useAuth()
const selectedAddressId = useAddressStore((state) => state.selectedAddressId)
const { data: addressesData, isLoading } = trpc.user.address.getUserAddresses.useQuery(
undefined,
{ enabled: isAuthenticated }
)
const addresses = addressesData?.data ?? []
const address =
addresses.find((candidate) => candidate.id === selectedAddressId) ??
addresses.find((candidate) => candidate.isDefault) ??
null
return (
<View style={tw`bg-white p-5 rounded-2xl shadow-sm mb-4 border border-gray-100`}>
<View style={tw`flex-row items-center mb-3`}>
<View style={tw`w-8 h-8 bg-blue-50 rounded-full items-center justify-center mr-3`}>
<MaterialIcons name="location-on" size={18} color="#3B82F6" />
</View>
<MyText style={tw`text-base font-bold text-gray-900`}>Delivery Address</MyText>
</View>
{isLoading ? (
<MyText style={tw`text-gray-500`}>Loading address...</MyText>
) : address ? (
<View style={tw`bg-gray-50 p-4 rounded-xl border border-gray-200`}>
<View style={tw`flex-row items-center mb-2`}>
<MaterialIcons
name={
address.name.toLowerCase().includes('home')
? 'home'
: address.name.toLowerCase().includes('work')
? 'work'
: 'location-on'
}
size={20}
color="#6B7280"
/>
<MyText style={tw`font-bold ml-2 text-gray-900`}>{address.name}</MyText>
</View>
<MyText style={tw`text-gray-600 text-sm leading-5 mb-1`} numberOfLines={2}>
{address.addressLine1}
{address.addressLine2 ? `, ${address.addressLine2}` : ''}
</MyText>
<MyText style={tw`text-gray-600 text-sm mb-1`}>
{address.city}, {address.state} - {address.pincode}
</MyText>
<MyText style={tw`text-gray-500 text-xs mt-2`}>Phone: {address.phone}</MyText>
</View>
) : (
<View
style={tw`bg-gray-50 p-6 rounded-xl border border-gray-200 border-dashed items-center justify-center`}
>
<MaterialIcons name="location-off" size={32} color="#9CA3AF" />
<MyText style={tw`text-gray-500 mt-2`}>No address selected</MyText>
</View>
)}
</View>
)
}
export default CheckoutAddressDisplay