Skip to content
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

03-1125: (bug) fix current visit retaining previous patient visit-data #332

Merged
merged 4 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"];
Expand Down Expand Up @@ -32,7 +32,7 @@ describe(`<ChangeLocale />`, () => {
fireEvent.change(screen.getByLabelText(/Select locale/i), {
target: { value: "en" },
});
await wait(() =>
await waitFor(() =>
expect(postUserPropertiesMock).toHaveBeenCalledWith(
user.uuid,
{ defaultLocale: "en" },
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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([]),
Expand Down Expand Up @@ -72,7 +71,7 @@ describe(`<Root />`, () => {
});

it("does not render side menu button if desktop", async () => {
await wait(() =>
await waitFor(() =>
expect(screen.queryAllByLabelText("Open menu")).toHaveLength(0)
);
});
Expand Down
17 changes: 9 additions & 8 deletions packages/framework/esm-api/src/shared-api-objects/visit-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FetchResponse<{ results: Array<Visit> }>> {
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}`,
Expand Down
39 changes: 25 additions & 14 deletions packages/framework/esm-framework/docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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)

___

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

___

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

___

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

___

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

___

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: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)

___

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

___

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

___

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

___

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

___

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

___

Expand All @@ -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)
67 changes: 26 additions & 41 deletions packages/framework/esm-react-utils/src/useVisit.ts
Original file line number Diff line number Diff line change
@@ -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<Visit | null>(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<Visit> };
}>(
`/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 };
}