freshyo/apps/admin-web/src/routes/dashboard.rebalance.tsx
shafi54 5e96f7a747 Revert "enh"
This reverts commit a724cd7bff.
2026-10-28 20:02:04 +05:30

248 lines
8.8 KiB
TypeScript

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<React.SetStateAction<any[]>>
setDialogOpen: React.Dispatch<React.SetStateAction<boolean>>
}
const SlotItemComponent: React.FC<SlotItemProps> = ({
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 (
<div
onClick={() => 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"
>
<div className="mb-4 flex-row flex items-center justify-between">
<div className="flex-row flex items-center">
<span className="mr-3" onClick={(e) => e.stopPropagation()}>
<Checkbox
checked={isSelected}
onPress={() => toggleSlotSelection(slot.id)}
size={24}
/>
</span>
<div
className="mr-3 flex h-10 w-10 items-center justify-center rounded-full"
style={{ backgroundColor: '#fff0f6' }}
>
<CalendarClock size={20} color="#F83758" />
</div>
<div>
<MyText className="text-lg font-bold text-gray-900">Slot #{slot.id}</MyText>
<MyText className="text-xs text-gray-500">ID: {slot.id}</MyText>
</div>
</div>
<div className="flex-row flex items-center">
<span className={`rounded-full px-3 py-1 ${statusColor.split(' ')[0]}`}>
<MyText className={`text-xs font-bold ${statusColor.split(' ')[1]}`}>{statusText}</MyText>
</span>
</div>
</div>
<div className="mb-4 h-[1px] bg-gray-100" />
<div className="flex-row flex flex-wrap">
<div className="mb-4 w-1/2 pr-2">
<div className="mb-1 flex-row flex items-center">
<Truck size={14} color="#6b7280" className="mr-1" />
<MyText className="text-xs font-semibold uppercase tracking-wider text-gray-500">Delivery</MyText>
</div>
<MyText className="text-sm font-medium text-gray-800">
{dayjs(slot.deliveryTime).format('DD MMM, h:mm A')}
</MyText>
</div>
<div className="mb-4 w-1/2 pl-2">
<div className="mb-1 flex-row flex items-center">
<Snowflake size={14} color="#6b7280" className="mr-1" />
<MyText className="text-xs font-semibold uppercase tracking-wider text-gray-500">Freeze</MyText>
</div>
<MyText className="text-sm font-medium text-gray-800">
{dayjs(slot.freezeTime).format('DD MMM, h:mm A')}
</MyText>
</div>
</div>
{slotProducts.length > 0 ? (
<div className="mt-1 rounded-xl bg-gray-50 p-3">
<div className="flex-row flex items-start">
<ShoppingBasket size={16} color="#4b5563" className="mr-2 mt-0.5" />
<div className="flex-1">
<MyText className="mb-0.5 text-xs text-gray-500">Products</MyText>
<div className="flex-row flex flex-wrap items-center">
<MyText className="text-sm leading-5 text-gray-800">{displayProducts}</MyText>
{slotProducts.length > 2 && (
<button
onClick={(e) => {
e.stopPropagation()
setDialogProducts(slotProducts)
setDialogOpen(true)
}}
>
<MyText className="ml-1 text-sm font-semibold" style={{ color: '#2E90FA' }}>
+{slotProducts.length - 2} more
</MyText>
</button>
)}
</div>
</div>
</div>
</div>
) : null}
</div>
)
}
function RebalanceOrders() {
const [selectedSlots, setSelectedSlots] = useState<number[]>([])
const [dialogOpen, setDialogOpen] = useState(false)
const [dialogProducts, setDialogProducts] = useState<any[]>([])
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 (
<div className="flex flex-1 items-center justify-center bg-white">
<MyText>Loading slots...</MyText>
</div>
)
}
return (
<div className="relative flex-1 bg-white">
<div className="flex-1 p-4">
<MyText className="mb-4 text-xl font-bold text-gray-900">Rebalance Upcoming Slots</MyText>
{selectedSlots.length > 0 && (
<MyText className="mb-4 text-sm text-gray-600">
{selectedSlots.length} slot{selectedSlots.length === 1 ? '' : 's'} selected
</MyText>
)}
{upcomingSlots.length === 0 ? (
<div className="flex flex-1 items-center justify-center">
<MyText className="text-lg text-gray-600">No upcoming slots available for rebalancing.</MyText>
</div>
) : (
<MyFlatList
data={upcomingSlots}
keyExtractor={(item) => item.id.toString()}
renderItem={(item) => (
<SlotItemComponent
item={item}
selectedSlots={selectedSlots}
toggleSlotSelection={toggleSlotSelection}
setDialogProducts={setDialogProducts}
setDialogOpen={setDialogOpen}
/>
)}
onRefresh={handleRefresh}
refreshing={refreshing}
ListEmptyComponent={
<div className="flex flex-1 items-center justify-center">
<MyText className="text-lg text-gray-600">No upcoming slots available for rebalancing.</MyText>
</div>
}
/>
)}
</div>
<MyTouchableOpacity
onClick={handleRebalance}
disabled={selectedSlots.length === 0}
activeOpacity={0.95}
style={{
position: 'fixed',
bottom: 32,
right: 24,
zIndex: 100,
opacity: selectedSlots.length === 0 ? 0.5 : 1,
}}
aria-label="Rebalance selected slots"
>
<span
className="flex h-16 w-16 items-center justify-center shadow-lg"
style={{
borderRadius: 24,
background: selectedSlots.length === 0
? 'linear-gradient(135deg, #9CA3AF, #6B7280)'
: 'linear-gradient(135deg, #F83758, #E91E63)',
}}
>
<RefreshCw size={32} color="white" />
</span>
</MyTouchableOpacity>
<BottomDialog open={dialogOpen} onClose={() => setDialogOpen(false)}>
<div className="p-4">
<MyText className="mb-4 text-lg font-bold">All Products</MyText>
<div className="max-h-80 overflow-y-auto">
{dialogProducts.map((item, index) => (
<div key={index} className="border-b border-gray-200 py-2">
<MyText className="text-base">{item}</MyText>
</div>
))}
</div>
</div>
</BottomDialog>
</div>
)
}