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