diff --git a/packages/apps/esm-primary-navigation-app/src/components/choose-locale/change-locale.component.test.tsx b/packages/apps/esm-primary-navigation-app/src/components/choose-locale/change-locale.component.test.tsx index cf887c23f..d374f1230 100644 --- a/packages/apps/esm-primary-navigation-app/src/components/choose-locale/change-locale.component.test.tsx +++ b/packages/apps/esm-primary-navigation-app/src/components/choose-locale/change-locale.component.test.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { fireEvent, render, screen, wait } from "@testing-library/react"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import ChangeLocale from "./change-locale.component"; const allowedLocales = ["en", "fr", "it", "pt"]; @@ -32,7 +32,7 @@ describe(``, () => { fireEvent.change(screen.getByLabelText(/Select locale/i), { target: { value: "en" }, }); - await wait(() => + await waitFor(() => expect(postUserPropertiesMock).toHaveBeenCalledWith( user.uuid, { defaultLocale: "en" }, diff --git a/packages/apps/esm-primary-navigation-app/src/root.component.test.tsx b/packages/apps/esm-primary-navigation-app/src/root.component.test.tsx index a28051492..8a6876c91 100644 --- a/packages/apps/esm-primary-navigation-app/src/root.component.test.tsx +++ b/packages/apps/esm-primary-navigation-app/src/root.component.test.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { fireEvent, render, screen, wait } from "@testing-library/react"; +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import { of } from "rxjs"; import { isDesktop } from "./utils"; import { mockUser } from "../__mocks__/mock-user"; @@ -8,7 +8,6 @@ import Root from "./root.component"; const mockUserObservable = of(mockUser); const mockSessionObservable = of({ data: mockSession }); - jest.mock("@openmrs/esm-framework", () => ({ openmrsFetch: jest.fn().mockResolvedValue({}), useAssignedExtensions: jest.fn().mockReturnValue([]), @@ -72,7 +71,7 @@ describe(``, () => { }); it("does not render side menu button if desktop", async () => { - await wait(() => + await waitFor(() => expect(screen.queryAllByLabelText("Open menu")).toHaveLength(0) ); }); 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 3793b5b37..b283356d5 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 @@ -13,19 +13,20 @@ import { Visit, } from "../types"; +export const defaultVisitCustomRepresentation = + "custom:(uuid,encounters:(uuid,encounterDatetime," + + "form:(uuid,name),location:ref," + + "encounterType:ref,encounterProviders:(uuid,display," + + "provider:(uuid,display))),patient:(uuid,uuid)," + + "visitType:(uuid,name,display),attributes:(uuid,display,value),location:(uuid,name,display),startDatetime," + + "stopDatetime)"; + export function getVisitsForPatient( patientUuid: string, abortController: AbortController, v?: string ): Observable }>> { - const custom = - v || - "custom:(uuid,encounters:(uuid,encounterDatetime," + - "form:(uuid,name),location:ref," + - "encounterType:ref,encounterProviders:(uuid,display," + - "provider:(uuid,display))),patient:(uuid,uuid)," + - "visitType:(uuid,name,display),attributes:(uuid,display,value),location:(uuid,name,display),startDatetime," + - "stopDatetime)"; + const custom = v ?? defaultVisitCustomRepresentation; return openmrsObservableFetch( `/ws/rest/v1/visit?patient=${patientUuid}&v=${custom}`, diff --git a/packages/framework/esm-framework/docs/API.md b/packages/framework/esm-framework/docs/API.md index bb9747160..cf97e07c9 100644 --- a/packages/framework/esm-framework/docs/API.md +++ b/packages/framework/esm-framework/docs/API.md @@ -501,6 +501,16 @@ ___ ## API Variables +### defaultVisitCustomRepresentation + +• **defaultVisitCustomRepresentation**: `string` + +#### Defined in + +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L16) + +___ + ### fhir • **fhir**: `FhirClient` @@ -532,7 +542,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:77](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L77) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:78](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L78) ___ @@ -877,7 +887,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L16) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:24](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L24) ___ @@ -1062,7 +1072,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:48](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L48) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:49](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L49) ___ @@ -1145,7 +1155,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:62](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L62) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:63](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L63) ___ @@ -4257,26 +4267,27 @@ ___ ### useVisit -▸ **useVisit**(`patientUuid`): `Object` +▸ **useVisit**(`patientUuid`): `VisitReturnType` + +This React hook returns a visit object. If the `patientUuid` is provided +as a parameter, then the currentVisit, error and mutate function +for that patient visit is returned. #### Parameters -| Name | Type | -| :------ | :------ | -| `patientUuid` | `string` | +| Name | Type | Description | +| :------ | :------ | :------ | +| `patientUuid` | `string` | Unique patient identifier `string` | #### Returns -`Object` +`VisitReturnType` -| Name | Type | -| :------ | :------ | -| `currentVisit` | ``null`` \| [`Visit`](interfaces/Visit.md) | -| `error` | ``null`` | +Object {`error` `isValidating`, `currentVisit`, `mutate`} #### Defined in -[packages/framework/esm-react-utils/src/useVisit.ts:11](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-react-utils/src/useVisit.ts#L11) +[packages/framework/esm-react-utils/src/useVisit.ts:22](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-react-utils/src/useVisit.ts#L22) ___ diff --git a/packages/framework/esm-framework/docs/enums/VisitMode.md b/packages/framework/esm-framework/docs/enums/VisitMode.md index 7f90a5643..6f78c4a34 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:88](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L88) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:89](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L89) ___ @@ -28,7 +28,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:89](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L89) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:90](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L90) ___ @@ -38,4 +38,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:87](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L87) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:88](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L88) diff --git a/packages/framework/esm-framework/docs/enums/VisitStatus.md b/packages/framework/esm-framework/docs/enums/VisitStatus.md index a80e609e7..45b625868 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:93](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L93) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:94](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L94) ___ @@ -27,4 +27,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:94](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L94) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:95](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L95) diff --git a/packages/framework/esm-framework/docs/interfaces/VisitItem.md b/packages/framework/esm-framework/docs/interfaces/VisitItem.md index 591390f77..7006e8bc4 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:83](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L83) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:84](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L84) ___ @@ -29,7 +29,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:80](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L80) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:81](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L81) ___ @@ -39,7 +39,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:82](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L82) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:83](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L83) ___ @@ -49,4 +49,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:81](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L81) +[packages/framework/esm-api/src/shared-api-objects/visit-utils.ts:82](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/visit-utils.ts#L82) diff --git a/packages/framework/esm-react-utils/src/useVisit.ts b/packages/framework/esm-react-utils/src/useVisit.ts index f8f4b4b90..d05ed4368 100644 --- a/packages/framework/esm-react-utils/src/useVisit.ts +++ b/packages/framework/esm-react-utils/src/useVisit.ts @@ -1,49 +1,34 @@ -import { useState, useEffect } from "react"; -import dayjs from "dayjs"; import { - getVisitsForPatient, - getStartedVisit, - VisitMode, - VisitStatus, + defaultVisitCustomRepresentation, + openmrsFetch, Visit, } from "@openmrs/esm-api"; +import useSWR from "swr"; -export function useVisit(patientUuid: string) { - const [currentVisit, setCurrentVisit] = useState(null); - const [error, setError] = useState(null); - - useEffect(() => { - const sub = getStartedVisit.subscribe((visit) => { - if (visit) { - setCurrentVisit(visit?.visitData ?? null); - } else { - setCurrentVisit(null); - } - }); - - return () => sub.unsubscribe(); - }, [patientUuid]); +interface VisitReturnType { + error: Error; + mutate: () => void; + isValidating: boolean; + currentVisit: Visit | null; +} - useEffect(() => { - const abortController = new AbortController(); - const sub = getVisitsForPatient(patientUuid, abortController).subscribe( - ({ data }) => { - const currentVisit = data.results.find( - (visit) => visit.stopDatetime === null - ); +/** + * This React hook returns a visit object. If the `patientUuid` is provided + * as a parameter, then the currentVisit, error and mutate function + * for that patient visit is returned. + * @param patientUuid Unique patient identifier `string` + * @returns Object {`error` `isValidating`, `currentVisit`, `mutate`} + */ +export function useVisit(patientUuid: string): VisitReturnType { + const { data, error, mutate, isValidating } = useSWR<{ + data: { results: Array }; + }>( + `/ws/rest/v1/visit?patient=${patientUuid}&v=${defaultVisitCustomRepresentation}&includeInactive=false`, + openmrsFetch + ); - if (currentVisit) { - getStartedVisit.next({ - mode: VisitMode.LOADING, - visitData: currentVisit, - status: VisitStatus.ONGOING, - }); - } - }, - setError - ); - return () => sub && sub.unsubscribe(); - }, [patientUuid]); + const currentVisit = + data?.data.results.find((visit) => visit.stopDatetime === null) ?? null; - return { currentVisit, error }; + return { error, mutate, isValidating, currentVisit }; }