-
Notifications
You must be signed in to change notification settings - Fork 217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(feat) O3-3759: Refactor usePatient hook to leverage useSWR #1156
Conversation
Size Change: -88.3 kB (-1.52%) Total Size: 5.72 MB
ℹ️ View Unchanged
|
packages/framework/esm-react-utils/src/openmrsComponentDecorator.tsx
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @denniskigen! Some complications to this. Hope it's not too much trouble.
@@ -1,10 +1,11 @@ | |||
/** @module @category API */ | |||
import useSWR from 'swr'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, as a rule, we want to keep SWR and any React-specific stuff out of the esm-api library if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use a different abstraction instead? Like useOpenmrsSwr
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's more a question of can we move the SWR-specific stuff just into the usePatient()
hook in react-utils?
packages/framework/esm-api/src/shared-api-objects/current-patient.ts
Outdated
Show resolved
Hide resolved
2a2c1c6
to
9af15f8
Compare
9af15f8
to
7575569
Compare
effbe25
to
9e48ff5
Compare
9e48ff5
to
a2a25ed
Compare
272c0c0
to
2bbafef
Compare
2bbafef
to
4336050
Compare
import type { SyncItem } from '@openmrs/esm-framework/src/internal'; | ||
import { | ||
fetchCurrentPatient, | ||
getFullSynchronizationItems, | ||
getSynchronizationItems, | ||
} from '@openmrs/esm-framework/src/internal'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just reordered the imports.
import { useMemo } from 'react'; | ||
import useSWR, { type SWRResponse } from 'swr'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise here.
import { fhirBaseUrl, openmrsFetch, type FetchConfig } from '../openmrs-fetch'; | ||
import { type FetchResponse } from '../types'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise here.
data: patient, | ||
error, | ||
isValidating, | ||
} = useSWR<NullablePatient, Error | null>(currentPatientUuid ? ['patient', currentPatientUuid] : null, () => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the array of keys here? From the doc, I think all keys in the array should be passed in as params in the fetcher function.
Should it be this instead?
useSWR(currentPatientUuid, (patientUuid) => fetchCurrentPatient(patientUuid!, {})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my testing, this doesn't work as well. More specifically, the deduplication aspect doesn't work at all. With the array key, we have more control over what constitutes a unique request. Per the docs, the cache key will be associated with the entire key
argument. In my implementation, the url
and token
are both tied to the cache key. I think that's why the deduplication works.
* (feat) useCurrentPatient should leverage useSWR * Commit API docs * Fixup * Increase framework test timeout to 20s * More fixes * Fix type annotations
I like this improvement @denniskigen @ibacher |
Requirements
feat
,fix
, orchore
, among others). See existing PR titles for inspiration.For changes to apps
If applicable
Summary
This PR refactors the
usePatient
hook to leverage theuseSWR
hook.useSWR
provides caching, deduping, and revalidation out of the box. Presently, multiple requests are made to theFHIR
Patient
resource when the user loads a patient chart (around 8 or so requests). By leveraging the deduplication capabilities of useSWR, we can whittle this down to a single request as demonstrated in the videos below.Screenshots
Current behaviour
Note how there's about 8 requests for the FHIR patient object:
current.mp4
New behaviour
The 8 or so requests have been whittled down to exactly 1:
new.mp4
Related Issue
Other