From e2fc183636fa26d634ae2e52416b3761e0d207b1 Mon Sep 17 00:00:00 2001 From: Adam Moussa <166072409+amoussa1229@users.noreply.github.com> Date: Fri, 17 Apr 2026 12:50:23 -0400 Subject: [PATCH] Move sorting to server-side for full dataset ordering - usePaginatedList now manages sortBy/sortDir state - Sort params passed to fetch function and sent to API - SharedTable receives sort state as props instead of managing internally - Clicking column headers triggers API refetch with new sort - Removes client-side sorting that only worked on current page --- src/components/SharedTable.js | 33 ++++++++++++------------------ src/hooks/usePaginatedList.js | 10 +++++---- src/pages/workorders/list/List.js | 16 ++++++--------- src/pages/workorders/list/Table.js | 4 ++-- src/pages/workorders/list/api.js | 7 +++++-- 5 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/components/SharedTable.js b/src/components/SharedTable.js index bfa4b278..594949f8 100644 --- a/src/components/SharedTable.js +++ b/src/components/SharedTable.js @@ -87,14 +87,16 @@ const SharedTable = ({ setPageSize, totalCount, loading, - error + error, + sortBy, + setSortBy, + sortDir, + setSortDir }) => { const totalPages = Math.max(1, Math.ceil(totalCount / pageSize)); const [colWidths, setColWidths] = useState(null); const [columnOrder, setColumnOrder] = useState(() => columns.map((c) => c.key)); const [hiddenColumns, setHiddenColumns] = useState([]); - const [sortKey, setSortKey] = useState("lastUpdated"); - const [sortDir, setSortDir] = useState("desc"); const gridRef = useRef(null); useEffect(() => { @@ -166,25 +168,16 @@ const SharedTable = ({ }, [snapshotWidths, visibleColumns]); const handleSort = (key) => { - if (sortKey === key) { - setSortDir((d) => (d === "asc" ? "desc" : "asc")); + if (!setSortBy || !setSortDir) return; + if (sortBy === key) { + setSortDir(sortDir === "asc" ? "desc" : "asc"); } else { - setSortKey(key); + setSortBy(key); setSortDir("asc"); } + if (setPage) setPage(1); }; - const sortedRows = [...rows].sort((a, b) => { - if (!sortKey) return 0; - let aVal = a[sortKey] ?? ""; - let bVal = b[sortKey] ?? ""; - if (typeof aVal === "string") aVal = aVal.toLowerCase(); - if (typeof bVal === "string") bVal = bVal.toLowerCase(); - if (aVal < bVal) return sortDir === "asc" ? -1 : 1; - if (aVal > bVal) return sortDir === "asc" ? 1 : -1; - return 0; - }); - if (loading) return