mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-08-15 11:42:15 +01:00
add dynamic loading of paginated endpoints above page_limit
add request classes and fix collection typing for clients, tasks and tags
This commit is contained in:
22
resources/js/utils/fetchAllPages.ts
Normal file
22
resources/js/utils/fetchAllPages.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Fetches all pages from a paginated Laravel API endpoint.
|
||||
* Uses `meta.last_page` to determine the total number of pages,
|
||||
* so only a single request is made when all data fits on one page.
|
||||
*/
|
||||
export async function fetchAllPages<T>(
|
||||
fetchPage: (page: number) => Promise<{
|
||||
data: T[];
|
||||
meta: { per_page: number; last_page: number };
|
||||
}>
|
||||
): Promise<T[]> {
|
||||
const firstResponse = await fetchPage(1);
|
||||
const allItems: T[] = [...firstResponse.data];
|
||||
const { last_page } = firstResponse.meta;
|
||||
|
||||
for (let page = 2; page <= last_page; page++) {
|
||||
const response = await fetchPage(page);
|
||||
allItems.push(...response.data);
|
||||
}
|
||||
|
||||
return allItems;
|
||||
}
|
||||
Reference in New Issue
Block a user