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

Basic Support for Offline Registered Patients #527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
b0b1d49
Pre-merge master.
manuelroemer Jan 12, 2022
3c50201
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer Jan 12, 2022
7a49b94
Enable patient banner to work with offline registered patient.
manuelroemer Jan 12, 2022
f9f6d5a
Fixed new bug where the offline visit conversion failed due to undefi…
manuelroemer Jan 12, 2022
e74394e
Improve and fix queued sync items w.r.t. offline registered patients.
manuelroemer Jan 12, 2022
6c0b4c9
Refined fhir.Patient <> PatientCreate interaction.
manuelroemer Jan 13, 2022
3b2c0f0
Consider FHIR errors when fetching an online patient.
manuelroemer Jan 13, 2022
8b8f1ba
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer Jan 20, 2022
aa2e6c3
Adapted patient FHIR gender mapping and values read from registration…
manuelroemer Jan 20, 2022
10a4475
Use expected visit sync type dependencies.
manuelroemer Jan 20, 2022
b0e1961
Adapt FHIR mapping to use correct `id` prop.
manuelroemer Jan 20, 2022
cc1fbf3
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer Jan 26, 2022
60bc1c0
Updated yarn.lock.
manuelroemer Jan 26, 2022
d306c0e
Fixed offline registered patient loading which got broken by newest c…
manuelroemer Jan 26, 2022
8f2ed2b
Use dedicated offline visit.
manuelroemer Jan 26, 2022
9c57ff2
Moved FHIR mapping to patient-registration.
manuelroemer Jan 26, 2022
b61ed18
Adapt to updated patient registration typings.
manuelroemer Jan 26, 2022
6bb471c
Merge branch 'master' of github.com:openmrs/openmrs-esm-patient-chart…
manuelroemer Jan 27, 2022
1d93b68
Pin @jsenv/file-size-impact dep.
manuelroemer Jan 27, 2022
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@babel/preset-react": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@babel/runtime": "^7.12.13",
"@jsenv/file-size-impact": "^12.1.1",
"@jsenv/file-size-impact": "12.1.1",
"@openmrs/esm-framework": "next",
"@testing-library/dom": "^7.20.0",
"@testing-library/jest-dom": "^5.11.0",
Expand Down
1 change: 1 addition & 0 deletions packages/esm-form-entry-app/src/offline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export async function setupOfflineEncounterSync() {
async function queueEncounterRequest(item: QueuedEncounterRequest) {
const descriptor: QueueItemDescriptor = {
id: item.body.uuid,
displayName: 'Patient form',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be translated?

dependencies: [
{
type: 'visit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,7 @@ import { Button } from 'carbon-components-react';
import { ExtensionSlot, age } from '@openmrs/esm-framework';

interface PatientBannerProps {
patient: Pick<
fhir.Patient,
| 'id'
| 'name'
| 'gender'
| 'birthDate'
| 'identifier'
| 'address'
| 'telecom'
| 'deceasedBoolean'
| 'deceasedDateTime'
>;
patient: fhir.Patient;
patientUuid: string;
onClick?: (patientUuid: string) => void;
onTransition?: () => void;
Expand All @@ -38,7 +27,7 @@ const PatientBanner: React.FC<PatientBannerProps> = ({ patient, patientUuid, onC
[patientUuid, onClick, onTransition],
);

const patientName = `${patient.name[0].given.join(' ')} ${patient.name[0].family}`;
const patientName = `${patient.name?.[0].given?.join(' ')} ${patient?.name?.[0].family}`;
const patientPhotoSlotState = React.useMemo(() => ({ patientUuid, patientName }), [patientUuid]);

const [showContactDetails, setShowContactDetails] = React.useState(false);
Expand Down Expand Up @@ -101,7 +90,9 @@ const PatientBanner: React.FC<PatientBannerProps> = ({ patient, patientUuid, onC
<span>{dayjs(patient.birthDate).format('DD - MMM - YYYY')}</span>
</div>
<div className={styles.row}>
<span className={styles.identifiers}>{patient.identifier.map((i) => i.value).join(', ')}</span>
<span className={styles.identifiers}>
{patient.identifier?.length ? patient.identifier.map((i) => i.value).join(', ') : '--'}
</span>
<Button
kind="ghost"
renderIcon={showContactDetails ? ChevronUp16 : ChevronDown16}
Expand All @@ -115,7 +106,7 @@ const PatientBanner: React.FC<PatientBannerProps> = ({ patient, patientUuid, onC
</div>
</div>
{showContactDetails && (
<ContactDetails address={patient.address} telecom={patient.telecom} patientId={patient.id} />
<ContactDetails address={patient.address ?? []} telecom={patient.telecom ?? []} patientId={patient.id} />
)}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,31 @@ interface ContactDetailsProps {
patientId: string;
}

const Address: React.FC<{ address: fhir.Address }> = ({ address }) => {
const Address: React.FC<{ address?: fhir.Address }> = ({ address }) => {
const { t } = useTranslation();

return (
<>
<p className={styles.heading}>{t('address', 'Address')}</p>
<ul>
{(() => {
if (address) {
const { city, country, postalCode, state } = address;
return (
<>
<li>{postalCode}</li>
<li>{city}</li>
<li>{state}</li>
<li>{country}</li>
</>
);
}
return '--';
})()}
{address ? (
<>
<li>{address.postalCode}</li>
<li>{address.city}</li>
<li>{address.state}</li>
<li>{address.country}</li>
</>
) : (
'--'
)}
</ul>
</>
);
};

const Contact: React.FC<{ telecom: Array<fhir.ContactPoint> }> = ({ telecom }) => {
const { t } = useTranslation();
const value = telecom && telecom.length ? telecom[0].value : '--';
const value = telecom?.length ? telecom[0].value : '--';

return (
<>
Expand Down Expand Up @@ -81,8 +77,7 @@ const Relationships: React.FC<{ patientId: string }> = ({ patientId }) => {
};

const ContactDetails: React.FC<ContactDetailsProps> = ({ address, telecom, patientId }) => {
const { t } = useTranslation();
const currentAddress = address ? address.find((a) => a.use === 'home') : null;
const currentAddress = address ? address.find((a) => a.use === 'home') : undefined;

return (
<div className={styles.contactDetails}>
Expand Down
88 changes: 77 additions & 11 deletions packages/esm-patient-chart-app/src/offline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NewVisitPayload, Visit } from '@openmrs/esm-api';
import { NewVisitPayload } from '@openmrs/esm-api';
import {
getStartedVisit,
getSynchronizationItems,
Expand All @@ -10,16 +10,19 @@ import {
VisitStatus,
subscribeConnectivity,
QueueItemDescriptor,
usePatient,
} from '@openmrs/esm-framework';
import { useEffect } from 'react';
import { v4 } from 'uuid';
import useSWR, { SWRResponse } from 'swr';

const visitSyncType = 'visit';
const patientRegistrationSyncType = 'patient-registration';

interface OfflineVisit extends NewVisitPayload {
uuid: string;
}

const visitSyncType = 'visit';

export function setupCacheableRoutes() {
messageOmrsServiceWorker({
type: 'registerDynamicRoute',
Expand All @@ -33,7 +36,7 @@ export function setupCacheableRoutes() {
}

export function setupOfflineVisitsSync() {
setupOfflineSync<OfflineVisit>(visitSyncType, [], async (visit, options) => {
setupOfflineSync<OfflineVisit>(visitSyncType, [patientRegistrationSyncType], async (visit, options) => {
const visitPayload = {
...visit,
stopDatetime: new Date(),
Expand Down Expand Up @@ -73,19 +76,30 @@ async function getOfflineVisitForPatient(patientUuid: string) {
}

async function createOfflineVisitForPatient(patientUuid: string, location: string) {
const patientRegistrationSyncItems = await getSynchronizationItems<any>(patientRegistrationSyncType);
const isVisitForOfflineRegisteredPatient = patientRegistrationSyncItems.some(
(item) => item.fhirPatient.id === patientUuid,
);

const offlineVisit: OfflineVisit = {
uuid: v4(),
patient: patientUuid,
startDatetime: new Date(),
location,
// TODO: This UUID belongs to the "Facility Visit" type.
// This should be replaced with the dedicated offline visit as soon as it exists in the BE.
visitType: '7b0f5697-27e3-40c4-8bae-f4049abfb4ed',
visitType: 'a22733fa-3501-4020-a520-da024eeff088', // "Offline" visit type UUID.
};

const descriptor: QueueItemDescriptor = {
id: offlineVisit.uuid,
dependencies: [],
displayName: 'Offline visit',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be translated?

dependencies: isVisitForOfflineRegisteredPatient
? [
{
type: patientRegistrationSyncType,
id: patientUuid,
},
]
: [],
};

await queueSynchronizationItem(visitSyncType, offlineVisit, descriptor);
Expand All @@ -95,15 +109,67 @@ async function createOfflineVisitForPatient(patientUuid: string, location: strin
function offlineVisitToVisit(offlineVisit: OfflineVisit) {
return {
uuid: offlineVisit.uuid,
startDatetime: offlineVisit.startDatetime.toString(),
stopDatetime: offlineVisit.stopDatetime.toString(),
startDatetime: offlineVisit.startDatetime?.toString(),
stopDatetime: offlineVisit.stopDatetime?.toString(),
encounters: [],
visitType: {
uuid: offlineVisit.visitType,
display: 'OFFLINE_VISIT_PLACEHOLDER',
display: 'Offline',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be translated?

},
patient: {
uuid: offlineVisit.patient,
},
};
}

export function usePatientOrOfflineRegisteredPatient(patientUuid: string): ReturnType<typeof usePatient> {
const onlinePatientState = usePatient(patientUuid);
const offlinePatientState = useSWR(`offlineRegisteredPatient/${patientUuid}`, async () => {
const offlinePatient = await getOfflineRegisteredPatientAsFhirPatient(patientUuid);
if (!offlinePatient) {
throw new Error(`No offline registered patient could be found. UUID: ${patientUuid}`);
}

return offlinePatient;
});

if (onlinePatientState.isLoading || (!offlinePatientState.data && !offlinePatientState.error)) {
return {
isLoading: true,
patient: null,
patientUuid,
error: null,
};
}

if (onlinePatientState.patient && !(onlinePatientState.patient as any).issue) {
return {
isLoading: false,
patient: onlinePatientState.patient,
patientUuid,
error: null,
};
}

if (offlinePatientState.data) {
return {
isLoading: false,
patient: offlinePatientState.data,
patientUuid,
error: null,
};
}

return {
isLoading: false,
patient: null,
patientUuid,
error: onlinePatientState.error ?? offlinePatientState.error,
};
}

async function getOfflineRegisteredPatientAsFhirPatient(patientUuid: string): Promise<fhir.Patient | undefined> {
const patientRegistrationSyncItems = await getSynchronizationItems<any>(patientRegistrationSyncType);
const patientSyncItem = patientRegistrationSyncItems.find((item) => item.fhirPatient.id === patientUuid);
return patientSyncItem.fhirPatient;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Loader from './loader.component';
import ChartReview from '../view-components/chart-review.component';
import VisitDialog from '../visit/visit-dialog.component';
import { RouteComponentProps } from 'react-router-dom';
import { detachAll, ExtensionSlot, usePatient, useSessionUser } from '@openmrs/esm-framework';
import { detachAll, ExtensionSlot, useSessionUser } from '@openmrs/esm-framework';
import ActionMenu from './action-menu.component';
import { useOfflineVisitForPatient } from '../offline';
import { useOfflineVisitForPatient, usePatientOrOfflineRegisteredPatient } from '../offline';
import { useContextWorkspace } from '../hooks/useContextWindowSize';
import { WorkspaceWindowState } from '../types';
import WorkspaceNotification from './workspace-notification.component';
Expand All @@ -19,7 +19,7 @@ interface PatientChartParams {

const PatientChart: React.FC<RouteComponentProps<PatientChartParams>> = ({ match }) => {
const { patientUuid, view, subview } = match.params;
const { isLoading, patient } = usePatient(patientUuid);
const { isLoading, patient } = usePatientOrOfflineRegisteredPatient(patientUuid);
const sessionUser = useSessionUser();
const state = useMemo(() => ({ patient, patientUuid }), [patient, patientUuid]);
const { windowSize, openWindows } = useContextWorkspace();
Expand Down
Loading