Skip to content

Commit

Permalink
(fix) Refined submit encounter and visit creation (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
icrc-jofrancisco authored Jan 30, 2024
1 parent 1d37750 commit 25076e5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 70 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
"dependencies": {
"i18next": "^21.10.0",
"i18next-parser": "^6.6.0",
"react-hook-form": "^7.34.2"
"react-hook-form": "^7.34.2",
"uuid": "^9.0.1"
},
"packageManager": "[email protected]"
}
34 changes: 20 additions & 14 deletions src/form-entry-workflow/FormEntryWorkflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PatientCard from "../patient-card/PatientCard";
import styles from "./styles.scss";
import PatientSearchHeader from "./patient-search-header";
import { useTranslation } from "react-i18next";
import { v4 as uuid } from "uuid";
import FormWorkflowContext, {
FormWorkflowProvider,
} from "../context/FormWorkflowContext";
Expand Down Expand Up @@ -72,11 +73,7 @@ const FormWorkspace = () => {
const [visit, setVisit] = useState(null);
const { sessionLocation } = useSession();

const {
saveVisit,
updateEncounter,
success: visitSaveSuccess,
} = useStartVisit({
const { updateEncounter, success: visitSaveSuccess } = useStartVisit({
showSuccessNotification: false,
showErrorNotification: true,
});
Expand Down Expand Up @@ -110,15 +107,24 @@ const FormWorkspace = () => {
// Create a visit with the same date as the encounter being saved
const visitStartDatetime = new Date(payload.encounterDatetime);
const visitStopDatetime = new Date(payload.encounterDatetime);
saveVisit({
patientUuid: activePatientUuid,
startDatetime: visitStartDatetime.toISOString(),
stopDatetime: visitStopDatetime.toISOString(),
visitType: singleSessionVisitTypeUuid,
location: sessionLocation?.uuid,
});
const visitInfo = {
startDatetime: visitStartDatetime,
stopDatetime: visitStopDatetime,
uuid: uuid(),
patient: {
uuid: activePatientUuid,
},
location: {
uuid: sessionLocation?.uuid,
},
visitType: {
uuid: singleSessionVisitTypeUuid,
},
};

payload.visit = visitInfo;
},
[activePatientUuid, singleSessionVisitTypeUuid, saveVisit, sessionLocation]
[activePatientUuid, singleSessionVisitTypeUuid, sessionLocation]
);

return (
Expand All @@ -142,7 +148,7 @@ const FormWorkspace = () => {
/>
</div>
<div className={styles.rightPanel}>
<h4>Forms filled</h4>
<h4>{t("formsFilled", "Forms filled")}</h4>
<div className={styles.patientCardsSection}>
{patientUuids.map((patientUuid) => (
<PatientCard
Expand Down
83 changes: 28 additions & 55 deletions src/group-form-entry-workflow/GroupSessionWorkspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import React, { useCallback, useContext, useEffect, useState } from "react";
import PatientCard from "../patient-card/PatientCard";
import styles from "./styles.scss";
import { useTranslation } from "react-i18next";
import { v4 as uuid } from "uuid";
import GroupFormWorkflowContext from "../context/GroupFormWorkflowContext";
import FormBootstrap from "../FormBootstrap";
import useStartVisit from "../hooks/useStartVisit";
import CompleteModal from "../CompleteModal";
import CancelModal from "../CancelModal";

Expand Down Expand Up @@ -100,53 +100,17 @@ const GroupSessionWorkspace = () => {
} = useContext(GroupFormWorkflowContext);

const { sessionLocation } = useSession();
const [encounter, setEncounter] = useState(null);
const [visit, setVisit] = useState(null);

const {
saveVisit,
updateEncounter,
success: visitSaveSuccess,
} = useStartVisit({
showSuccessNotification: false,
showErrorNotification: true,
});

// 0. user clicks "next patient" in WorkflowNavigationButtons
// which triggers submitForNext() if workflowState === "EDIT_FORM"

// 1. save the new visit uuid and start form submission
useEffect(() => {
if (
visitSaveSuccess &&
visitSaveSuccess.data.patient.uuid === activePatientUuid
) {
setVisit(visitSaveSuccess.data);
// Update visit UUID on workflow
updateVisitUuid(visitSaveSuccess.data.uuid);
if (activeVisitUuid) {
updateVisitUuid(activeVisitUuid);
}
}, [
visitSaveSuccess,
updateVisitUuid,
activeVisitUuid,
activePatientUuid,
visit,
setVisit,
]);
}, [updateVisitUuid, activeVisitUuid, activePatientUuid]);

// 2. If there's no active visit, trigger the creation of a new one
// If there's no active visit, trigger the creation of a new one
const handleEncounterCreate = useCallback(
(payload) => {
// Create a visit with the same date as the encounter being saved
if (!activeVisitUuid) {
saveVisit({
patientUuid: activePatientUuid,
startDatetime: activeSessionMeta.sessionDate,
stopDatetime: activeSessionMeta.sessionDate,
visitType: groupVisitTypeUuid,
location: sessionLocation?.uuid,
});
}
const obsTime = new Date(activeSessionMeta.sessionDate);
payload.obs.forEach((item, index) => {
payload.obs[index] = {
Expand All @@ -158,42 +122,51 @@ const GroupSessionWorkspace = () => {
obsDatetime: obsTime.toISOString(),
};
});
// If this is a newly created encounter and visit, add session concepts to encounter payload.
const visitUuid = activeVisitUuid ? activeVisitUuid : uuid();
if (!activeVisitUuid) {
Object.entries(groupSessionConcepts).forEach(([field, uuid]) => {
payload.obs.push({
concept: uuid,
value: activeSessionMeta?.[field],
});
});
// If this is a newly created encounter and visit, add session concepts to encounter payload.
const visitInfo = {
startDatetime: activeSessionMeta.sessionDate,
stopDatetime: activeSessionMeta.sessionDate,
uuid: visitUuid,
patient: {
uuid: activePatientUuid,
},
location: {
uuid: sessionLocation?.uuid,
},
visitType: {
uuid: groupVisitTypeUuid,
},
};
payload.visit = visitInfo;
updateVisitUuid(visitUuid);
}
payload.location = sessionLocation?.uuid;
payload.encounterDatetime = obsTime.toISOString();
},
[
activePatientUuid,
activeVisitUuid,
activeSessionMeta,
activeVisitUuid,
sessionLocation?.uuid,
groupSessionConcepts,
activePatientUuid,
groupVisitTypeUuid,
saveVisit,
sessionLocation,
updateVisitUuid,
]
);

// 3. Update encounter so that it belongs to the created visit
useEffect(() => {
if (encounter && visit && encounter.patient?.uuid === visit.patient?.uuid) {
updateEncounter({ uuid: encounter.uuid, visit: visit.uuid });
}
}, [encounter, updateEncounter, visit]);

// 4. Once form has been posted, save the new encounter uuid so we can edit it later
// Once form has been posted, save the new encounter uuid so we can edit it later
const handlePostResponse = useCallback(
(encounter) => {
if (encounter && encounter.uuid) {
saveEncounter(encounter.uuid);
setEncounter(encounter);
}
},
[saveEncounter]
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3116,6 +3116,7 @@ __metadata:
swc-loader: ^0.2.3
swr: ^2.2.4
typescript: ^4.7.3
uuid: ^9.0.1
webpack: ^5.73.0
webpack-cli: ^5.1.4
peerDependencies:
Expand Down Expand Up @@ -17038,6 +17039,15 @@ __metadata:
languageName: node
linkType: hard

"uuid@npm:^9.0.1":
version: 9.0.1
resolution: "uuid@npm:9.0.1"
bin:
uuid: dist/bin/uuid
checksum: 39931f6da74e307f51c0fb463dc2462807531dc80760a9bff1e35af4316131b4fc3203d16da60ae33f07fdca5b56f3f1dd662da0c99fea9aaeab2004780cc5f4
languageName: node
linkType: hard

"v8-to-istanbul@npm:^9.0.1":
version: 9.0.1
resolution: "v8-to-istanbul@npm:9.0.1"
Expand Down

0 comments on commit 25076e5

Please sign in to comment.