import React, { useState } from 'react'; import { View, ScrollView, Dimensions } from 'react-native'; import { useRouter } from 'expo-router'; import MaterialIcons from '@expo/vector-icons/MaterialIcons'; import { BottomDialog, MyTouchableOpacity, MyText, tw, theme } from 'common-ui'; import { useAuth } from '@/src/contexts/AuthContext'; import { trpc } from '@/src/trpc-client'; import { useAddressStore } from '@/src/store/addressStore'; import dayjs from 'dayjs'; interface QuickDeliveryAddressSelectorProps { deliveryTime?: string; slotId?: number; onSlotChange?: (slotId: number) => void; isForFlashDelivery?: boolean; } const QuickDeliveryAddressSelector: React.FC = ({ deliveryTime, slotId, onSlotChange, isForFlashDelivery = false }) => { const router = useRouter(); const [dialogOpen, setDialogOpen] = useState(false); const { isAuthenticated } = useAuth(); const { selectedAddressId, setSelectedAddressId } = useAddressStore(); const { data: defaultAddressData } = trpc.user.address.getDefaultAddress.useQuery(); const { data: addressesData } = trpc.user.address.getUserAddresses.useQuery(undefined, { enabled: isAuthenticated, }); const { data: slotsData } = trpc.user.slots.getSlotsWithProducts.useQuery(); const defaultAddress = defaultAddressData?.data; const addresses = addressesData?.data || []; // Find earliest slot for pre-selection const earliestSlot = slotsData?.slots?.sort((a, b) => dayjs(a.deliveryTime).diff(dayjs(b.deliveryTime)) )[0]; // Transform addresses for display const addressOptions = addresses.map(address => ({ id: address.id, name: address.name, address: `${address.addressLine1}${address.addressLine2 ? `, ${address.addressLine2}` : ''}, ${address.city}, ${address.state} - ${address.pincode}`, phone: address.phone, })); // Transform slots for display const slotOptions = slotsData?.slots?.map(slot => ({ id: slot.id, deliveryTime: dayjs(slot.deliveryTime).format('MMM DD, h:mm A'), closeTime: dayjs(slot.freezeTime).format('h:mm A'), })) || []; // Get current selected slot display const getCurrentSlotDisplay = () => { if (isForFlashDelivery) return '30 minutes'; if (slotId) { const slot = slotsData?.slots?.find(s => s.id === slotId); return slot ? dayjs(slot.deliveryTime).format('MMM DD, h:mm A') : 'Select time'; } if (earliestSlot) { return dayjs(earliestSlot.deliveryTime).format('MMM DD, h:mm A'); } return 'Select time'; }; // Get current selected address display const getCurrentAddressDisplay = () => { if (selectedAddressId) { const address = addresses.find(a => a.id === selectedAddressId); if (address) return `${address.name} - ${address.addressLine1}`; } if (defaultAddress) { return `${defaultAddress.name} - ${defaultAddress.addressLine1}`; } return 'Select address'; }; return ( <> {isForFlashDelivery && ( router.back()} style={tw`p-2 -ml-2`} activeOpacity={0.7} > Delivery in 30 minutes )} {!isForFlashDelivery && ( router.back()} style={tw`p-2 -ml-2`} activeOpacity={0.7} > Delivery At {getCurrentSlotDisplay()} {/* Trigger Component with Separate Chevrons */} /* Regular Delivery Time Section */ setDialogOpen(true)} style={tw`flex-row items-center justify-between mb-2`} activeOpacity={0.7} > {/* Delivery Time */} Delivery at: {getCurrentSlotDisplay()} {/* Address Section */} setDialogOpen(true)} style={tw`flex-row items-center justify-between`} activeOpacity={0.7} > {/* Delivery Address */} TO: {getCurrentAddressDisplay()} {/* Consolidated Dialog - 80% height */} setDialogOpen(false)} > Select Delivery Options {/* Section 1: Delivery Time Selection */} Select Delivery Time {isForFlashDelivery ? ( Flash Delivery - 30 minutes Selected automatically for flash delivery ) : ( slotOptions.map(slot => ( { onSlotChange?.(slot.id); setDialogOpen(false); }} activeOpacity={0.7} > Delivery: {slot.deliveryTime} Orders Close at: {slot.closeTime} )) )} {/* Divider */} {/* Section 2: Address Selection */} Select Delivery Address {!isAuthenticated ? ( Authentication Required Please log in to select and manage delivery addresses. ) : addressOptions.length === 0 ? ( No delivery addresses available. Please add an address first. ) : ( addressOptions.map(address => ( { setSelectedAddressId(address.id); setDialogOpen(false); }} activeOpacity={0.7} > {address.name} {address.address} Phone: {address.phone} )) )} )} ); }; export default QuickDeliveryAddressSelector;