From 25076e566aa30b608681afa452d6d41db41b06d7 Mon Sep 17 00:00:00 2001 From: Jose Francisco <94977371+icrc-jofrancisco@users.noreply.github.com> Date: Tue, 30 Jan 2024 16:21:59 +0000 Subject: [PATCH] (fix) Refined submit encounter and visit creation (#71) --- package.json | 3 +- src/form-entry-workflow/FormEntryWorkflow.tsx | 34 ++++---- .../GroupSessionWorkspace.tsx | 83 +++++++------------ yarn.lock | 10 +++ 4 files changed, 60 insertions(+), 70 deletions(-) diff --git a/package.json b/package.json index a75cb2d..c3d5477 100644 --- a/package.json +++ b/package.json @@ -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": "yarn@3.6.4" } diff --git a/src/form-entry-workflow/FormEntryWorkflow.tsx b/src/form-entry-workflow/FormEntryWorkflow.tsx index 9e6adb6..470daf4 100644 --- a/src/form-entry-workflow/FormEntryWorkflow.tsx +++ b/src/form-entry-workflow/FormEntryWorkflow.tsx @@ -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"; @@ -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, }); @@ -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 ( @@ -142,7 +148,7 @@ const FormWorkspace = () => { />
-

Forms filled

+

{t("formsFilled", "Forms filled")}

{patientUuids.map((patientUuid) => ( { } = 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] = { @@ -158,7 +122,7 @@ 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({ @@ -166,34 +130,43 @@ const GroupSessionWorkspace = () => { 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] diff --git a/yarn.lock b/yarn.lock index 2f21d9e..7426026 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: @@ -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"