-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathuseConcepts.ts
53 lines (45 loc) · 1.89 KB
/
useConcepts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useMemo } from 'react';
import useSWRInfinite from 'swr/infinite';
import { type FetchResponse, openmrsFetch, type OpenmrsResource, restBaseUrl } from '@openmrs/esm-framework';
import { useRestApiMaxResults } from './useRestApiMaxResults';
type ConceptFetchResponse = FetchResponse<{ results: Array<OpenmrsResource> }>;
const conceptRepresentation =
'custom:(units,lowAbsolute,hiAbsolute,uuid,display,conceptClass:(uuid,display),answers:(uuid,display),conceptMappings:(conceptReferenceTerm:(conceptSource:(name),code)))';
export function useConcepts(references: Set<string>): {
concepts: Array<OpenmrsResource> | undefined;
isLoading: boolean;
error: Error | undefined;
} {
const { maxResults } = useRestApiMaxResults();
const totalCount = references.size;
const totalPages = Math.ceil(totalCount / maxResults);
const getUrl = (index: number, prevPageData: ConceptFetchResponse) => {
if (index >= totalPages) {
return null;
}
const start = index * maxResults;
const end = start + maxResults;
const referenceChunk = Array.from(references).slice(start, end);
return `${restBaseUrl}/concept?references=${referenceChunk.join(
',',
)}&v=${conceptRepresentation}&limit=${maxResults}`;
};
const { data, error, isLoading } = useSWRInfinite<ConceptFetchResponse, Error>(getUrl, openmrsFetch, {
initialSize: totalPages,
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false,
});
const results = useMemo(
() => ({
// data?.[0] check is added for tests, as response is undefined in tests
// hence the returned concepts are [undefined], which breaks the form-helper.ts
// As it cannot read `uuid` of `undefined`
concepts: data && data?.[0] ? [].concat(...data.map((res) => res?.data?.results)) : undefined,
error,
isLoading,
}),
[data, error, isLoading],
);
return results;
}