Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix): Add limit parameter to API calls to return full chunk results in useConcepts hook #458

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/hooks/useConcepts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react';
import useSWRInfinite from 'swr/infinite';
import { type FetchResponse, type OpenmrsResource, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import { type FetchResponse, openmrsFetch, type OpenmrsResource, restBaseUrl } from '@openmrs/esm-framework';
import { useRestApiMaxResults } from './useRestApiMaxResults';

type ConceptFetchResponse = FetchResponse<{ results: Array<OpenmrsResource> }>;

Expand All @@ -12,23 +13,21 @@ export function useConcepts(references: Set<string>): {
isLoading: boolean;
error: Error | undefined;
} {
const chunkSize = 100;
const { maxResults } = useRestApiMaxResults();
const totalCount = references.size;
const totalPages = Math.ceil(totalCount / chunkSize);
const totalPages = Math.ceil(totalCount / maxResults);

const getUrl = (index, prevPageData: ConceptFetchResponse) => {
const getUrl = (index: number, prevPageData: ConceptFetchResponse) => {
if (index >= totalPages) {
return null;
}

if (!chunkSize) {
return null;
}

const start = index * chunkSize;
const end = start + chunkSize;
const start = index * maxResults;
const end = start + maxResults;
const referenceChunk = Array.from(references).slice(start, end);
return `${restBaseUrl}/concept?references=${referenceChunk.join(',')}&v=${conceptRepresentation}`;
return `${restBaseUrl}/concept?references=${referenceChunk.join(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As defined in the PR description, the chunkSize must be fetched from the global properties, endpoint ${restBaseUrl}/systemsetting/<setting_name>

',',
)}&v=${conceptRepresentation}&limit=${maxResults}`;
};

const { data, error, isLoading } = useSWRInfinite<ConceptFetchResponse, Error>(getUrl, openmrsFetch, {
Expand Down
35 changes: 35 additions & 0 deletions src/hooks/useRestApiMaxResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useMemo } from 'react';
import useSWR from 'swr';
import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';

type GlobalPropertyResponse = FetchResponse<{
results: Array<{ property: string; value: string }>;
}>;

const DEFAULT_CHUNK_SIZE = 100;

export function useRestApiMaxResults() {
const { data, error, isLoading } = useSWR<GlobalPropertyResponse, Error>(
`${restBaseUrl}/systemsetting?q=webservices.rest.maxResultsAbsolute&v=custom:(property,value)`,
openmrsFetch,
);

const maxResults = useMemo(() => {
try {
const maxResultsValue = data?.data.results.find(
(prop) => prop.property === 'webservices.rest.maxResultsAbsolute',
)?.value;

const parsedValue = parseInt(maxResultsValue ?? '');
return !isNaN(parsedValue) && parsedValue > 0 ? parsedValue : DEFAULT_CHUNK_SIZE;
} catch {
return DEFAULT_CHUNK_SIZE;
}
}, [data]);

return {
maxResults,
error,
isLoading,
};
}
Loading