1+ import useSWR from 'swr' ;
12import { openmrsFetch } from '@openmrs/esm-framework' ;
23import {
34 CohortType ,
@@ -7,50 +8,51 @@ import {
78} from './cohort-management.types' ;
89
910export 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
2224export 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
3941export 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
5658export async function createCohort ( cohortData : CohortFormData ) {
0 commit comments