Skip to content

Commit d8f9d0b

Browse files
chibonghoibacher
andauthored
(fix) O3-4481 store function to mutate visits in visit store (#1348)
Co-authored-by: Ian <52504170+ibacher@users.noreply.github.com>
1 parent 28171d3 commit d8f9d0b

File tree

10 files changed

+280
-90
lines changed

10 files changed

+280
-90
lines changed

packages/framework/esm-api/src/shared-api-objects/visit-utils.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ export enum VisitStatus {
2525
export interface VisitStoreState {
2626
patientUuid: string | null;
2727
manuallySetVisitUuid: string | null;
28+
29+
/**
30+
* Stores a record of SWR mutate callbacks that should be called when
31+
* the Visit with the specified uuid is modified. The callbacks are keyed
32+
* by unique component IDs.
33+
*/
34+
mutateVisitCallbacks: {
35+
[componentId: string]: () => void;
36+
};
2837
}
2938

3039
export const defaultVisitCustomRepresentation =
@@ -39,9 +48,10 @@ export const defaultVisitCustomRepresentation =
3948
'attributes:(uuid,display,attributeType:(name,datatypeClassname,uuid),value),' +
4049
'location:(uuid,name,display))';
4150

42-
const initialState = getVisitLocalStorage() || {
51+
const initialState: VisitStoreState = getVisitSessionStorage() || {
4352
patientUuid: null,
4453
manuallySetVisitUuid: null,
54+
mutateVisitCallbacks: {},
4555
};
4656

4757
export function getVisitStore() {
@@ -53,16 +63,16 @@ export function setCurrentVisit(patientUuid: string, visitUuid: string) {
5363
}
5464

5565
getVisitStore().subscribe((state) => {
56-
setVisitLocalStorage(state);
66+
setVisitSessionStorage(state);
5767
});
5868

59-
function setVisitLocalStorage(value: VisitStoreState) {
60-
localStorage.setItem('openmrs:visitStoreState', JSON.stringify(value));
69+
function setVisitSessionStorage(value: VisitStoreState) {
70+
sessionStorage.setItem('openmrs:visitStoreState', JSON.stringify(value));
6171
}
6272

63-
function getVisitLocalStorage(): VisitStoreState | null {
73+
function getVisitSessionStorage(): VisitStoreState | null {
6474
try {
65-
return JSON.parse(localStorage.getItem('openmrs:visitStoreState') || 'null');
75+
return JSON.parse(sessionStorage.getItem('openmrs:visitStoreState') || 'null');
6676
} catch (e) {
6777
return null;
6878
}
@@ -79,7 +89,11 @@ export function saveVisit(payload: NewVisitPayload, abortController: AbortContro
7989
});
8090
}
8191

82-
export function updateVisit(uuid: string, payload: UpdateVisitPayload, abortController: AbortController) {
92+
export function updateVisit(
93+
uuid: string,
94+
payload: UpdateVisitPayload,
95+
abortController: AbortController,
96+
): Promise<FetchResponse<Visit>> {
8397
return openmrsFetch(`${restBaseUrl}/visit/${uuid}`, {
8498
signal: abortController.signal,
8599
method: 'POST',

0 commit comments

Comments
 (0)