Skip to content

Commit

Permalink
(refactor) Switch visit requests to use promises instead of observables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
denniskigen committed Oct 23, 2024
1 parent 129c4c5 commit c9b24bc
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 66 deletions.
65 changes: 27 additions & 38 deletions packages/framework/esm-api/src/shared-api-objects/visit-utils.ts
Original file line number Diff line number Diff line change
@@ -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,' +
Expand All @@ -27,6 +43,7 @@ const initialState = getVisitLocalStorage() || {
patientUuid: null,
manuallySetVisitUuid: null,
};

export function getVisitStore() {
return getGlobalStore<VisitStoreState>('visit', initialState);
}
Expand Down Expand Up @@ -55,26 +72,20 @@ export function getVisitsForPatient(
patientUuid: string,
abortController: AbortController,
v?: string,
): Observable<FetchResponse<{ results: Array<Visit> }>> {
): Promise<FetchResponse<{ results: Array<Visit> }>> {
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<Visit> }>) => {
return response;
}),
);
});
}

export function saveVisit(payload: NewVisitPayload, abortController: AbortController): Observable<FetchResponse<any>> {
return openmrsObservableFetch(`${restBaseUrl}/visit`, {
export function saveVisit(payload: NewVisitPayload, abortController: AbortController): Promise<FetchResponse<Visit>> {
return openmrsFetch(`${restBaseUrl}/visit`, {
signal: abortController.signal,
method: 'POST',
headers: {
Expand All @@ -84,12 +95,8 @@ export function saveVisit(payload: NewVisitPayload, abortController: AbortContro
});
}

export function updateVisit(
uuid: string,
payload: UpdateVisitPayload,
abortController: AbortController,
): Observable<any> {
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: {
Expand All @@ -101,21 +108,3 @@ export function updateVisit(

/** @deprecated */
export const getStartedVisit = new BehaviorSubject<VisitItem | null>(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',
}
2 changes: 1 addition & 1 deletion packages/framework/esm-api/src/types/visit-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface NewVisitPayload {
}>;
}

export type UpdateVisitPayload = NewVisitPayload & {};
export type UpdateVisitPayload = Partial<NewVisitPayload> & {};

export interface Visit {
uuid: string;
Expand Down
30 changes: 15 additions & 15 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ ___

### UpdateVisitPayload

Ƭ **UpdateVisitPayload**: [`NewVisitPayload`](interfaces/NewVisitPayload.md) & {}
Ƭ **UpdateVisitPayload**: `Partial`<[`NewVisitPayload`](interfaces/NewVisitPayload.md)\> & {}

#### Defined in

Expand Down Expand Up @@ -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)

___

Expand All @@ -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)

___

Expand Down Expand Up @@ -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)

___

Expand All @@ -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

Expand All @@ -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)

___

Expand Down Expand Up @@ -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

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand Down Expand Up @@ -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)

___

Expand Down
6 changes: 3 additions & 3 deletions packages/framework/esm-framework/docs/enums/VisitMode.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)
4 changes: 2 additions & 2 deletions packages/framework/esm-framework/docs/enums/VisitStatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions packages/framework/esm-framework/docs/interfaces/VisitItem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)

___

Expand All @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)

___

Expand All @@ -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)

0 comments on commit c9b24bc

Please sign in to comment.