-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9d3b82f
commit 3554f5c
Showing
9 changed files
with
151 additions
and
121 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
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
38 changes: 30 additions & 8 deletions
38
packages/esm-patient-chart-app/src/clinical-views/hooks/useLastEncounter.ts
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,27 +1,49 @@ | ||
import { openmrsFetch } from '@openmrs/esm-framework'; | ||
import { type OpenmrsEncounter } from '@openmrs/esm-patient-common-lib'; | ||
|
||
import useSWR from 'swr'; | ||
import { type Encounter } from '../utils/helpers'; | ||
|
||
export const encounterRepresentation = | ||
'custom:(uuid,encounterDatetime,encounterType,location:(uuid,name),' + | ||
'patient:(uuid,display,age,identifiers,person),encounterProviders:(uuid,provider:(uuid,name)),' + | ||
'obs:(uuid,obsDatetime,voided,groupMembers,concept:(uuid,display,name:(uuid,name)),value:(uuid,name:(uuid,name,display),' + | ||
'names:(uuid,conceptNameType,name,display))),form:(uuid,name))'; | ||
|
||
const cache = new Map(); | ||
|
||
export function useLastEncounter(patientUuid: string, encounterType: string) { | ||
const query = `encounterType=${encounterType}&patient=${patientUuid}&limit=1&order=desc&startIndex=0`; | ||
const endpointUrl = `/ws/rest/v1/encounter?${query}&v=${encounterRepresentation}`; | ||
const endpointUrl = | ||
patientUuid && encounterType ? `/ws/rest/v1/encounter?${query}&v=${encounterRepresentation}` : null; | ||
|
||
const cacheKey = endpointUrl; | ||
|
||
const { data, error, isValidating } = useSWR<{ data: { results: Array<OpenmrsEncounter> } }, Error>( | ||
endpointUrl, | ||
openmrsFetch, | ||
{ dedupingInterval: 5000, refreshInterval: 0 }, | ||
const { data, error, isValidating, isLoading } = useSWR<{ results: Array<Encounter> }, Error>( | ||
cacheKey, | ||
async (url) => { | ||
const cachedData = cache.get(url); | ||
if (cachedData) { | ||
return cachedData; | ||
} | ||
const response = await openmrsFetch(url); | ||
|
||
if (!response.ok) { | ||
throw new Error('Failed to fetch data'); | ||
} | ||
const responseData = await response.json(); | ||
cache.set(url, responseData); | ||
return responseData; | ||
}, | ||
{ | ||
dedupingInterval: 50000, | ||
refreshInterval: 0, | ||
}, | ||
); | ||
|
||
return { | ||
lastEncounter: data ? data?.data?.results.shift() : null, | ||
lastEncounter: data ? data.results?.length > 0 && data.results[0] : null, | ||
error, | ||
isLoading: !data && !error, | ||
isLoading, | ||
isValidating, | ||
}; | ||
} |
Oops, something went wrong.