Skip to content

Commit

Permalink
Merge pull request #764 from Health-Informatics-UoN/fix/763/paginatio…
Browse files Browse the repository at this point in the history
…n-limits

Fix update table list of columns limit
  • Loading branch information
AndyRae authored Jun 24, 2024
2 parents 387bda6 + e9eaf08 commit 177b1c9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
12 changes: 12 additions & 0 deletions app/next-client-app/api/scanreports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { revalidatePath } from "next/cache";
import request from "@/lib/api/request";
import { redirect } from "next/navigation";
import { fetchAllPages } from "@/lib/api/utils";

const fetchKeys = {
list: (filter?: string) =>
Expand Down Expand Up @@ -72,6 +73,17 @@ export async function getScanReportFields(
}
}

export async function getAllScanReportFields(
filter: string | undefined,
): Promise<ScanReportField[]> {
try {
return await fetchAllPages<ScanReportField>(fetchKeys.fields(filter));
} catch (error) {
console.warn("Failed to fetch data.");
return [];
}
}

export async function getScanReportValues(
filter: string | undefined,
): Promise<PaginatedResponse<ScanReportValue>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
BreadcrumbSeparator,
} from "@/components/ui/breadcrumb";
import {
getAllScanReportFields,
getScanReport,
getScanReportFields,
getScanReportPermissions,
getScanReportTable,
} from "@/api/scanreports";
Expand All @@ -26,21 +26,21 @@ interface UpdateTableProps {
export default async function UpdateTable({
params: { id, tableId },
}: UpdateTableProps) {
const defaultPageSize = 50;
const defaultParams = {
scan_report_table: tableId,
fields: "name,id",
page_size: defaultPageSize,
};
const combinedParams = { ...defaultParams };

const query = objToQuery(combinedParams);

const scanReportsFields = await getScanReportFields(query);
const shortenFields = scanReportsFields.results.map(
(item: ScanReportField) => ({
id: item.id,
name: item.name,
})
);
const scanReportsFields = await getAllScanReportFields(query);
const shortenFields = scanReportsFields.map((item: ScanReportField) => ({
id: item.id,
name: item.name,
}));
const scanReportsName = await getScanReport(id);
const table = await getScanReportTable(tableId);
const permissions = await getScanReportPermissions(id);
Expand Down
26 changes: 26 additions & 0 deletions app/next-client-app/lib/api/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import request from "./request";

/**
* Fetches all pages of paginated data from the specified url.
*
* Iterates through paginated data until all pages are fetched and concatenated.
*
* @template T - The type of data to be fetched
* @param {string} url - The url to fetch from
* @returns {Promise<T[]>} - A promise that resolves to an array of all fetched data
*/
export async function fetchAllPages<T>(url: string): Promise<T[]> {
let allResults: T[] = [];
let pageNumber = 1;

while (true) {
const urlWithPage = `${url}&p=${pageNumber}`;
const data: PaginatedResponse<T> = await request(urlWithPage);

allResults = allResults.concat(data.results);
pageNumber++;
if (!data.next) break;
}

return allResults;
}

0 comments on commit 177b1c9

Please sign in to comment.