From 26b036d8c6982f6a424c049db9705417b0a892d9 Mon Sep 17 00:00:00 2001 From: NotII <46204250+NotII@users.noreply.github.com> Date: Thu, 20 Mar 2025 16:58:33 +0100 Subject: [PATCH] Update order-table.tsx --- components/tables/order-table.tsx | 100 ++++++++++++++++++------------ 1 file changed, 62 insertions(+), 38 deletions(-) diff --git a/components/tables/order-table.tsx b/components/tables/order-table.tsx index 9dd2c7f..6a54a20 100644 --- a/components/tables/order-table.tsx +++ b/components/tables/order-table.tsx @@ -129,6 +129,8 @@ export default function OrderTable() { const queryParams = new URLSearchParams({ page: currentPage.toString(), limit: itemsPerPage.toString(), + sortBy: sortConfig.column, + sortOrder: sortConfig.direction, ...(statusFilter !== "all" && { status: statusFilter }), }); @@ -143,7 +145,7 @@ export default function OrderTable() { } finally { setLoading(false); } - }, [currentPage, statusFilter, itemsPerPage]); + }, [currentPage, statusFilter, itemsPerPage, sortConfig]); useEffect(() => { fetchOrders(); @@ -165,38 +167,24 @@ export default function OrderTable() { }; // Derived data calculations - const filteredOrders = orders.filter( - (order) => statusFilter === "all" || order.status === statusFilter - ); - - const sortedOrders = [...filteredOrders].sort((a, b) => { - const aValue = a[sortConfig.column]; - const bValue = b[sortConfig.column]; - - if (typeof aValue === "string" && typeof bValue === "string") { - return sortConfig.direction === "asc" - ? aValue.localeCompare(bValue) - : bValue.localeCompare(aValue); - } - - if (typeof aValue === "number" && typeof bValue === "number") { - return sortConfig.direction === "asc" ? aValue - bValue : bValue - aValue; - } - - return 0; - }); - - const paginatedOrders = sortedOrders.slice( - (currentPage - 1) * itemsPerPage, - currentPage * itemsPerPage - ); + const filteredOrders = orders; + + // Use orders directly as they're already sorted by the server + const sortedOrders = filteredOrders; + const paginatedOrders = sortedOrders; // Handlers const handleSort = (column: SortableColumns) => { - setSortConfig(prev => ({ + const newDirection = sortConfig.column === column && sortConfig.direction === "asc" ? "desc" : "asc"; + console.log(`Setting sort config: ${column} ${newDirection}`); + + setSortConfig({ column, - direction: prev.column === column && prev.direction === "asc" ? "desc" : "asc" - })); + direction: newDirection + }); + + // Force a re-fetch when sorting changes + fetchOrders(); }; const toggleSelection = (orderId: string) => { @@ -365,24 +353,60 @@ export default function OrderTable() { onCheckedChange={toggleAll} /> - handleSort("orderId")}> - Order ID + handleSort("orderId")} + > +
+ Order ID + + {sortConfig.column === 'orderId' && ( + {sortConfig.direction} + )} +
- handleSort("totalPrice")}> - Total + handleSort("totalPrice")} + > +
+ Total + + {sortConfig.column === 'totalPrice' && ( + {sortConfig.direction} + )} +
- handleSort("status")}> - Status + handleSort("status")} + > +
+ Status + + {sortConfig.column === 'status' && ( + {sortConfig.direction} + )} +
- handleSort("orderDate")}> - Date + handleSort("orderDate")} + > +
+ Date + + {sortConfig.column === 'orderDate' && ( + {sortConfig.direction} + )} +
Buyer Actions - {orders.map((order) => { + {paginatedOrders.map((order) => { const StatusIcon = statusConfig[order.status as keyof typeof statusConfig]?.icon || XCircle; return (