@openmrs/esm-patient-common-lib 13.0.2-pre.12768 → 13.0.2-pre.12774
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -16,6 +16,8 @@ export * from './time-helper';
|
|
|
16
16
|
export * from './types';
|
|
17
17
|
export * from './useAllowedFileExtensions';
|
|
18
18
|
export * from './useMaxAttachmentFileSize';
|
|
19
|
+
export * from './usePatientForBanner';
|
|
20
|
+
export * from './usePatientIdentifier';
|
|
19
21
|
export * from './useSystemVisitSetting';
|
|
20
22
|
export * from './visit';
|
|
21
23
|
export * from './workspaces';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { renderHook } from '@testing-library/react';
|
|
3
|
+
import { usePatient } from '@openmrs/esm-framework';
|
|
4
|
+
import { mockPatient } from 'tools';
|
|
5
|
+
import { usePatientForBanner } from './usePatientForBanner';
|
|
6
|
+
|
|
7
|
+
const mockUsePatient = vi.mocked(usePatient);
|
|
8
|
+
|
|
9
|
+
describe('usePatientForBanner', () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
mockUsePatient.mockReset();
|
|
12
|
+
mockUsePatient.mockReturnValue({ patient: null, patientUuid: null, isLoading: false, error: null });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('does not fetch when the supplied patient already has banner data', () => {
|
|
16
|
+
const { result } = renderHook(() => usePatientForBanner(mockPatient));
|
|
17
|
+
|
|
18
|
+
expect(mockUsePatient).toHaveBeenCalledWith('');
|
|
19
|
+
expect(result.current).toBe(mockPatient);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('fetches the full patient when the supplied patient is partial', () => {
|
|
23
|
+
const partialPatient = { id: mockPatient.id, birthDate: mockPatient.birthDate } as fhir.Patient;
|
|
24
|
+
mockUsePatient.mockReturnValue({
|
|
25
|
+
patient: mockPatient,
|
|
26
|
+
patientUuid: mockPatient.id,
|
|
27
|
+
isLoading: false,
|
|
28
|
+
error: null,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const { result } = renderHook(() => usePatientForBanner(partialPatient));
|
|
32
|
+
|
|
33
|
+
expect(mockUsePatient).toHaveBeenCalledWith(mockPatient.id);
|
|
34
|
+
expect(result.current).toBe(mockPatient);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('fetches the patient by UUID when no patient is supplied', () => {
|
|
38
|
+
mockUsePatient.mockReturnValue({
|
|
39
|
+
patient: mockPatient,
|
|
40
|
+
patientUuid: mockPatient.id,
|
|
41
|
+
isLoading: false,
|
|
42
|
+
error: null,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const { result } = renderHook(() => usePatientForBanner(undefined, mockPatient.id));
|
|
46
|
+
|
|
47
|
+
expect(mockUsePatient).toHaveBeenCalledWith(mockPatient.id);
|
|
48
|
+
expect(result.current).toBe(mockPatient);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { usePatient } from '@openmrs/esm-framework';
|
|
2
|
+
|
|
3
|
+
export function hasPatientBannerData(patient?: fhir.Patient) {
|
|
4
|
+
return Boolean(patient?.name?.length && patient?.identifier?.length);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resolves a patient with the data needed by the patient header. Some callers pass a partial
|
|
9
|
+
* patient or a REST representation, so fetch the FHIR patient when the supplied patient cannot
|
|
10
|
+
* populate the banner. No request is made when the supplied patient already has what the banner
|
|
11
|
+
* needs.
|
|
12
|
+
*/
|
|
13
|
+
export function usePatientForBanner(patient?: fhir.Patient, patientUuid?: string) {
|
|
14
|
+
const uuid = patientUuid ?? patient?.id;
|
|
15
|
+
const shouldFetch = Boolean(uuid) && !hasPatientBannerData(patient);
|
|
16
|
+
const { patient: fetchedPatient } = usePatient(shouldFetch ? uuid : '');
|
|
17
|
+
|
|
18
|
+
return hasPatientBannerData(patient) ? patient : fetchedPatient ?? patient;
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { usePrimaryIdentifierCode } from '@openmrs/esm-framework';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Returns the patient's preferred identifier value (the one matching the configured
|
|
6
|
+
* primary identifier type), falling back to the patient's first identifier. Returns
|
|
7
|
+
* `undefined` until the patient and/or the primary identifier code has loaded.
|
|
8
|
+
*/
|
|
9
|
+
export function usePatientIdentifier(patient?: fhir.Patient) {
|
|
10
|
+
const primaryIdentifierCode = usePrimaryIdentifierCode()?.primaryIdentifierCode;
|
|
11
|
+
|
|
12
|
+
return useMemo(() => {
|
|
13
|
+
if (!patient?.identifier?.length) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const preferredIdentifier = patient.identifier.find(
|
|
18
|
+
(identifier) => identifier.type?.coding?.[0]?.code === primaryIdentifierCode,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
return (preferredIdentifier ?? patient.identifier[0])?.value;
|
|
22
|
+
}, [patient?.identifier, primaryIdentifierCode]);
|
|
23
|
+
}
|