-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(refactor) Refactor usePatientPrograms hook to use SWR
Refactors the usePatientProgram hook to leverage an SWR hook for fetching patient programs. With this, we get: - Automatic caching and revalidation - Conditional fetching based on the patientUuid and the `formJson.meta.programs.hasProgramFields` property of the form - Built-in error and loading states - Better handling of race conditions
- Loading branch information
1 parent
611e050
commit e9e89b7
Showing
3 changed files
with
52 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,38 @@ | ||
import useSWR from 'swr'; | ||
import { openmrsFetch, restBaseUrl } from '@openmrs/esm-framework'; | ||
import { useEffect, useState } from 'react'; | ||
import { type FormSchema, type PatientProgram } from '../types'; | ||
const customRepresentation = `custom:(uuid,display,program:(uuid,name,allWorkflows),dateEnrolled,dateCompleted,location:(uuid,display),states:(startDate,endDate,state:(uuid,name,retired,concept:(uuid),programWorkflow:(uuid)))`; | ||
import type { FormSchema, ProgramsFetchResponse } from '../types'; | ||
|
||
export const usePatientPrograms = (patientUuid: string, formJson: FormSchema) => { | ||
const [patientPrograms, setPatientPrograms] = useState<Array<PatientProgram>>([]); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
const useActiveProgramEnrollments = (patientUuid: string) => { | ||
const customRepresentation = `custom:(uuid,display,program:(uuid,name,allWorkflows),dateEnrolled,dateCompleted,location:(uuid,display),states:(startDate,endDate,state:(uuid,name,retired,concept:(uuid),programWorkflow:(uuid)))`; | ||
const apiUrl = `${restBaseUrl}/programenrollment?patient=${patientUuid}&v=${customRepresentation}`; | ||
|
||
const { data, error, isLoading } = useSWR<{ data: ProgramsFetchResponse }, Error>( | ||
patientUuid ? apiUrl : null, | ||
openmrsFetch, | ||
); | ||
|
||
const sortedEnrollments = | ||
data?.data?.results.length > 0 | ||
? data?.data.results.sort((a, b) => (b.dateEnrolled > a.dateEnrolled ? 1 : -1)) | ||
: null; | ||
|
||
useEffect(() => { | ||
if (formJson.meta?.programs?.hasProgramFields) { | ||
openmrsFetch(`${restBaseUrl}/programenrollment?patient=${patientUuid}&v=${customRepresentation}`) | ||
.then((response) => { | ||
setPatientPrograms(response.data.results.filter((enrollment) => enrollment.dateCompleted === null)); | ||
setIsLoading(false); | ||
}) | ||
.catch((error) => { | ||
setError(error); | ||
setIsLoading(false); | ||
}); | ||
} else { | ||
setIsLoading(false); | ||
} | ||
}, [formJson]); | ||
const activePrograms = sortedEnrollments?.filter((enrollment) => !enrollment.dateCompleted); | ||
|
||
return { | ||
patientPrograms, | ||
activePrograms, | ||
error, | ||
isLoading, | ||
}; | ||
}; | ||
|
||
export const usePatientPrograms = (patientUuid: string, formJson: FormSchema) => { | ||
const { activePrograms, error, isLoading } = useActiveProgramEnrollments( | ||
formJson.meta?.programs?.hasProgramFields ? patientUuid : null, | ||
); | ||
|
||
return { | ||
patientPrograms: activePrograms, | ||
errorFetchingPatientPrograms: error, | ||
isLoadingPatientPrograms: isLoading, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters