import React, { useState, useMemo } from 'react'; import { View, Text, TouchableOpacity, ScrollView } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import { StackNavigationProp } from '@react-navigation/stack'; import { getStyles } from './checkoutAddressScreen.styles'; import { Header, StepProgress, PrimaryButton } from '@components'; import { useAppTheme } from '@theme'; import { useAppSelector } from '@store'; import { AddressLabel, CustomerAddress } from '@interfaces'; import { AppStackParamList } from '@navigation/appStack'; type CheckoutAddressNavProp = StackNavigationProp< AppStackParamList, 'CheckoutAddressScreen' >; const CHECKOUT_STEPS = [ { key: 'address', label: 'Address' }, { key: 'payment', label: 'Payment' }, { key: 'confirm', label: 'Confirm' }, ]; const LABEL_ICON: Record = { Home: '🏠', Work: '💼', Other: '📍', }; export const CheckoutAddressScreen: React.FC = () => { const { colors } = useAppTheme(); const styles = getStyles(colors); const navigation = useNavigation(); const { customerDetails } = useAppSelector(state => state.customerProfile); const addresses: CustomerAddress[] = customerDetails?.addresses ?? []; const defaultAddressId = useMemo(() => { const def = addresses.find(a => a.isDefault); return def?.id ?? addresses[0]?.id ?? null; }, [addresses]); const [selectedAddressId, setSelectedAddressId] = useState( defaultAddressId, ); const handleContinue = () => { if (!selectedAddressId) return; navigation.navigate('CheckoutPaymentScreen', { selectedAddressId, }); }; return (
navigation.goBack()} /> Deliver to {addresses.length === 0 && ( No saved addresses yet. )} {addresses.map(address => { const isSelected = selectedAddressId === address.id; return ( setSelectedAddressId(address.id)} > {LABEL_ICON[address.label] ?? '📍'} {address.label} {address.isDefault && ( DEFAULT )} {isSelected && } {address.houseNumber ? `${address.houseNumber}, ` : ''} {address.addressLine1} {!!address.landmark && ( Landmark: {address.landmark} )} {address.city}, {address.state} - {address.postalCode} 📞 {address.phone} ); })} { // navigation.navigate('AddAddressScreen'); }} > + Add New Address ); };