Skip to content

Commit 078850b

Browse files
[dev] [tofikwest] tofik/vendor-pagination-fix (#2040)
* refactor(ui): enhance pagination logic in VendorsTable for search functionality * refactor(ui): improve pagination logic in VendorsTable for clarity * refactor(ui): simplify comparison logic and improve pagination comments in VendorsTable --------- Co-authored-by: Tofik Hasanov <annexcies@gmail.com>
1 parent 025e9c6 commit 078850b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

apps/app/src/app/(app)/[orgId]/vendors/(overview)/components/VendorsTable.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,17 +327,24 @@ export function VendorsTable({
327327
const comparison = (aValue as string).localeCompare(bValue as string);
328328
return sort.desc ? -comparison : comparison;
329329
}
330-
const comparison =
331-
new Date(aValue as Date).getTime() - new Date(bValue as Date).getTime();
330+
const comparison = new Date(aValue as Date).getTime() - new Date(bValue as Date).getTime();
332331
return sort.desc ? -comparison : comparison;
333332
});
334333

335334
return result;
336335
}, [mergedVendors, searchQuery, sort]);
337336

338337
// Calculate pageCount from filtered data and paginate
339-
const filteredPageCount = Math.max(1, Math.ceil(filteredAndSortedVendors.length / perPage));
340-
const startIndex = (page - 1) * perPage;
338+
// When searching locally, calculate pageCount from filtered data
339+
// When not searching, use server's pageCount (server handles pagination)
340+
const filteredPageCount = searchQuery
341+
? Math.max(1, Math.ceil(filteredAndSortedVendors.length / perPage))
342+
: Math.max(1, vendorsData?.pageCount ?? initialPageCount);
343+
344+
// When searching locally, slice the data for client-side pagination
345+
// When not searching, server returns the correct page, but slice to enforce perPage
346+
// (avoids extra rows from onboarding pending/temp vendors)
347+
const startIndex = searchQuery ? (page - 1) * perPage : 0;
341348
const paginatedVendors = filteredAndSortedVendors.slice(startIndex, startIndex + perPage);
342349

343350
// Keep page in bounds when pageCount changes
@@ -358,9 +365,7 @@ export function VendorsTable({
358365
return metadataStatus === 'completed' || vendor.status === 'assessed';
359366
}).length;
360367

361-
const completedInMetadata = Object.values(itemStatuses).filter(
362-
(s) => s === 'completed',
363-
).length;
368+
const completedInMetadata = Object.values(itemStatuses).filter((s) => s === 'completed').length;
364369

365370
const total = Math.max(progress.total, itemsInfo.length, vendors.length);
366371
const completed = Math.max(completedCount, completedInMetadata);
@@ -420,9 +425,7 @@ export function VendorsTable({
420425
setVendorToDelete(null);
421426
} else {
422427
const errorMsg =
423-
typeof result?.data?.error === 'string'
424-
? result.data.error
425-
: 'Failed to delete vendor';
428+
typeof result?.data?.error === 'string' ? result.data.error : 'Failed to delete vendor';
426429
toast.error(errorMsg);
427430
}
428431
} catch {

0 commit comments

Comments
 (0)