import React, { useState } from 'react'; import { View, ScrollView } from 'react-native'; import { Image } from 'expo-image'; import { useRouter } from 'expo-router'; import { MyFlatList, MyText, tw, useMarkDataFetchers, BottomDialog, theme, MyTouchableOpacity } from 'common-ui'; import { trpc } from '@/src/trpc-client'; import MaterialIcons from '@expo/vector-icons/MaterialIcons'; import dayjs from 'dayjs'; export default function DeliverySlots() { const router = useRouter(); const { data, isLoading, error, refetch } = trpc.user.slots.getSlotsWithProducts.useQuery(); const [selectedSlotForDialog, setSelectedSlotForDialog] = useState(null); useMarkDataFetchers(() => { refetch(); }); if (isLoading) { return ( null} ListHeaderComponent={() => ( Loading delivery slots... )} /> ); } if (error) { return ( null} ListHeaderComponent={() => ( Error loading delivery slots refetch()} style={tw`mt-4 bg-blue-500 px-4 py-2 rounded-lg`} > Retry )} /> ); } const slots = data?.slots || []; if (slots.length === 0) { return ( null} ListHeaderComponent={() => ( No upcoming delivery slots available Check back later for new delivery schedules )} /> ); } return ( <> item.id.toString()} // ListHeaderComponent={() => ( // // Delivery Slots // // Choose your preferred delivery time // // // )} renderItem={({ item: slot }) => ( {/* Slot Header */} {dayjs(slot.deliveryTime).format('ddd DD MMM, h:mm a')} Orders close by: {dayjs(slot.freezeTime).format('h:mm a')} {slot.products.length} items router.push(`/(drawer)/(tabs)/home/cart?slot=${slot.id}`)} style={tw`bg-pink-500 p-2 rounded-full`} > {/* Products List */} Available Products {slot.products.slice(0, 2).map((product) => ( router.push(`/(drawer)/(tabs)/home/product-detail/${product.id}`)} style={tw`bg-gray-50 rounded-lg p-3 flex-row items-center`} > {product.images && product.images.length > 0 ? ( ) : ( )} {product.name} ₹{product.price} {product.unit && `per ${product.unit}`} {product.isOutOfStock && ( Out of stock )} ))} {slot.products.length > 2 && ( setSelectedSlotForDialog(slot)} style={tw`bg-pink-50 rounded-lg p-3 flex-row items-center justify-center border border-pink-200`} > +{slot.products.length - 2} more products )} )} ListFooterComponent={() => } showsVerticalScrollIndicator={false} contentContainerStyle={tw`pt-2`} /> {/* Products Dialog */} setSelectedSlotForDialog(null)} > All Products - {dayjs(selectedSlotForDialog?.deliveryTime).format('ddd DD MMM, h:mm a')} {selectedSlotForDialog?.products.map((product: any) => ( { setSelectedSlotForDialog(null); router.push(`/(drawer)/(tabs)/home/product-detail/${product.id}`); }} style={tw`bg-gray-50 rounded-lg p-4 flex-row items-center`} > {product.images && product.images.length > 0 ? ( ) : ( )} {product.name} ₹{product.price} {product.unit && `per ${product.unit}`} {product.marketPrice && ( ₹{product.marketPrice} )} {product.isOutOfStock && ( Out of stock )} ))} ); }