import React, { useState } from 'react' import { createFileRoute } from '@tanstack/react-router' import { CalendarClock, Truck, Snowflake, ShoppingBasket, RefreshCw } from 'lucide-react' import { p as MyText, MyFlatList, BottomDialog, Checkbox, div as MyTouchableOpacity } from 'web-components' import { trpc } from '@/lib/trpc-client' import dayjs from 'dayjs' export const Route = createFileRoute('/dashboard/rebalance')({ component: RebalanceOrders, }) interface SlotItemProps { item: any selectedSlots: number[] toggleSlotSelection: (slotId: number) => void setDialogProducts: React.Dispatch> setDialogOpen: React.Dispatch> } const SlotItemComponent: React.FC = ({ item: slot, selectedSlots, toggleSlotSelection, setDialogProducts, setDialogOpen, }) => { const isSelected = selectedSlots.includes(slot.id) const slotProducts = slot.products?.map((p: any) => p.name).filter(Boolean) || [] const displayProducts = slotProducts.slice(0, 2).join(', ') const isActive = slot.isActive const statusColor = isActive ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' const statusText = isActive ? 'Active' : 'Inactive' return (
toggleSlotSelection(slot.id)} role="button" tabIndex={0} onKeyDown={(e) => { if (e.key === 'Enter') toggleSlotSelection(slot.id) }} className="mb-4 cursor-pointer rounded-3xl border border-gray-100 bg-white p-5 text-left shadow-sm" >
e.stopPropagation()}> toggleSlotSelection(slot.id)} size={24} />
Slot #{slot.id} ID: {slot.id}
{statusText}
Delivery
{dayjs(slot.deliveryTime).format('DD MMM, h:mm A')}
Freeze
{dayjs(slot.freezeTime).format('DD MMM, h:mm A')}
{slotProducts.length > 0 ? (
Products
{displayProducts} {slotProducts.length > 2 && ( )}
) : null}
) } function RebalanceOrders() { const [selectedSlots, setSelectedSlots] = useState([]) const [dialogOpen, setDialogOpen] = useState(false) const [dialogProducts, setDialogProducts] = useState([]) const [refreshing, setRefreshing] = useState(false) const { data: slotsData, isLoading, refetch: refetchSlots } = trpc.admin.slots.getAll.useQuery() const upcomingSlots = slotsData?.slots?.filter(slot => dayjs(slot.deliveryTime).isAfter(dayjs())) || [] const handleRefresh = async () => { setRefreshing(true) await refetchSlots() setRefreshing(false) } const { mutate: rebalanceSlots } = trpc.admin.order.rebalanceSlots.useMutation({ onSuccess: () => { refetchSlots() }, onSettled: () => { window.alert('Rebalance Complete: Slots have been rebalanced.') } }) const toggleSlotSelection = (slotId: number) => { setSelectedSlots(prev => prev.includes(slotId) ? prev.filter(id => id !== slotId) : [...prev, slotId] ) } const handleRebalance = () => { window.alert('Rebalancing... Please wait while we rebalance the selected slots.') rebalanceSlots({ slotIds: selectedSlots }) } if (isLoading) { return (
Loading slots...
) } return (
Rebalance Upcoming Slots {selectedSlots.length > 0 && ( {selectedSlots.length} slot{selectedSlots.length === 1 ? '' : 's'} selected )} {upcomingSlots.length === 0 ? (
No upcoming slots available for rebalancing.
) : ( item.id.toString()} renderItem={(item) => ( )} onRefresh={handleRefresh} refreshing={refreshing} ListEmptyComponent={
No upcoming slots available for rebalancing.
} /> )}
setDialogOpen(false)}>
All Products
{dialogProducts.map((item, index) => (
{item}
))}
) }