Skip to content

Commit a36cf75

Browse files
committed
remove overloading fetching of cohort
1 parent 322f742 commit a36cf75

5 files changed

Lines changed: 361 additions & 342 deletions

File tree

src/pages/system-admin/cohort-management/cohort-management.component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ const CohortManagement: React.FC<CohortManagementProps> = () => {
3838
</TabList>
3939
<TabPanels>
4040
<TabPanel>
41-
<CohortRegistration />
41+
{selectedIndex === 0 && <CohortRegistration />}
4242
</TabPanel>
4343
<TabPanel>
44-
<PatientExit />
44+
{selectedIndex === 1 && <PatientExit />}
4545
</TabPanel>
4646
</TabPanels>
4747
</Tabs>

src/pages/system-admin/cohort-management/cohort-management.resources.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import useSWR from 'swr';
12
import { openmrsFetch } from '@openmrs/esm-framework';
23
import {
34
CohortType,
@@ -7,50 +8,51 @@ import {
78
} from './cohort-management.types';
89

910
export function useCohortTypes() {
10-
const abortController = new AbortController();
11-
12-
const fetchCohortTypes = async () => {
13-
const response = await openmrsFetch('/ws/rest/v1/cohortm/cohorttype?v=default', {
14-
signal: abortController.signal,
15-
});
16-
return response.data.results;
11+
const apiUrl = '/ws/rest/v1/cohortm/cohorttype?v=default';
12+
const { data, error, isLoading } = useSWR<{ results: Array<CohortType> }, Error>(
13+
apiUrl,
14+
() => openmrsFetch(apiUrl).then((res) => res.json())
15+
);
16+
17+
return {
18+
cohortTypes: data?.results ?? [],
19+
isLoading,
20+
isError: error,
1721
};
18-
19-
return { fetchCohortTypes, abortController };
2022
}
2123

2224
export function useCohorts(cohortTypeUuid?: string) {
23-
const abortController = new AbortController();
24-
25-
const fetchCohorts = async () => {
26-
const url = cohortTypeUuid
27-
? `/ws/rest/v1/cohortm/cohort?v=custom:(name,uuid,description,voided,cohortType,startDate)&cohortType=${cohortTypeUuid}`
28-
: '/ws/rest/v1/cohortm/cohort?v=custom:(name,uuid,description,voided,cohortType,startDate)';
29-
30-
const response = await openmrsFetch(url, {
31-
signal: abortController.signal,
32-
});
33-
return response.data.results;
25+
const apiUrl = cohortTypeUuid
26+
? `/ws/rest/v1/cohortm/cohort?v=custom:(name,uuid,description,voided,cohortType,startDate)&cohortType=${cohortTypeUuid}`
27+
: null;
28+
29+
const { data, error, isLoading } = useSWR<{ results: Array<Cohort> }, Error>(
30+
apiUrl,
31+
apiUrl ? () => openmrsFetch(apiUrl).then((res) => res.json()) : null
32+
);
33+
34+
return {
35+
cohorts: data?.results ?? [],
36+
isLoading,
37+
isError: error,
3438
};
35-
36-
return { fetchCohorts, abortController };
3739
}
3840

3941
export function useCohortsWithMembers(cohortTypeUuid?: string) {
40-
const abortController = new AbortController();
41-
42-
const fetchCohortsWithMembers = async () => {
43-
const url = cohortTypeUuid
44-
? `/ws/rest/v1/cohortm/cohort?v=custom:(name,cohortMembers,voided)&cohortType=${cohortTypeUuid}`
45-
: '/ws/rest/v1/cohortm/cohort?v=custom:(name,cohortMembers,voided)';
46-
47-
const response = await openmrsFetch(url, {
48-
signal: abortController.signal,
49-
});
50-
return response.data.results;
42+
const apiUrl = cohortTypeUuid
43+
? `/ws/rest/v1/cohortm/cohort?v=custom:(name,cohortMembers,voided)&cohortType=${cohortTypeUuid}`
44+
: null;
45+
46+
const { data, error, isLoading } = useSWR<{ results: Array<CohortWithMembers> }, Error>(
47+
apiUrl,
48+
apiUrl ? () => openmrsFetch(apiUrl).then((res) => res.json()) : null
49+
);
50+
51+
return {
52+
cohortsWithMembers: data?.results ?? [],
53+
isLoading,
54+
isError: error,
5155
};
52-
53-
return { fetchCohortsWithMembers, abortController };
5456
}
5557

5658
export async function createCohort(cohortData: CohortFormData) {

0 commit comments

Comments
 (0)