From c9b24bc4cf55ead0d911311bd8a7b0b1e47e54f5 Mon Sep 17 00:00:00 2001 From: Dennis Kigen Date: Wed, 23 Oct 2024 22:13:35 +0300 Subject: [PATCH] (refactor) Switch visit requests to use promises instead of observables Ports over requests to the Visit API to use promise-based fetch requests instead of observables. Promises are generally simpler to work with than observables, and this should make the code easier to understand. This change also makes the code more consistent with other requests to the REST API. I've also updated the `UpdateVisitPayload` type to be a partial of `NewVisitPayload`, as not all fields are required when updating a visit. We should only really need to provide the stop datetime when ending a visit, for example. --- .../src/shared-api-objects/visit-utils.ts | 65 ++++++++----------- .../esm-api/src/types/visit-resource.ts | 2 +- packages/framework/esm-framework/docs/API.md | 30 ++++----- .../esm-framework/docs/enums/VisitMode.md | 6 +- .../esm-framework/docs/enums/VisitStatus.md | 4 +- .../docs/interfaces/ConfigurableLinkProps.md | 2 +- .../docs/interfaces/VisitItem.md | 8 +-- .../docs/interfaces/VisitStoreState.md | 4 +- 8 files changed, 55 insertions(+), 66 deletions(-) diff --git a/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts b/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts index 729358a29..37c55066e 100644 --- a/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts +++ b/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts @@ -1,11 +1,27 @@ /** @module @category API */ -import type { Observable } from 'rxjs'; import { BehaviorSubject } from 'rxjs'; -import { take, map } from 'rxjs/operators'; -import { openmrsObservableFetch, restBaseUrl } from '../openmrs-fetch'; +import { openmrsFetch, restBaseUrl } from '../openmrs-fetch'; import type { FetchResponse, NewVisitPayload, UpdateVisitPayload, Visit } from '../types'; import { getGlobalStore } from '@openmrs/esm-state'; +export interface VisitItem { + mode: VisitMode; + visitData?: Visit; + status: VisitStatus; + anythingElse?: any; +} + +export enum VisitMode { + NEWVISIT = 'startVisit', + EDITVISIT = 'editVisit', + LOADING = 'loadingVisit', +} + +export enum VisitStatus { + NOTSTARTED = 'notStarted', + ONGOING = 'ongoing', +} + export const defaultVisitCustomRepresentation = 'custom:(uuid,display,voided,indication,startDatetime,stopDatetime,' + 'encounters:(uuid,display,encounterDatetime,' + @@ -27,6 +43,7 @@ const initialState = getVisitLocalStorage() || { patientUuid: null, manuallySetVisitUuid: null, }; + export function getVisitStore() { return getGlobalStore('visit', initialState); } @@ -55,26 +72,20 @@ export function getVisitsForPatient( patientUuid: string, abortController: AbortController, v?: string, -): Observable }>> { +): Promise }>> { const custom = v ?? defaultVisitCustomRepresentation; - return openmrsObservableFetch(`${restBaseUrl}/visit?patient=${patientUuid}&v=${custom}`, { + return openmrsFetch(`${restBaseUrl}/visit?patient=${patientUuid}&v=${custom}`, { signal: abortController.signal, method: 'GET', headers: { 'Content-type': 'application/json', }, - }) - .pipe(take(1)) - .pipe( - map((response: FetchResponse<{ results: Array }>) => { - return response; - }), - ); + }); } -export function saveVisit(payload: NewVisitPayload, abortController: AbortController): Observable> { - return openmrsObservableFetch(`${restBaseUrl}/visit`, { +export function saveVisit(payload: NewVisitPayload, abortController: AbortController): Promise> { + return openmrsFetch(`${restBaseUrl}/visit`, { signal: abortController.signal, method: 'POST', headers: { @@ -84,12 +95,8 @@ export function saveVisit(payload: NewVisitPayload, abortController: AbortContro }); } -export function updateVisit( - uuid: string, - payload: UpdateVisitPayload, - abortController: AbortController, -): Observable { - return openmrsObservableFetch(`${restBaseUrl}/visit/${uuid}`, { +export function updateVisit(uuid: string, payload: UpdateVisitPayload, abortController: AbortController) { + return openmrsFetch(`${restBaseUrl}/visit/${uuid}`, { signal: abortController.signal, method: 'POST', headers: { @@ -101,21 +108,3 @@ export function updateVisit( /** @deprecated */ export const getStartedVisit = new BehaviorSubject(null); - -export interface VisitItem { - mode: VisitMode; - visitData?: Visit; - status: VisitStatus; - anythingElse?: any; -} - -export enum VisitMode { - NEWVISIT = 'startVisit', - EDITVISIT = 'editVisit', - LOADING = 'loadingVisit', -} - -export enum VisitStatus { - NOTSTARTED = 'notStarted', - ONGOING = 'ongoing', -} diff --git a/packages/framework/esm-api/src/types/visit-resource.ts b/packages/framework/esm-api/src/types/visit-resource.ts index 26f4d1499..6d76b0e9b 100644 --- a/packages/framework/esm-api/src/types/visit-resource.ts +++ b/packages/framework/esm-api/src/types/visit-resource.ts @@ -13,7 +13,7 @@ export interface NewVisitPayload { }>; } -export type UpdateVisitPayload = NewVisitPayload & {}; +export type UpdateVisitPayload = Partial & {}; export interface Visit { uuid: string; diff --git a/packages/framework/esm-framework/docs/API.md b/packages/framework/esm-framework/docs/API.md index 8d9a4bf80..83f3d10f8 100644 --- a/packages/framework/esm-framework/docs/API.md +++ b/packages/framework/esm-framework/docs/API.md @@ -718,7 +718,7 @@ ___ ### UpdateVisitPayload -Ƭ **UpdateVisitPayload**: [`NewVisitPayload`](interfaces/NewVisitPayload.md) & {} +Ƭ **UpdateVisitPayload**: `Partial`<[`NewVisitPayload`](interfaces/NewVisitPayload.md)\> & {} #### Defined in @@ -1016,7 +1016,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:9](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L9) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:25](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L25) ___ @@ -1038,7 +1038,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:103](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L103) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:110](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L110) ___ @@ -2468,7 +2468,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:30](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L30) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:47](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L47) ___ @@ -2488,7 +2488,7 @@ ___ ### getVisitsForPatient -▸ **getVisitsForPatient**(`patientUuid`, `abortController`, `v?`): `Observable`<[`FetchResponse`](interfaces/FetchResponse.md)<{ `results`: [`Visit`](interfaces/Visit.md)[] }\>\> +▸ **getVisitsForPatient**(`patientUuid`, `abortController`, `v?`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<{ `results`: [`Visit`](interfaces/Visit.md)[] }\>\> #### Parameters @@ -2500,11 +2500,11 @@ ___ #### Returns -`Observable`<[`FetchResponse`](interfaces/FetchResponse.md)<{ `results`: [`Visit`](interfaces/Visit.md)[] }\>\> +`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<{ `results`: [`Visit`](interfaces/Visit.md)[] }\>\> #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:54](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L54) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:71](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L71) ___ @@ -2690,7 +2690,7 @@ ___ ### saveVisit -▸ **saveVisit**(`payload`, `abortController`): `Observable`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\> +▸ **saveVisit**(`payload`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<[`Visit`](interfaces/Visit.md)\>\> #### Parameters @@ -2701,11 +2701,11 @@ ___ #### Returns -`Observable`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\> +`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<[`Visit`](interfaces/Visit.md)\>\> #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:76](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L76) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:87](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L87) ___ @@ -2726,7 +2726,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:34](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L34) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:51](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L51) ___ @@ -2835,23 +2835,23 @@ ___ ### updateVisit -▸ **updateVisit**(`uuid`, `payload`, `abortController`): `Observable`<`any`\> +▸ **updateVisit**(`uuid`, `payload`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\> #### Parameters | Name | Type | | :------ | :------ | | `uuid` | `string` | -| `payload` | [`NewVisitPayload`](interfaces/NewVisitPayload.md) | +| `payload` | `Partial`<[`NewVisitPayload`](interfaces/NewVisitPayload.md)\> | | `abortController` | `AbortController` | #### Returns -`Observable`<`any`\> +`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\> #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:87](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L87) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:98](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L98) ___ diff --git a/packages/framework/esm-framework/docs/enums/VisitMode.md b/packages/framework/esm-framework/docs/enums/VisitMode.md index d4fdb34d3..e886b9eee 100644 --- a/packages/framework/esm-framework/docs/enums/VisitMode.md +++ b/packages/framework/esm-framework/docs/enums/VisitMode.md @@ -18,7 +18,7 @@ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:114](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L114) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L16) ___ @@ -28,7 +28,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:115](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L115) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:17](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L17) ___ @@ -38,4 +38,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:113](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L113) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:15](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L15) diff --git a/packages/framework/esm-framework/docs/enums/VisitStatus.md b/packages/framework/esm-framework/docs/enums/VisitStatus.md index ac160bf2a..3c4d26f9a 100644 --- a/packages/framework/esm-framework/docs/enums/VisitStatus.md +++ b/packages/framework/esm-framework/docs/enums/VisitStatus.md @@ -17,7 +17,7 @@ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:119](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L119) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:21](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L21) ___ @@ -27,4 +27,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:120](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L120) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:22](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L22) diff --git a/packages/framework/esm-framework/docs/interfaces/ConfigurableLinkProps.md b/packages/framework/esm-framework/docs/interfaces/ConfigurableLinkProps.md index 96e64b2c0..12ab22312 100644 --- a/packages/framework/esm-framework/docs/interfaces/ConfigurableLinkProps.md +++ b/packages/framework/esm-framework/docs/interfaces/ConfigurableLinkProps.md @@ -496,7 +496,7 @@ ___ ### aria-current -• `Optional` **aria-current**: `boolean` \| ``"true"`` \| ``"false"`` \| ``"page"`` \| ``"step"`` \| ``"location"`` \| ``"date"`` \| ``"time"`` +• `Optional` **aria-current**: `boolean` \| ``"location"`` \| ``"true"`` \| ``"false"`` \| ``"page"`` \| ``"step"`` \| ``"date"`` \| ``"time"`` Indicates the element that represents the current item within a container or set of related elements. diff --git a/packages/framework/esm-framework/docs/interfaces/VisitItem.md b/packages/framework/esm-framework/docs/interfaces/VisitItem.md index 7add1f769..697726994 100644 --- a/packages/framework/esm-framework/docs/interfaces/VisitItem.md +++ b/packages/framework/esm-framework/docs/interfaces/VisitItem.md @@ -19,7 +19,7 @@ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:109](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L109) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:11](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L11) ___ @@ -29,7 +29,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:106](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L106) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:8](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L8) ___ @@ -39,7 +39,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:108](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L108) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:10](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L10) ___ @@ -49,4 +49,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:107](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L107) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:9](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L9) diff --git a/packages/framework/esm-framework/docs/interfaces/VisitStoreState.md b/packages/framework/esm-framework/docs/interfaces/VisitStoreState.md index 098993301..7e80f1bbf 100644 --- a/packages/framework/esm-framework/docs/interfaces/VisitStoreState.md +++ b/packages/framework/esm-framework/docs/interfaces/VisitStoreState.md @@ -17,7 +17,7 @@ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:23](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L23) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:39](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L39) ___ @@ -27,4 +27,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:22](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L22) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:38](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L38)