mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-08 04:15:43 +00:00
refactor: migrated of the customer hooks for warehouse data fetching
This commit is contained in:
@@ -18,34 +18,14 @@ import {
|
||||
} from '@mantine/core';
|
||||
import { ChevronDown, ChevronRight, ClipboardCheck, Info, PackageSearch, Train, Truck } from 'lucide-react';
|
||||
|
||||
import { useMutation, useQuery } from '@tanstack/react-query';
|
||||
|
||||
import { api } from '@/services/api';
|
||||
import { useToast } from '@/hooks/use-toast';
|
||||
import {
|
||||
useAutoUnloadArrivedBookings,
|
||||
useBulkDispatchExport,
|
||||
useBulkMarkInspected,
|
||||
useBulkReceive,
|
||||
useEligibleBookings,
|
||||
useImportArriveQueue,
|
||||
useImportTrainItems,
|
||||
useImportUnloadedQueue,
|
||||
useLoadPassedExport,
|
||||
useLoadedExport,
|
||||
useReadyToLoadExport,
|
||||
useReceiveInventory,
|
||||
useWarehouseInventory,
|
||||
useWarehouseYards,
|
||||
useWarehouseZones,
|
||||
useWarehouses,
|
||||
} from '@/hooks/useWarehouses';
|
||||
import type {
|
||||
AutoUnloadArrivedResult,
|
||||
BulkDispatchResult,
|
||||
BulkInspectResult,
|
||||
BulkReceiveResult,
|
||||
ImportTrain,
|
||||
ImportTrainItem,
|
||||
ImportUnloadedItem,
|
||||
LoadPassedExportResult,
|
||||
ReadyToLoadRow,
|
||||
ReceiveInventoryPayload,
|
||||
} from '@/types/warehouse';
|
||||
@@ -77,9 +57,21 @@ function LocationSelects({
|
||||
value: Location;
|
||||
onChange: (next: Location) => void;
|
||||
}) {
|
||||
const warehousesQuery = useWarehouses({ status: 'ACTIVE' });
|
||||
const yardsQuery = useWarehouseYards(value.warehouseId || undefined);
|
||||
const zonesQuery = useWarehouseZones(value.yardId || undefined);
|
||||
const warehousesQuery = useQuery(
|
||||
api.warehouses.list.queryOptions({ input: { filter: { status: 'ACTIVE' } } }),
|
||||
);
|
||||
const yardsQuery = useQuery(
|
||||
api.warehouses.listYards.queryOptions({
|
||||
input: { warehouseId: value.warehouseId ?? '' },
|
||||
enabled: Boolean(value.warehouseId),
|
||||
}),
|
||||
);
|
||||
const zonesQuery = useQuery(
|
||||
api.warehouses.listZones.queryOptions({
|
||||
input: { yardId: value.yardId ?? '' },
|
||||
enabled: Boolean(value.yardId),
|
||||
}),
|
||||
);
|
||||
|
||||
const warehouseOptions = useMemo(
|
||||
() => (warehousesQuery.data ?? []).map((w) => ({ value: w.id, label: `${w.name} (${w.code})` })),
|
||||
@@ -148,10 +140,12 @@ function EligibleTab({
|
||||
onChanged?: () => void;
|
||||
}) {
|
||||
const { toast } = useToast();
|
||||
const { data: allRows = [], isLoading } = useEligibleBookings(enabled);
|
||||
const { data: allRows = [], isLoading } = useQuery(
|
||||
api.warehouses.eligibleBookings.queryOptions({ enabled }),
|
||||
);
|
||||
const rows = useMemo(() => allRows.filter((r) => r.direction === direction), [allRows, direction]);
|
||||
const bulkReceive = useBulkReceive();
|
||||
const loadPassed = useLoadPassedExport();
|
||||
const bulkReceive = useMutation(api.warehouses.bulkReceive.mutationOptions());
|
||||
const loadPassed = useMutation(api.warehouses.loadPassedExport.mutationOptions());
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set());
|
||||
|
||||
const locationReady = Boolean(location.warehouseId && location.yardId && location.zoneId);
|
||||
@@ -177,10 +171,7 @@ function EligibleTab({
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = (await bulkReceive.mutateAsync({ direction, ...location, bookingIds })) as {
|
||||
data: BulkReceiveResult;
|
||||
};
|
||||
const r = res.data;
|
||||
const r = await bulkReceive.mutateAsync({ direction, ...location, bookingIds });
|
||||
toast({
|
||||
title: `${r.receivedCount} received at ${direction === 'EXPORT' ? 'facility' : 'warehouse'}`,
|
||||
description: r.skippedCount ? `${r.skippedCount} skipped` : undefined,
|
||||
@@ -194,8 +185,7 @@ function EligibleTab({
|
||||
|
||||
const loadPassedExport = async () => {
|
||||
try {
|
||||
const res = (await loadPassed.mutateAsync(undefined)) as { data: LoadPassedExportResult };
|
||||
const r = res.data;
|
||||
const r = await loadPassed.mutateAsync(undefined);
|
||||
toast({
|
||||
title: `${r.loadedCount} loaded`,
|
||||
description: r.skippedCount ? `${r.skippedCount} skipped — inspection not passed` : undefined,
|
||||
@@ -353,8 +343,10 @@ function EligibleTab({
|
||||
/** Export items that passed inspection and are queued to be loaded onto a train. */
|
||||
function ReadyToLoadTab({ enabled, onChanged }: { enabled: boolean; onChanged?: () => void }) {
|
||||
const { toast } = useToast();
|
||||
const { data: rows = [], isLoading } = useReadyToLoadExport(enabled);
|
||||
const loadPassed = useLoadPassedExport();
|
||||
const { data: rows = [], isLoading } = useQuery(
|
||||
api.warehouses.readyToLoadExport.queryOptions({ enabled }),
|
||||
);
|
||||
const loadPassed = useMutation(api.warehouses.loadPassedExport.mutationOptions());
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set());
|
||||
|
||||
const allSelected = rows.length > 0 && selected.size === rows.length;
|
||||
@@ -369,8 +361,7 @@ function ReadyToLoadTab({ enabled, onChanged }: { enabled: boolean; onChanged?:
|
||||
|
||||
const autoLoad = async () => {
|
||||
try {
|
||||
const res = (await loadPassed.mutateAsync(undefined)) as { data: LoadPassedExportResult };
|
||||
const r = res.data;
|
||||
const r = await loadPassed.mutateAsync(undefined);
|
||||
toast({
|
||||
title: `${r.loadedCount} items loaded`,
|
||||
description: r.skippedCount ? `${r.skippedCount} skipped` : undefined,
|
||||
@@ -494,8 +485,12 @@ function LoadedExportTab({
|
||||
onChanged?: () => void;
|
||||
}) {
|
||||
const { toast } = useToast();
|
||||
const { data: rows = [], isLoading } = useLoadedExport(enabled);
|
||||
const bulkDispatch = useBulkDispatchExport();
|
||||
const { data: rows = [], isLoading } = useQuery(
|
||||
api.warehouses.loadedExport.queryOptions({ enabled }),
|
||||
);
|
||||
const bulkDispatch = useMutation(
|
||||
api.warehouses.bulkDispatchExport.mutationOptions(),
|
||||
);
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set());
|
||||
|
||||
const allSelected = rows.length > 0 && selected.size === rows.length;
|
||||
@@ -514,8 +509,7 @@ function LoadedExportTab({
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = (await bulkDispatch.mutateAsync(inventoryIds)) as { data: BulkDispatchResult };
|
||||
const r = res.data;
|
||||
const r = await bulkDispatch.mutateAsync(inventoryIds);
|
||||
toast({
|
||||
title: `${r.dispatchedCount} dispatched`,
|
||||
description: r.skippedCount ? `${r.skippedCount} skipped` : undefined,
|
||||
@@ -659,7 +653,12 @@ function LoadedExportTab({
|
||||
|
||||
/** Assigned bookings/items for an arrived import train (read-only detail view). */
|
||||
function ImportTrainDetailTable({ scheduleId }: { scheduleId: string }) {
|
||||
const { data: items = [], isLoading } = useImportTrainItems(scheduleId);
|
||||
const { data: items = [], isLoading } = useQuery(
|
||||
api.warehouses.importTrainItems.queryOptions({
|
||||
input: { scheduleId },
|
||||
enabled: Boolean(scheduleId),
|
||||
}),
|
||||
);
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
@@ -735,18 +734,19 @@ function ImportArriveQueueTab({
|
||||
onChanged?: () => void;
|
||||
}) {
|
||||
const { toast } = useToast();
|
||||
const { data: trains = [], isLoading } = useImportArriveQueue(enabled);
|
||||
const autoUnloadMutation = useAutoUnloadArrivedBookings();
|
||||
const { data: trains = [], isLoading } = useQuery(
|
||||
api.warehouses.importArriveQueue.queryOptions({ enabled }),
|
||||
);
|
||||
const autoUnloadMutation = useMutation(
|
||||
api.warehouses.autoUnloadArrivedBookings.mutationOptions(),
|
||||
);
|
||||
const [openId, setOpenId] = useState<string | null>(null);
|
||||
const [busyId, setBusyId] = useState<string | null>(null);
|
||||
|
||||
const autoUnload = async (train: ImportTrain) => {
|
||||
setBusyId(train.scheduleId);
|
||||
try {
|
||||
const res = (await autoUnloadMutation.mutateAsync(train.scheduleId)) as {
|
||||
data: AutoUnloadArrivedResult;
|
||||
};
|
||||
const r = res.data;
|
||||
const r = await autoUnloadMutation.mutateAsync(train.scheduleId);
|
||||
const extra = [
|
||||
r.skippedCount ? `${r.skippedCount} skipped` : '',
|
||||
r.failedCount ? `${r.failedCount} failed` : '',
|
||||
@@ -866,8 +866,12 @@ function ImportArriveQueueTab({
|
||||
*/
|
||||
function ImportUnloadedQueueTab({ enabled }: { enabled: boolean }) {
|
||||
const { toast } = useToast();
|
||||
const { data: rows = [], isLoading } = useImportUnloadedQueue(enabled);
|
||||
const inspectMutation = useBulkMarkInspected();
|
||||
const { data: rows = [], isLoading } = useQuery(
|
||||
api.warehouses.importUnloadedQueue.queryOptions({ enabled }),
|
||||
);
|
||||
const inspectMutation = useMutation(
|
||||
api.warehouses.bulkMarkInspected.mutationOptions(),
|
||||
);
|
||||
const [selected, setSelected] = useState<Set<string>>(new Set());
|
||||
const [inspectId, setInspectId] = useState<string | null>(null);
|
||||
|
||||
@@ -888,10 +892,7 @@ function ImportUnloadedQueueTab({ enabled }: { enabled: boolean }) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = (await inspectMutation.mutateAsync({ inventoryIds: [...selected] })) as {
|
||||
data: BulkInspectResult;
|
||||
};
|
||||
const r = res.data;
|
||||
const r = await inspectMutation.mutateAsync({ inventoryIds: [...selected] });
|
||||
toast({
|
||||
title: `${r.inspectedCount} marked inspected`,
|
||||
description: r.skippedCount ? `${r.skippedCount} skipped` : undefined,
|
||||
@@ -1033,8 +1034,10 @@ function ImportUnloadedQueueTab({ enabled }: { enabled: boolean }) {
|
||||
*/
|
||||
function ImportDispatchQueueTab({ enabled }: { enabled: boolean }) {
|
||||
const { toast } = useToast();
|
||||
const { data: items = [], isLoading } = useWarehouseInventory(
|
||||
enabled ? { status: 'READY_FOR_PICKUP' } : undefined,
|
||||
const { data: items = [], isLoading } = useQuery(
|
||||
api.warehouses.listInventory.queryOptions({
|
||||
input: { filter: enabled ? { status: 'READY_FOR_PICKUP' } : undefined },
|
||||
}),
|
||||
);
|
||||
|
||||
return (
|
||||
@@ -1155,7 +1158,9 @@ function SingleBookingReceiveModal({
|
||||
onReceived,
|
||||
}: ReceiveInventoryModalProps) {
|
||||
const { toast } = useToast();
|
||||
const receiveMutation = useReceiveInventory();
|
||||
const receiveMutation = useMutation(
|
||||
api.warehouses.receiveInventory.mutationOptions(),
|
||||
);
|
||||
const [selectedBooking, setSelectedBooking] = useState(bookingId ?? '');
|
||||
const [form, setForm] = useState<SingleFormState>({
|
||||
warehouseId: '',
|
||||
|
||||
Reference in New Issue
Block a user