From ea0d8c3a5e974e213b77e16dbef314d8858421b5 Mon Sep 17 00:00:00 2001 From: Dennis Kigen Date: Tue, 18 Apr 2023 12:25:18 +0300 Subject: [PATCH] (fix) Miscellaneous improvements --- src/ohri-form.component.tsx | 74 ++++++++++++++++++------------------- src/ohri-form.scss | 5 +++ 2 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/ohri-form.component.tsx b/src/ohri-form.component.tsx index babc4e669..80533ced2 100644 --- a/src/ohri-form.component.tsx +++ b/src/ohri-form.component.tsx @@ -1,6 +1,8 @@ import React, { useEffect, useMemo, useRef, useState } from 'react'; -import styles from './ohri-form.scss'; +import ReactMarkdown from 'react-markdown'; import { Form, Formik } from 'formik'; +import { Button, ButtonSet } from '@carbon/react'; +import { useTranslation } from 'react-i18next'; import * as Yup from 'yup'; import { usePatient, @@ -11,21 +13,19 @@ import { registerExtension, attach, } from '@openmrs/esm-framework'; -import { useTranslation } from 'react-i18next'; -import { OHRIFormSchema, SessionMode, OHRIFormPage as OHRIFormPageProps } from './api/types'; +import LinearLoader from './components/loaders/linear-loader.component'; +import LoadingIcon from './components/loaders/loading.component'; import OHRIFormSidebar from './components/sidebar/ohri-form-sidebar.component'; +import { init, teardown } from './lifecycle'; +import { OHRIFormSchema, SessionMode, OHRIFormPage as OHRIFormPageProps } from './api/types'; import { OHRIEncounterForm } from './components/encounter/ohri-encounter-form'; -import { useWorkspaceLayout } from './hooks/useWorkspaceLayout'; -import { Button, ButtonSet } from '@carbon/react'; -import ReactMarkdown from 'react-markdown'; import { PatientBanner } from './components/patient-banner/patient-banner.component'; -import LoadingIcon from './components/loaders/loading.component'; -import { init, teardown } from './lifecycle'; -import { usePostSubmissionAction } from './hooks/usePostSubmissionAction'; -import LinearLoader from './components/loaders/linear-loader.component'; -import { useFormJson } from './hooks/useFormJson'; import { PatientChartWorkspaceHeaderSlot } from './constants'; import { reportError } from './utils/error-utils'; +import { useFormJson } from './hooks/useFormJson'; +import { usePostSubmissionAction } from './hooks/usePostSubmissionAction'; +import { useWorkspaceLayout } from './hooks/useWorkspaceLayout'; +import styles from './ohri-form.scss'; interface OHRIFormProps { patientUUID: string; @@ -67,8 +67,8 @@ interface OHRIFormProps { } export interface FormSubmissionHandler { - validate: (values) => boolean; submit: (values) => Promise; + validate: (values) => boolean; } const OHRIForm: React.FC = ({ @@ -84,8 +84,9 @@ const OHRIForm: React.FC = ({ meta, encounterUuid, }) => { - const [currentProvider, setCurrentProvider] = useState(null); - const [location, setEncounterLocation] = useState(null); + const session = useSession(); + const currentProvider = session?.currentProvider?.uuid ? session.currentProvider.uuid : null; + const location = session && !(encounterUUID || encounterUuid) ? session?.sessionLocation : null; const { patient, isLoading: isLoadingPatient, error: patientError } = usePatient(patientUUID); const { formJson: refinedFormJson, isLoading: isLoadingFormJson, formError } = useFormJson( formUUID, @@ -93,20 +94,20 @@ const OHRIForm: React.FC = ({ encounterUUID || encounterUuid, formSessionIntent, ); - const session = useSession(); - const [initialValues, setInitialValues] = useState({}); + + const { t } = useTranslation(); const encDate = new Date(); + const handlers = new Map(); + const ref = useRef(null); + const workspaceLayout = useWorkspaceLayout(ref); + const [initialValues, setInitialValues] = useState({}); const [scrollablePages, setScrollablePages] = useState(new Set()); const [selectedPage, setSelectedPage] = useState(''); const [collapsed, setCollapsed] = useState(true); - const { t } = useTranslation(); - const ref = useRef(null); - const workspaceLayout = useWorkspaceLayout(ref); - const handlers = new Map(); + const [isLoadingFormDependencies, setIsLoadingFormDependencies] = useState(true); const [isSubmitting, setIsSubmitting] = useState(false); const [pagesWithErrors, setPagesWithErrors] = useState([]); const postSubmissionHandlers = usePostSubmissionAction(refinedFormJson?.postSubmissionActions); - const [isLoadingFormDependencies, setIsLoadingFormDependencies] = useState(true); const sessionMode = useMemo(() => { if (mode) { @@ -115,7 +116,7 @@ const OHRIForm: React.FC = ({ return encounterUUID || encounterUuid ? 'edit' : 'enter'; }, [mode, encounterUUID, encounterUuid]); - const showSideBar = useMemo(() => { + const showSidebar = useMemo(() => { return workspaceLayout != 'minimized' && scrollablePages.size > 0; }, [workspaceLayout, scrollablePages.size]); @@ -137,6 +138,7 @@ const OHRIForm: React.FC = ({ }, }, }; + registerExtension(extDetails); attach(PatientChartWorkspaceHeaderSlot, extDetails.name); @@ -145,15 +147,6 @@ const OHRIForm: React.FC = ({ }; }, []); - useEffect(() => { - if (session) { - if (!(encounterUUID || encounterUuid)) { - setEncounterLocation(session.sessionLocation); - } - setCurrentProvider(session.currentProvider.uuid); - } - }, [session]); - useEffect(() => { //////////// // This hooks into the React lifecycle of the forms engine. @@ -173,20 +166,21 @@ const OHRIForm: React.FC = ({ }, [patientError, t]); const handleFormSubmit = (values: Record) => { - // validate form and it's suforms - let isSubmitable = true; + // validate the form and its subforms (when present) + let isSubmittable = true; handlers.forEach(handler => { const result = handler?.validate?.(values); if (!result) { - isSubmitable = false; + isSubmittable = false; } }); // do submit - if (isSubmitable) { + if (isSubmittable) { setIsSubmitting(true); const submissions = [...handlers].map(([key, handler]) => { return handler?.submit?.(values); }); + Promise.all(submissions) .then(async results => { if (mode == 'edit') { @@ -242,18 +236,18 @@ const OHRIForm: React.FC = ({ setSubmitting(false); }}> {props => ( -
+ {isLoadingPatient || isLoadingFormJson ? ( ) : (
{isLoadingFormDependencies && ( -
+
)}
- {showSideBar && ( + {showSidebar && ( = ({ /> )}
- {workspaceLayout != 'minimized' && } + {workspaceLayout != 'minimized' && patient?.id && ( + + )} {refinedFormJson.markdown && (
diff --git a/src/ohri-form.scss b/src/ohri-form.scss index c890b3f12..2e707c3e8 100644 --- a/src/ohri-form.scss +++ b/src/ohri-form.scss @@ -17,6 +17,11 @@ height: 100%; } +.loader { + @extend .ohriFormBody; + height: 1rem; +} + .ohriFormContainer :global(.cds--label) { font-size: 0.875rem; width: 18rem;