@openmrs/esm-form-engine-lib 4.2.2-pre.2653 → 4.2.2-pre.2660
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/dist/adapters/encounter-diagnosis-adapter.js +2 -1
- package/dist/adapters/orders-adapter.js +2 -2
- package/dist/components/processor-factory/form-processor-factory.component.js +21 -4
- package/dist/form-engine.component.js +12 -2
- package/dist/hooks/useProcessorDependencies.js +29 -19
- package/dist/provider/form-factory-provider.js +12 -4
- package/dist/src/adapters/encounter-diagnosis-adapter.d.ts.map +1 -1
- package/dist/src/adapters/orders-adapter.d.ts.map +1 -1
- package/dist/src/components/processor-factory/form-processor-factory.component.d.ts +1 -1
- package/dist/src/components/processor-factory/form-processor-factory.component.d.ts.map +1 -1
- package/dist/src/components/renderer/form/form-renderer.component.d.ts +1 -1
- package/dist/src/components/renderer/form/form-renderer.component.d.ts.map +1 -1
- package/dist/src/form-engine.component.d.ts.map +1 -1
- package/dist/src/hooks/useProcessorDependencies.d.ts +2 -1
- package/dist/src/hooks/useProcessorDependencies.d.ts.map +1 -1
- package/dist/src/provider/form-factory-provider.d.ts +1 -0
- package/dist/src/provider/form-factory-provider.d.ts.map +1 -1
- package/dist/src/utils/zscore-service.d.ts +12 -5
- package/dist/src/utils/zscore-service.d.ts.map +1 -1
- package/dist/utils/common-expression-helpers.js +4 -4
- package/dist/utils/zscore-service.js +83 -23
- package/package.json +1 -1
- package/src/adapters/encounter-diagnosis-adapter.ts +2 -1
- package/src/adapters/orders-adapter.ts +2 -2
- package/src/components/processor-factory/form-processor-factory.component.tsx +31 -11
- package/src/components/renderer/form/form-renderer.component.tsx +1 -1
- package/src/form-engine.component.tsx +12 -2
- package/src/hooks/useProcessorDependencies.ts +29 -19
- package/src/provider/form-factory-provider.tsx +11 -3
- package/src/utils/common-expression-helpers.ts +4 -4
- package/src/utils/zscore-service.ts +94 -24
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { gracefullySetSubmission } from "../utils/common-utils.js";
|
|
1
|
+
import { clearSubmission, gracefullySetSubmission } from "../utils/common-utils.js";
|
|
2
2
|
import { isEmpty } from "../validators/form-validator.js";
|
|
3
3
|
import { isTrue } from "../utils/boolean-utils.js";
|
|
4
4
|
export let assignedDiagnosesIds = [];
|
|
5
5
|
export const EncounterDiagnosisAdapter = {
|
|
6
6
|
transformFieldValue: function(field, value, context) {
|
|
7
|
+
clearSubmission(field);
|
|
7
8
|
if (field.meta.initialValue?.omrsObject && isEmpty(value)) {
|
|
8
9
|
return gracefullySetSubmission(field, undefined, voidDiagnosis(field.meta.initialValue.omrsObject));
|
|
9
10
|
}
|
|
@@ -4,7 +4,8 @@ const defaultOrderType = 'testorder';
|
|
|
4
4
|
const defaultCareSetting = '6f0c9a92-6f24-11e3-af88-005056821db0';
|
|
5
5
|
export const OrdersAdapter = {
|
|
6
6
|
transformFieldValue: function(field, value, context) {
|
|
7
|
-
|
|
7
|
+
clearSubmission(field);
|
|
8
|
+
if (context.sessionMode === 'edit' && field.meta.initialValue?.omrsObject) {
|
|
8
9
|
return editOrder(value, field, context.currentProvider.uuid);
|
|
9
10
|
}
|
|
10
11
|
const newValue = constructNewOrder(value, field, context.currentProvider.uuid);
|
|
@@ -52,7 +53,6 @@ function constructNewOrder(value, field, orderer) {
|
|
|
52
53
|
function editOrder(newOrder, field, orderer) {
|
|
53
54
|
const previousOrder = field.meta.initialValue?.omrsObject;
|
|
54
55
|
if (newOrder === previousOrder?.concept?.uuid) {
|
|
55
|
-
clearSubmission(field);
|
|
56
56
|
return null;
|
|
57
57
|
}
|
|
58
58
|
const voided = {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
1
|
+
import React, { useCallback, useEffect, useId, useMemo, useState } from "react";
|
|
2
|
+
import { Button, InlineNotification } from "@carbon/react";
|
|
2
3
|
import useProcessorDependencies from "../../hooks/useProcessorDependencies.js";
|
|
3
4
|
import useInitialValues from "../../hooks/useInitialValues.js";
|
|
4
5
|
import { FormRenderer } from "../renderer/form/form-renderer.component.js";
|
|
@@ -37,6 +38,7 @@ function warnAboutIgnoredContextKeys(previous, next) {
|
|
|
37
38
|
const FormProcessorFactory = ({ formJson, isSubForm = false, setIsLoadingFormDependencies })=>{
|
|
38
39
|
const { patient, sessionMode, formProcessors, layoutType, location, provider, sessionDate, visit } = useFormFactory();
|
|
39
40
|
const { t } = useTranslation();
|
|
41
|
+
const formId = useId();
|
|
40
42
|
const processor = useMemo(()=>{
|
|
41
43
|
const ProcessorClass = formProcessors[formJson.processor];
|
|
42
44
|
let processorInstance;
|
|
@@ -139,7 +141,7 @@ const FormProcessorFactory = ({ formJson, isSubForm = false, setIsLoadingFormDep
|
|
|
139
141
|
customDependencies: typeof dependencies === 'function' ? dependencies(prev.customDependencies ?? {}) : dependencies
|
|
140
142
|
}))
|
|
141
143
|
}), []);
|
|
142
|
-
const { isLoading: isLoadingCustomDeps } = useProcessorDependencies(processor, processorContext, setProcessorContext, processorSetters);
|
|
144
|
+
const { isLoading: isLoadingCustomDeps, error: dependenciesError, retry } = useProcessorDependencies(processor, processorContext, setProcessorContext, processorSetters);
|
|
143
145
|
const useCustomHooks = processor.getCustomHooks().useCustomHooks;
|
|
144
146
|
const [isLoadingCustomHooks, setIsLoadingCustomHooks] = useState(!!useCustomHooks);
|
|
145
147
|
const { isLoadingInitialValues, initialValues, error: initialValuesError } = useInitialValues(processor, isLoadingCustomDeps || isLoadingCustomHooks || isLoadingConcepts, processorContext);
|
|
@@ -152,11 +154,18 @@ const FormProcessorFactory = ({ formJson, isSubForm = false, setIsLoadingFormDep
|
|
|
152
154
|
]);
|
|
153
155
|
// Notify parent of loading state changes
|
|
154
156
|
useEffect(()=>{
|
|
155
|
-
setIsLoadingFormDependencies(isLoadingProcessorDependencies);
|
|
157
|
+
setIsLoadingFormDependencies(formId, isLoadingProcessorDependencies);
|
|
156
158
|
}, [
|
|
159
|
+
formId,
|
|
157
160
|
isLoadingProcessorDependencies,
|
|
158
161
|
setIsLoadingFormDependencies
|
|
159
162
|
]);
|
|
163
|
+
useEffect(()=>{
|
|
164
|
+
return ()=>setIsLoadingFormDependencies(formId, false);
|
|
165
|
+
}, [
|
|
166
|
+
formId,
|
|
167
|
+
setIsLoadingFormDependencies
|
|
168
|
+
]);
|
|
160
169
|
useEffect(()=>{
|
|
161
170
|
reportError(initialValuesError, t('errorLoadingInitialValues', 'Error loading initial values'));
|
|
162
171
|
}, [
|
|
@@ -176,7 +185,15 @@ const FormProcessorFactory = ({ formJson, isSubForm = false, setIsLoadingFormDep
|
|
|
176
185
|
setters: processorSetters,
|
|
177
186
|
useCustomHooks: useCustomHooks,
|
|
178
187
|
setIsLoadingCustomHooks: setIsLoadingCustomHooks
|
|
179
|
-
}),
|
|
188
|
+
}), dependenciesError ? /*#__PURE__*/ React.createElement("div", null, /*#__PURE__*/ React.createElement(InlineNotification, {
|
|
189
|
+
kind: "error",
|
|
190
|
+
hideCloseButton: true,
|
|
191
|
+
title: t('errorLoadingFormDependencies', 'Unable to load form data'),
|
|
192
|
+
subtitle: t('retryLoadingFormDependencies', 'Required form data could not be loaded. Retry to complete and save this form.')
|
|
193
|
+
}), /*#__PURE__*/ React.createElement(Button, {
|
|
194
|
+
kind: "tertiary",
|
|
195
|
+
onClick: retry
|
|
196
|
+
}, t('retry', 'Retry'))) : isLoadingProcessorDependencies ? /*#__PURE__*/ React.createElement(Loader, null) : /*#__PURE__*/ React.createElement(FormRenderer, {
|
|
180
197
|
processorContext: processorContext,
|
|
181
198
|
initialValues: initialValues,
|
|
182
199
|
isSubForm: isSubForm,
|
|
@@ -34,7 +34,16 @@ const FormEngine = ({ formJson, patientUUID, formUUID, encounterUUID, visit, for
|
|
|
34
34
|
]);
|
|
35
35
|
const workspaceSize = useFormWorkspaceSize(ref);
|
|
36
36
|
const { patient, isLoadingPatient } = usePatientData(patientUUID);
|
|
37
|
-
const [isLoadingDependencies, setIsLoadingDependencies] = useState(
|
|
37
|
+
const [isLoadingDependencies, setIsLoadingDependencies] = useState(true);
|
|
38
|
+
const loadingForms = useRef(new Set());
|
|
39
|
+
const setIsLoadingFormDependencies = useCallback((formId, isLoading)=>{
|
|
40
|
+
if (isLoading) {
|
|
41
|
+
loadingForms.current.add(formId);
|
|
42
|
+
} else {
|
|
43
|
+
loadingForms.current.delete(formId);
|
|
44
|
+
}
|
|
45
|
+
setIsLoadingDependencies(loadingForms.current.size > 0);
|
|
46
|
+
}, []);
|
|
38
47
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
39
48
|
const [isFormDirty, setIsFormDirty] = useState(false);
|
|
40
49
|
const sessionMode = !isEmpty(mode) ? mode : !isEmpty(encounterUUID) ? 'edit' : 'enter';
|
|
@@ -105,6 +114,7 @@ const FormEngine = ({ formJson, patientUUID, formUUID, encounterUUID, visit, for
|
|
|
105
114
|
onSubmit: handleSubmit
|
|
106
115
|
}, isLoadingPatient || isLoadingFormJson ? /*#__PURE__*/ React.createElement(Loader, null) : /*#__PURE__*/ React.createElement(FormFactoryProvider, {
|
|
107
116
|
patient: patient,
|
|
117
|
+
isLoadingDependencies: isLoadingDependencies,
|
|
108
118
|
patientUUID: patientUUID,
|
|
109
119
|
sessionMode: sessionMode,
|
|
110
120
|
sessionDate: sessionDate,
|
|
@@ -153,7 +163,7 @@ const FormEngine = ({ formJson, patientUUID, formUUID, encounterUUID, visit, for
|
|
|
153
163
|
className: styles.formBody
|
|
154
164
|
}, /*#__PURE__*/ React.createElement(FormProcessorFactory, {
|
|
155
165
|
formJson: refinedFormJson,
|
|
156
|
-
setIsLoadingFormDependencies:
|
|
166
|
+
setIsLoadingFormDependencies: setIsLoadingFormDependencies
|
|
157
167
|
})), showBottomButtonSet && !hideControls && /*#__PURE__*/ React.createElement(ButtonSet, {
|
|
158
168
|
className: styles.minifiedButtons
|
|
159
169
|
}, /*#__PURE__*/ React.createElement(Button, {
|
|
@@ -1,33 +1,43 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
2
|
-
import {
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { loadZScoreReferences } from "../utils/zscore-service.js";
|
|
3
3
|
const useProcessorDependencies = (formProcessor, context, setContext, setters)=>{
|
|
4
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
5
|
-
const [error, setError] = useState('');
|
|
6
4
|
const { loadDependencies } = formProcessor;
|
|
5
|
+
const { formJson } = context;
|
|
6
|
+
const [attempt, setAttempt] = useState(0);
|
|
7
|
+
const [result, setResult] = useState(null);
|
|
8
|
+
const retry = useCallback(()=>setAttempt((previous)=>previous + 1), []);
|
|
9
|
+
// A retry is pending on its first render, before the effect runs.
|
|
10
|
+
const currentResult = result?.loadDependencies === loadDependencies && result?.attempt === attempt ? result : null;
|
|
7
11
|
useEffect(()=>{
|
|
8
12
|
let ignore = false;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
async function load() {
|
|
14
|
+
// Wait for both operations to finish before allowing a retry, including when one fails.
|
|
15
|
+
const results = await Promise.allSettled([
|
|
16
|
+
Promise.resolve().then(()=>loadDependencies?.(context, setContext, setters)),
|
|
17
|
+
loadZScoreReferences(formJson)
|
|
18
|
+
]);
|
|
19
|
+
if (!ignore) {
|
|
20
|
+
const failure = results.find((result)=>result.status === 'rejected');
|
|
21
|
+
setResult({
|
|
22
|
+
loadDependencies,
|
|
23
|
+
attempt,
|
|
24
|
+
error: failure ? new Error(failure.reason?.message ?? String(failure.reason)) : null
|
|
25
|
+
});
|
|
26
|
+
}
|
|
21
27
|
}
|
|
28
|
+
load();
|
|
22
29
|
return ()=>{
|
|
23
30
|
ignore = true;
|
|
24
31
|
};
|
|
25
32
|
}, [
|
|
26
|
-
loadDependencies
|
|
33
|
+
loadDependencies,
|
|
34
|
+
attempt
|
|
27
35
|
]);
|
|
36
|
+
// Failed dependencies must also keep initial values, expressions and submission blocked.
|
|
28
37
|
return {
|
|
29
|
-
isLoading,
|
|
30
|
-
error
|
|
38
|
+
isLoading: !currentResult || !!currentResult.error,
|
|
39
|
+
error: currentResult?.error,
|
|
40
|
+
retry
|
|
31
41
|
};
|
|
32
42
|
};
|
|
33
43
|
export default useProcessorDependencies;
|
|
@@ -6,7 +6,7 @@ import { processPostSubmissionActions, validateForm } from "./form-factory-helpe
|
|
|
6
6
|
import { useExternalFormAction } from "../hooks/useExternalFormAction.js";
|
|
7
7
|
import { usePostSubmissionActions } from "../hooks/usePostSubmissionActions.js";
|
|
8
8
|
const FormFactoryProviderContext = /*#__PURE__*/ createContext(undefined);
|
|
9
|
-
export const FormFactoryProvider = ({ patient, patientUUID, sessionMode, sessionDate, formJson, workspaceLayout, location, provider, visit, isFormExpanded = true, children, formSubmissionProps, hideFormCollapseToggle, handleConfirmQuestionDeletion, setIsFormDirty })=>{
|
|
9
|
+
export const FormFactoryProvider = ({ patient, patientUUID, sessionMode, sessionDate, formJson, workspaceLayout, location, provider, visit, isFormExpanded = true, children, isLoadingDependencies = false, formSubmissionProps, hideFormCollapseToggle, handleConfirmQuestionDeletion, setIsFormDirty })=>{
|
|
10
10
|
const { t } = useTranslation();
|
|
11
11
|
const rootForm = useRef(undefined);
|
|
12
12
|
const subForms = useRef({});
|
|
@@ -45,15 +45,22 @@ export const FormFactoryProvider = ({ patient, patientUUID, sessionMode, session
|
|
|
45
45
|
});
|
|
46
46
|
useEffect(()=>{
|
|
47
47
|
if (isValidating) {
|
|
48
|
-
|
|
48
|
+
if (!isLoadingDependencies) {
|
|
49
|
+
validateAllForms();
|
|
50
|
+
}
|
|
49
51
|
setIsValidating(false);
|
|
50
52
|
}
|
|
51
53
|
}, [
|
|
52
54
|
isValidating,
|
|
53
|
-
validateAllForms
|
|
55
|
+
validateAllForms,
|
|
56
|
+
isLoadingDependencies
|
|
54
57
|
]);
|
|
55
58
|
useEffect(()=>{
|
|
56
59
|
if (isSubmitting) {
|
|
60
|
+
if (isLoadingDependencies) {
|
|
61
|
+
setIsSubmitting(false);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
57
64
|
// TODO: find a dynamic way of managing the form processing order
|
|
58
65
|
// validate all forms
|
|
59
66
|
const { forms, isValid } = validateAllForms();
|
|
@@ -106,7 +113,8 @@ export const FormFactoryProvider = ({ patient, patientUUID, sessionMode, session
|
|
|
106
113
|
};
|
|
107
114
|
}, [
|
|
108
115
|
isSubmitting,
|
|
109
|
-
validateAllForms
|
|
116
|
+
validateAllForms,
|
|
117
|
+
isLoadingDependencies
|
|
110
118
|
]);
|
|
111
119
|
return /*#__PURE__*/ React.createElement(FormFactoryProviderContext.Provider, {
|
|
112
120
|
value: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"encounter-diagnosis-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/encounter-diagnosis-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAEL,KAAK,qBAAqB,EAI3B,MAAM,UAAU,CAAC;AAMlB,eAAO,IAAI,oBAAoB,EAAE,MAAM,EAAO,CAAC;AAE/C,eAAO,MAAM,yBAAyB,EAAE,
|
|
1
|
+
{"version":3,"file":"encounter-diagnosis-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/encounter-diagnosis-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAEL,KAAK,qBAAqB,EAI3B,MAAM,UAAU,CAAC;AAMlB,eAAO,IAAI,oBAAoB,EAAE,MAAM,EAAO,CAAC;AAE/C,eAAO,MAAM,yBAAyB,EAAE,qBA0DvC,CAAC;AAuCF,wBAAgB,gCAAgC,CAAC,iBAAiB,EAAE,eAAe,EAAE,QAAQ,EAAE,MAAM,WAKpG;AAED,wBAAgB,aAAa,CAAC,SAAS,EAAE,eAAe;;;EAEvD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/orders-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,qBAAqB,EAAkC,MAAM,IAAI,CAAC;AAKhF,eAAO,IAAI,gBAAgB,EAAE,MAAM,EAAO,CAAC;AAI3C,eAAO,MAAM,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"orders-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapters/orders-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,qBAAqB,EAAkC,MAAM,IAAI,CAAC;AAKhF,eAAO,IAAI,gBAAgB,EAAE,MAAM,EAAO,CAAC;AAI3C,eAAO,MAAM,aAAa,EAAE,qBA6C3B,CAAC"}
|
|
@@ -3,7 +3,7 @@ import { type FormSchema } from '../../types';
|
|
|
3
3
|
interface FormProcessorFactoryProps {
|
|
4
4
|
formJson: FormSchema;
|
|
5
5
|
isSubForm?: boolean;
|
|
6
|
-
setIsLoadingFormDependencies: (isLoading: boolean) => void;
|
|
6
|
+
setIsLoadingFormDependencies: (formId: string, isLoading: boolean) => void;
|
|
7
7
|
}
|
|
8
8
|
declare const FormProcessorFactory: ({ formJson, isSubForm, setIsLoadingFormDependencies, }: FormProcessorFactoryProps) => React.JSX.Element;
|
|
9
9
|
export default FormProcessorFactory;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form-processor-factory.component.d.ts","sourceRoot":"","sources":["../../../../src/components/processor-factory/form-processor-factory.component.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"form-processor-factory.component.d.ts","sourceRoot":"","sources":["../../../../src/components/processor-factory/form-processor-factory.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA2D,MAAM,OAAO,CAAC;AAMhF,OAAO,EAAkC,KAAK,UAAU,EAAE,MAAM,aAAa,CAAC;AAe9E,UAAU,yBAAyB;IACjC,QAAQ,EAAE,UAAU,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,4BAA4B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC;CAC5E;AAqCD,QAAA,MAAM,oBAAoB,GAAI,wDAI3B,yBAAyB,sBAmM3B,CAAC;AAEF,eAAe,oBAAoB,CAAC"}
|
|
@@ -4,7 +4,7 @@ export type FormRendererProps = {
|
|
|
4
4
|
processorContext: FormProcessorContextProps;
|
|
5
5
|
initialValues: Record<string, any>;
|
|
6
6
|
isSubForm: boolean;
|
|
7
|
-
setIsLoadingFormDependencies: (isLoading: boolean) => void;
|
|
7
|
+
setIsLoadingFormDependencies: (formId: string, isLoading: boolean) => void;
|
|
8
8
|
};
|
|
9
9
|
export declare const FormRenderer: ({ processorContext, initialValues, isSubForm, setIsLoadingFormDependencies, }: FormRendererProps) => React.JSX.Element;
|
|
10
10
|
//# sourceMappingURL=form-renderer.component.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form-renderer.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/renderer/form/form-renderer.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAyC,MAAM,OAAO,CAAC;AAQ9D,OAAO,EAAE,KAAK,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAKhE,MAAM,MAAM,iBAAiB,GAAG;IAC9B,gBAAgB,EAAE,yBAAyB,CAAC;IAC5C,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACnC,SAAS,EAAE,OAAO,CAAC;IACnB,4BAA4B,EAAE,CAAC,SAAS,EAAE,OAAO,KAAK,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"form-renderer.component.d.ts","sourceRoot":"","sources":["../../../../../src/components/renderer/form/form-renderer.component.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA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,eAAe,qBAQ5C;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -2,7 +2,8 @@ import { type FormProcessorContextProps } from '../types';
|
|
|
2
2
|
import { type FormProcessor, type FormProcessorContextSetters } from '../processors/form-processor';
|
|
3
3
|
declare const useProcessorDependencies: (formProcessor: FormProcessor, context: Partial<FormProcessorContextProps>, setContext: React.Dispatch<React.SetStateAction<FormProcessorContextProps>>, setters: FormProcessorContextSetters) => {
|
|
4
4
|
isLoading: boolean;
|
|
5
|
-
error:
|
|
5
|
+
error: Error;
|
|
6
|
+
retry: () => void;
|
|
6
7
|
};
|
|
7
8
|
export default useProcessorDependencies;
|
|
8
9
|
//# sourceMappingURL=useProcessorDependencies.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useProcessorDependencies.d.ts","sourceRoot":"","sources":["../../../src/hooks/useProcessorDependencies.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAGpG,QAAA,MAAM,wBAAwB,GAC5B,eAAe,aAAa,EAC5B,SAAS,OAAO,CAAC,yBAAyB,CAAC,EAC3C,YAAY,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,yBAAyB,CAAC,CAAC,EAC3E,SAAS,2BAA2B
|
|
1
|
+
{"version":3,"file":"useProcessorDependencies.d.ts","sourceRoot":"","sources":["../../../src/hooks/useProcessorDependencies.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EAAE,KAAK,aAAa,EAAE,KAAK,2BAA2B,EAAE,MAAM,8BAA8B,CAAC;AAGpG,QAAA,MAAM,wBAAwB,GAC5B,eAAe,aAAa,EAC5B,SAAS,OAAO,CAAC,yBAAyB,CAAC,EAC3C,YAAY,KAAK,CAAC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,cAAc,EAAE,OAAO,CAAC;IACxB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,mBAAmB,EAAE;QACnB,YAAY,EAAE,OAAO,CAAC;QACtB,eAAe,EAAE,CAAC,YAAY,EAAE,OAAO,KAAK,IAAI,CAAC;QACjD,QAAQ,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,CAAC;QAC9B,OAAO,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,IAAI,CAAC;QAC9B,WAAW,EAAE,MAAM,IAAI,CAAC;KACzB,CAAC;IACF,sBAAsB,EAAE,MAAM,IAAI,CAAC;IACnC,6BAA6B,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,SAAS,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACjF,cAAc,EAAE,CAAC,WAAW,EAAE,OAAO,KAAK,IAAI,CAAC;CAChD;AAID,eAAO,MAAM,mBAAmB,EAAE,KAAK,CAAC,EAAE,CAAC,wBAAwB,CAmJlE,CAAC;AAEF,eAAO,MAAM,cAAc,uCAM1B,CAAC"}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { type FormSchema } from '../types';
|
|
2
|
+
type ZScoreTable = Array<Record<string, any>>;
|
|
3
|
+
/**
|
|
4
|
+
* Loads the reference tables for the z-score helpers a form mentions anywhere in its schema. This has to
|
|
5
|
+
* finish before the form evaluates any expressions, because a helper whose tables aren't loaded returns
|
|
6
|
+
* `null`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function loadZScoreReferences(formJson: FormSchema): Promise<void>;
|
|
9
|
+
export declare function getWeightForHeightRef(gender: any, birthDate: any, refdate: any): ZScoreTable;
|
|
10
|
+
export declare function getHeightForAgeRef(gender: any, birthDate: any, refdate: any): any;
|
|
11
|
+
export declare function getBmiForAgeRef(gender: any, birthDate: any, refdate: any): any;
|
|
12
|
+
export {};
|
|
6
13
|
//# sourceMappingURL=zscore-service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zscore-service.d.ts","sourceRoot":"","sources":["../../../src/utils/zscore-service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"zscore-service.d.ts","sourceRoot":"","sources":["../../../src/utils/zscore-service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,KAAK,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;AA6B9C;;;;GAIG;AACH,wBAAsB,oBAAoB,CAAC,QAAQ,EAAE,UAAU,iBAkB9D;AAUD,wBAAgB,qBAAqB,CAAC,MAAM,KAAA,EAAE,SAAS,KAAA,EAAE,OAAO,KAAA,eAU/D;AAED,wBAAgB,kBAAkB,CAAC,MAAM,KAAA,EAAE,SAAS,KAAA,EAAE,OAAO,KAAA,OAsB5D;AAED,wBAAgB,eAAe,CAAC,MAAM,KAAA,EAAE,SAAS,KAAA,EAAE,OAAO,KAAA,OAWzD"}
|
|
@@ -21,7 +21,7 @@ import forEach from "lodash/forEach";
|
|
|
21
21
|
import last from "lodash/last";
|
|
22
22
|
import { isEmpty as isValueEmpty } from "../validators/form-validator.js";
|
|
23
23
|
import * as apiFunctions from "../api/index.js";
|
|
24
|
-
import {
|
|
24
|
+
import { getBmiForAgeRef, getHeightForAgeRef, getWeightForHeightRef } from "./zscore-service.js";
|
|
25
25
|
import { formatDate, parseDate } from "@openmrs/esm-framework";
|
|
26
26
|
export class CommonExpressionHelpers {
|
|
27
27
|
constructor(node, patient, allFields, allFieldValues){
|
|
@@ -415,7 +415,7 @@ export class CommonExpressionHelpers {
|
|
|
415
415
|
return null;
|
|
416
416
|
}
|
|
417
417
|
const birthDate = new Date(this.patient.birthDate);
|
|
418
|
-
const weightForHeightRef =
|
|
418
|
+
const weightForHeightRef = getWeightForHeightRef(this.patient.sex, birthDate, new Date());
|
|
419
419
|
const formattedHeight = height.toFixed(1);
|
|
420
420
|
const standardHeightMin = 45;
|
|
421
421
|
const standardMaxHeight = 110;
|
|
@@ -439,7 +439,7 @@ export class CommonExpressionHelpers {
|
|
|
439
439
|
return null;
|
|
440
440
|
}
|
|
441
441
|
const birthDate = new Date(this.patient.birthDate);
|
|
442
|
-
const bmiForAgeRef =
|
|
442
|
+
const bmiForAgeRef = getBmiForAgeRef(this.patient.sex, birthDate, new Date());
|
|
443
443
|
const heightInMeters = height / 100;
|
|
444
444
|
const bmi = parseFloat((weight / (heightInMeters * heightInMeters)).toFixed(1));
|
|
445
445
|
const refSectionObject = first(bmiForAgeRef);
|
|
@@ -456,7 +456,7 @@ export class CommonExpressionHelpers {
|
|
|
456
456
|
return null;
|
|
457
457
|
}
|
|
458
458
|
const birthDate = new Date(this.patient.birthDate);
|
|
459
|
-
const heightForAgeRef =
|
|
459
|
+
const heightForAgeRef = getHeightForAgeRef(this.patient.sex, birthDate, new Date());
|
|
460
460
|
const refSectionObject = first(heightForAgeRef);
|
|
461
461
|
return this.calculateZScoreFromRef(refSectionObject, height);
|
|
462
462
|
});
|
|
@@ -1,43 +1,103 @@
|
|
|
1
1
|
import filter from "lodash/filter";
|
|
2
|
-
import bfaMale5Above from "../zscore/bfa_boys_5_above.json";
|
|
3
|
-
import wflMaleBelow5 from "../zscore/wfl_boys_below5.json";
|
|
4
|
-
import hfaMale5Above from "../zscore/hfa_boys_5_above.json";
|
|
5
|
-
import hfaMaleBelow5 from "../zscore/hfa_boys_below5.json";
|
|
6
|
-
import bfaFemale5Above from "../zscore/bfa_girls_5_above.json";
|
|
7
|
-
import wflFemaleBelow5 from "../zscore/wfl_girls_below5.json";
|
|
8
|
-
import hfaFemale5Above from "../zscore/hfa_girls_5_above.json";
|
|
9
|
-
import hfaFemaleBelow5 from "../zscore/hfa_girls_below5.json";
|
|
10
2
|
import dayjs from "dayjs";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
/**
|
|
4
|
+
* The WHO reference tables come to about 1.3 MB of JSON and few forms use them, so each one loads only
|
|
5
|
+
* when a form that calls a z-score helper opens. The helpers themselves stay synchronous: the expression
|
|
6
|
+
* evaluator doesn't wait for a promise a helper returns before comparing it or passing it to another
|
|
7
|
+
* function, so an async helper would break expressions like `calcBMIForAgeZscore(height, weight) < -2`.
|
|
8
|
+
*/ const tableLoaders = {
|
|
9
|
+
wflFemaleBelow5: ()=>import("../zscore/wfl_girls_below5.json"),
|
|
10
|
+
wflMaleBelow5: ()=>import("../zscore/wfl_boys_below5.json"),
|
|
11
|
+
hfaFemaleBelow5: ()=>import("../zscore/hfa_girls_below5.json"),
|
|
12
|
+
hfaMaleBelow5: ()=>import("../zscore/hfa_boys_below5.json"),
|
|
13
|
+
hfaFemale5Above: ()=>import("../zscore/hfa_girls_5_above.json"),
|
|
14
|
+
hfaMale5Above: ()=>import("../zscore/hfa_boys_5_above.json"),
|
|
15
|
+
bfaFemale5Above: ()=>import("../zscore/bfa_girls_5_above.json"),
|
|
16
|
+
bfaMale5Above: ()=>import("../zscore/bfa_boys_5_above.json")
|
|
17
|
+
};
|
|
18
|
+
const tablesByHelper = {
|
|
19
|
+
calcWeightForHeightZscore: [
|
|
20
|
+
'wflFemaleBelow5',
|
|
21
|
+
'wflMaleBelow5'
|
|
22
|
+
],
|
|
23
|
+
calcHeightForAgeZscore: [
|
|
24
|
+
'hfaFemaleBelow5',
|
|
25
|
+
'hfaMaleBelow5',
|
|
26
|
+
'hfaFemale5Above',
|
|
27
|
+
'hfaMale5Above'
|
|
28
|
+
],
|
|
29
|
+
calcBMIForAgeZscore: [
|
|
30
|
+
'bfaFemale5Above',
|
|
31
|
+
'bfaMale5Above'
|
|
32
|
+
]
|
|
33
|
+
};
|
|
34
|
+
const loadedTables = new Map();
|
|
35
|
+
/**
|
|
36
|
+
* Loads the reference tables for the z-score helpers a form mentions anywhere in its schema. This has to
|
|
37
|
+
* finish before the form evaluates any expressions, because a helper whose tables aren't loaded returns
|
|
38
|
+
* `null`.
|
|
39
|
+
*/ export async function loadZScoreReferences(formJson) {
|
|
40
|
+
const schema = JSON.stringify(formJson ?? {});
|
|
41
|
+
const tableNames = Object.entries(tablesByHelper).filter(([helper])=>schema.includes(helper)).flatMap(([, names])=>names).filter((name)=>!loadedTables.has(name));
|
|
42
|
+
// Settled rather than all, so every table that can load has loaded by the time a failure is reported
|
|
43
|
+
const results = await Promise.allSettled(tableNames.map(async (name)=>{
|
|
44
|
+
const table = await tableLoaders[name]();
|
|
45
|
+
loadedTables.set(name, table.default);
|
|
46
|
+
}));
|
|
47
|
+
const failure = results.find((result)=>result.status === 'rejected');
|
|
48
|
+
if (failure) {
|
|
49
|
+
throw failure.reason;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function getTable(name) {
|
|
53
|
+
const table = loadedTables.get(name);
|
|
54
|
+
if (!table) {
|
|
55
|
+
console.warn(`Z-score calculation: the ${name} reference table hasn't been loaded`);
|
|
56
|
+
}
|
|
57
|
+
return table;
|
|
58
|
+
}
|
|
59
|
+
export function getWeightForHeightRef(gender, birthDate, refdate) {
|
|
60
|
+
const age = getAge(birthDate, refdate, 'years');
|
|
61
|
+
if (gender === 'F' && age < 5) {
|
|
62
|
+
return getTable('wflFemaleBelow5');
|
|
63
|
+
}
|
|
64
|
+
if (gender === 'M' && age < 5) {
|
|
65
|
+
return getTable('wflMaleBelow5');
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
export function getHeightForAgeRef(gender, birthDate, refdate) {
|
|
17
70
|
const age = getAge(birthDate, refdate, 'years');
|
|
18
71
|
const ageInMonths = getAge(birthDate, refdate, 'months');
|
|
19
72
|
const ageInDays = getAge(birthDate, refdate, 'days');
|
|
20
73
|
if (gender === 'F') {
|
|
21
74
|
if (age < 5) {
|
|
22
|
-
|
|
23
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaFemaleBelow5, 'Day', ageInDays);
|
|
75
|
+
return getScoreReference(getTable('hfaFemaleBelow5'), 'Day', ageInDays);
|
|
24
76
|
}
|
|
25
77
|
if (age >= 5 && age < 18) {
|
|
26
|
-
|
|
27
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaFemale5Above, 'Month', ageInMonths);
|
|
78
|
+
return getScoreReference(getTable('hfaFemale5Above'), 'Month', ageInMonths);
|
|
28
79
|
}
|
|
29
80
|
}
|
|
30
81
|
if (gender === 'M') {
|
|
31
82
|
if (age < 5) {
|
|
32
|
-
|
|
33
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaMaleBelow5, 'Day', ageInDays);
|
|
83
|
+
return getScoreReference(getTable('hfaMaleBelow5'), 'Day', ageInDays);
|
|
34
84
|
}
|
|
35
85
|
if (age >= 5 && age < 18) {
|
|
36
|
-
|
|
37
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaMale5Above, 'Month', ageInMonths);
|
|
86
|
+
return getScoreReference(getTable('hfaMale5Above'), 'Month', ageInMonths);
|
|
38
87
|
}
|
|
39
88
|
}
|
|
40
|
-
return
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
export function getBmiForAgeRef(gender, birthDate, refdate) {
|
|
92
|
+
const age = getAge(birthDate, refdate, 'years');
|
|
93
|
+
const ageInMonths = getAge(birthDate, refdate, 'months');
|
|
94
|
+
if (gender === 'F' && age >= 5 && age < 18) {
|
|
95
|
+
return getScoreReference(getTable('bfaFemale5Above'), 'Month', ageInMonths);
|
|
96
|
+
}
|
|
97
|
+
if (gender === 'M' && age >= 5 && age < 18) {
|
|
98
|
+
return getScoreReference(getTable('bfaMale5Above'), 'Month', ageInMonths);
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
41
101
|
}
|
|
42
102
|
function getScoreReference(refData, searchKey, searchValue) {
|
|
43
103
|
return filter(refData, (refObject)=>{
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
type FormField,
|
|
8
8
|
} from '../types';
|
|
9
9
|
import { type FormContextProps } from '../provider/form-provider';
|
|
10
|
-
import { gracefullySetSubmission } from '../utils/common-utils';
|
|
10
|
+
import { clearSubmission, gracefullySetSubmission } from '../utils/common-utils';
|
|
11
11
|
import { isEmpty } from '../validators/form-validator';
|
|
12
12
|
import { isTrue } from '../utils/boolean-utils';
|
|
13
13
|
|
|
@@ -15,6 +15,7 @@ export let assignedDiagnosesIds: string[] = [];
|
|
|
15
15
|
|
|
16
16
|
export const EncounterDiagnosisAdapter: FormFieldValueAdapter = {
|
|
17
17
|
transformFieldValue: function (field: FormField, value: any, context: FormContextProps) {
|
|
18
|
+
clearSubmission(field);
|
|
18
19
|
if (field.meta.initialValue?.omrsObject && isEmpty(value)) {
|
|
19
20
|
return gracefullySetSubmission(field, undefined, voidDiagnosis(field.meta.initialValue.omrsObject as OpenmrsObs));
|
|
20
21
|
}
|
|
@@ -10,7 +10,8 @@ const defaultCareSetting = '6f0c9a92-6f24-11e3-af88-005056821db0';
|
|
|
10
10
|
|
|
11
11
|
export const OrdersAdapter: FormFieldValueAdapter = {
|
|
12
12
|
transformFieldValue: function (field: FormField, value: any, context: FormContextProps) {
|
|
13
|
-
|
|
13
|
+
clearSubmission(field);
|
|
14
|
+
if (context.sessionMode === 'edit' && field.meta.initialValue?.omrsObject) {
|
|
14
15
|
return editOrder(value, field, context.currentProvider.uuid);
|
|
15
16
|
}
|
|
16
17
|
const newValue = constructNewOrder(value, field, context.currentProvider.uuid);
|
|
@@ -70,7 +71,6 @@ function constructNewOrder(value: any, field: FormField, orderer: string) {
|
|
|
70
71
|
function editOrder(newOrder: any, field: FormField, orderer: string) {
|
|
71
72
|
const previousOrder = field.meta.initialValue?.omrsObject as OpenmrsResource;
|
|
72
73
|
if (newOrder === previousOrder?.concept?.uuid) {
|
|
73
|
-
clearSubmission(field);
|
|
74
74
|
return null;
|
|
75
75
|
}
|
|
76
76
|
const voided = {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
1
|
+
import React, { useCallback, useEffect, useId, useMemo, useState } from 'react';
|
|
2
|
+
import { Button, InlineNotification } from '@carbon/react';
|
|
2
3
|
import { type OpenmrsResource } from '@openmrs/esm-framework';
|
|
3
4
|
import useProcessorDependencies from '../../hooks/useProcessorDependencies';
|
|
4
5
|
import useInitialValues from '../../hooks/useInitialValues';
|
|
@@ -21,7 +22,7 @@ import { registerFormFieldAdaptersForCleanUp } from '../../lifecycle';
|
|
|
21
22
|
interface FormProcessorFactoryProps {
|
|
22
23
|
formJson: FormSchema;
|
|
23
24
|
isSubForm?: boolean;
|
|
24
|
-
setIsLoadingFormDependencies: (isLoading: boolean) => void;
|
|
25
|
+
setIsLoadingFormDependencies: (formId: string, isLoading: boolean) => void;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
// Mutable parts of the context that can be updated by processors/hooks
|
|
@@ -66,6 +67,7 @@ const FormProcessorFactory = ({
|
|
|
66
67
|
}: FormProcessorFactoryProps) => {
|
|
67
68
|
const { patient, sessionMode, formProcessors, layoutType, location, provider, sessionDate, visit } = useFormFactory();
|
|
68
69
|
const { t } = useTranslation();
|
|
70
|
+
const formId = useId();
|
|
69
71
|
|
|
70
72
|
const processor = useMemo(() => {
|
|
71
73
|
const ProcessorClass = formProcessors[formJson.processor];
|
|
@@ -181,12 +183,11 @@ const FormProcessorFactory = ({
|
|
|
181
183
|
[],
|
|
182
184
|
);
|
|
183
185
|
|
|
184
|
-
const {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
);
|
|
186
|
+
const {
|
|
187
|
+
isLoading: isLoadingCustomDeps,
|
|
188
|
+
error: dependenciesError,
|
|
189
|
+
retry,
|
|
190
|
+
} = useProcessorDependencies(processor, processorContext, setProcessorContext, processorSetters);
|
|
190
191
|
const useCustomHooks = processor.getCustomHooks().useCustomHooks;
|
|
191
192
|
const [isLoadingCustomHooks, setIsLoadingCustomHooks] = useState(!!useCustomHooks);
|
|
192
193
|
const {
|
|
@@ -203,8 +204,12 @@ const FormProcessorFactory = ({
|
|
|
203
204
|
|
|
204
205
|
// Notify parent of loading state changes
|
|
205
206
|
useEffect(() => {
|
|
206
|
-
setIsLoadingFormDependencies(isLoadingProcessorDependencies);
|
|
207
|
-
}, [isLoadingProcessorDependencies, setIsLoadingFormDependencies]);
|
|
207
|
+
setIsLoadingFormDependencies(formId, isLoadingProcessorDependencies);
|
|
208
|
+
}, [formId, isLoadingProcessorDependencies, setIsLoadingFormDependencies]);
|
|
209
|
+
|
|
210
|
+
useEffect(() => {
|
|
211
|
+
return () => setIsLoadingFormDependencies(formId, false);
|
|
212
|
+
}, [formId, setIsLoadingFormDependencies]);
|
|
208
213
|
|
|
209
214
|
useEffect(() => {
|
|
210
215
|
reportError(initialValuesError, t('errorLoadingInitialValues', 'Error loading initial values'));
|
|
@@ -227,7 +232,22 @@ const FormProcessorFactory = ({
|
|
|
227
232
|
setIsLoadingCustomHooks={setIsLoadingCustomHooks}
|
|
228
233
|
/>
|
|
229
234
|
)}
|
|
230
|
-
{
|
|
235
|
+
{dependenciesError ? (
|
|
236
|
+
<div>
|
|
237
|
+
<InlineNotification
|
|
238
|
+
kind="error"
|
|
239
|
+
hideCloseButton
|
|
240
|
+
title={t('errorLoadingFormDependencies', 'Unable to load form data')}
|
|
241
|
+
subtitle={t(
|
|
242
|
+
'retryLoadingFormDependencies',
|
|
243
|
+
'Required form data could not be loaded. Retry to complete and save this form.',
|
|
244
|
+
)}
|
|
245
|
+
/>
|
|
246
|
+
<Button kind="tertiary" onClick={retry}>
|
|
247
|
+
{t('retry', 'Retry')}
|
|
248
|
+
</Button>
|
|
249
|
+
</div>
|
|
250
|
+
) : isLoadingProcessorDependencies ? (
|
|
231
251
|
<Loader />
|
|
232
252
|
) : (
|
|
233
253
|
<FormRenderer
|
|
@@ -15,7 +15,7 @@ export type FormRendererProps = {
|
|
|
15
15
|
processorContext: FormProcessorContextProps;
|
|
16
16
|
initialValues: Record<string, any>;
|
|
17
17
|
isSubForm: boolean;
|
|
18
|
-
setIsLoadingFormDependencies: (isLoading: boolean) => void;
|
|
18
|
+
setIsLoadingFormDependencies: (formId: string, isLoading: boolean) => void;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
export const FormRenderer = ({
|
|
@@ -67,7 +67,16 @@ const FormEngine = ({
|
|
|
67
67
|
}, [visit?.startDatetime, visit?.stopDatetime]);
|
|
68
68
|
const workspaceSize = useFormWorkspaceSize(ref);
|
|
69
69
|
const { patient, isLoadingPatient } = usePatientData(patientUUID);
|
|
70
|
-
const [isLoadingDependencies, setIsLoadingDependencies] = useState(
|
|
70
|
+
const [isLoadingDependencies, setIsLoadingDependencies] = useState(true);
|
|
71
|
+
const loadingForms = useRef(new Set<string>());
|
|
72
|
+
const setIsLoadingFormDependencies = useCallback((formId: string, isLoading: boolean) => {
|
|
73
|
+
if (isLoading) {
|
|
74
|
+
loadingForms.current.add(formId);
|
|
75
|
+
} else {
|
|
76
|
+
loadingForms.current.delete(formId);
|
|
77
|
+
}
|
|
78
|
+
setIsLoadingDependencies(loadingForms.current.size > 0);
|
|
79
|
+
}, []);
|
|
71
80
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
72
81
|
const [isFormDirty, setIsFormDirty] = useState(false);
|
|
73
82
|
const sessionMode = !isEmpty(mode) ? mode : !isEmpty(encounterUUID) ? 'edit' : 'enter';
|
|
@@ -132,6 +141,7 @@ const FormEngine = ({
|
|
|
132
141
|
) : (
|
|
133
142
|
<FormFactoryProvider
|
|
134
143
|
patient={patient}
|
|
144
|
+
isLoadingDependencies={isLoadingDependencies}
|
|
135
145
|
patientUUID={patientUUID}
|
|
136
146
|
sessionMode={sessionMode}
|
|
137
147
|
sessionDate={sessionDate}
|
|
@@ -179,7 +189,7 @@ const FormEngine = ({
|
|
|
179
189
|
<div className={styles.formBody}>
|
|
180
190
|
<FormProcessorFactory
|
|
181
191
|
formJson={refinedFormJson}
|
|
182
|
-
setIsLoadingFormDependencies={
|
|
192
|
+
setIsLoadingFormDependencies={setIsLoadingFormDependencies}
|
|
183
193
|
/>
|
|
184
194
|
</div>
|
|
185
195
|
{showBottomButtonSet && !hideControls && (
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { useEffect, useState } from 'react';
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react';
|
|
2
2
|
import { type FormProcessorContextProps } from '../types';
|
|
3
3
|
import { type FormProcessor, type FormProcessorContextSetters } from '../processors/form-processor';
|
|
4
|
-
import {
|
|
4
|
+
import { loadZScoreReferences } from '../utils/zscore-service';
|
|
5
5
|
|
|
6
6
|
const useProcessorDependencies = (
|
|
7
7
|
formProcessor: FormProcessor,
|
|
@@ -9,35 +9,45 @@ const useProcessorDependencies = (
|
|
|
9
9
|
setContext: React.Dispatch<React.SetStateAction<FormProcessorContextProps>>,
|
|
10
10
|
setters: FormProcessorContextSetters,
|
|
11
11
|
) => {
|
|
12
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
13
|
-
const [error, setError] = useState('');
|
|
14
12
|
const { loadDependencies } = formProcessor;
|
|
13
|
+
const { formJson } = context;
|
|
14
|
+
const [attempt, setAttempt] = useState(0);
|
|
15
|
+
const [result, setResult] = useState<{
|
|
16
|
+
loadDependencies: typeof loadDependencies;
|
|
17
|
+
attempt: number;
|
|
18
|
+
error: Error | null;
|
|
19
|
+
}>(null);
|
|
20
|
+
const retry = useCallback(() => setAttempt((previous) => previous + 1), []);
|
|
21
|
+
// A retry is pending on its first render, before the effect runs.
|
|
22
|
+
const currentResult = result?.loadDependencies === loadDependencies && result?.attempt === attempt ? result : null;
|
|
15
23
|
|
|
16
24
|
useEffect(() => {
|
|
17
25
|
let ignore = false;
|
|
18
26
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
.then(() =>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
27
|
+
async function load() {
|
|
28
|
+
// Wait for both operations to finish before allowing a retry, including when one fails.
|
|
29
|
+
const results = await Promise.allSettled([
|
|
30
|
+
Promise.resolve().then(() => loadDependencies?.(context, setContext, setters)),
|
|
31
|
+
loadZScoreReferences(formJson),
|
|
32
|
+
]);
|
|
33
|
+
if (!ignore) {
|
|
34
|
+
const failure = results.find((result): result is PromiseRejectedResult => result.status === 'rejected');
|
|
35
|
+
setResult({
|
|
36
|
+
loadDependencies,
|
|
37
|
+
attempt,
|
|
38
|
+
error: failure ? new Error(failure.reason?.message ?? String(failure.reason)) : null,
|
|
32
39
|
});
|
|
40
|
+
}
|
|
33
41
|
}
|
|
42
|
+
load();
|
|
34
43
|
|
|
35
44
|
return () => {
|
|
36
45
|
ignore = true;
|
|
37
46
|
};
|
|
38
|
-
}, [loadDependencies]);
|
|
47
|
+
}, [loadDependencies, attempt]);
|
|
39
48
|
|
|
40
|
-
|
|
49
|
+
// Failed dependencies must also keep initial values, expressions and submission blocked.
|
|
50
|
+
return { isLoading: !currentResult || !!currentResult.error, error: currentResult?.error, retry };
|
|
41
51
|
};
|
|
42
52
|
|
|
43
53
|
export default useProcessorDependencies;
|
|
@@ -46,6 +46,7 @@ interface FormFactoryProviderProps {
|
|
|
46
46
|
visit: Visit;
|
|
47
47
|
isFormExpanded: boolean;
|
|
48
48
|
children: React.ReactNode;
|
|
49
|
+
isLoadingDependencies?: boolean;
|
|
49
50
|
formSubmissionProps: {
|
|
50
51
|
isSubmitting: boolean;
|
|
51
52
|
setIsSubmitting: (isSubmitting: boolean) => void;
|
|
@@ -72,6 +73,7 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({
|
|
|
72
73
|
visit,
|
|
73
74
|
isFormExpanded = true,
|
|
74
75
|
children,
|
|
76
|
+
isLoadingDependencies = false,
|
|
75
77
|
formSubmissionProps,
|
|
76
78
|
hideFormCollapseToggle,
|
|
77
79
|
handleConfirmQuestionDeletion,
|
|
@@ -118,13 +120,19 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({
|
|
|
118
120
|
|
|
119
121
|
useEffect(() => {
|
|
120
122
|
if (isValidating) {
|
|
121
|
-
|
|
123
|
+
if (!isLoadingDependencies) {
|
|
124
|
+
validateAllForms();
|
|
125
|
+
}
|
|
122
126
|
setIsValidating(false);
|
|
123
127
|
}
|
|
124
|
-
}, [isValidating, validateAllForms]);
|
|
128
|
+
}, [isValidating, validateAllForms, isLoadingDependencies]);
|
|
125
129
|
|
|
126
130
|
useEffect(() => {
|
|
127
131
|
if (isSubmitting) {
|
|
132
|
+
if (isLoadingDependencies) {
|
|
133
|
+
setIsSubmitting(false);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
128
136
|
// TODO: find a dynamic way of managing the form processing order
|
|
129
137
|
// validate all forms
|
|
130
138
|
const { forms, isValid } = validateAllForms();
|
|
@@ -177,7 +185,7 @@ export const FormFactoryProvider: React.FC<FormFactoryProviderProps> = ({
|
|
|
177
185
|
return () => {
|
|
178
186
|
abortController.abort();
|
|
179
187
|
};
|
|
180
|
-
}, [isSubmitting, validateAllForms]);
|
|
188
|
+
}, [isSubmitting, validateAllForms, isLoadingDependencies]);
|
|
181
189
|
|
|
182
190
|
return (
|
|
183
191
|
<FormFactoryProviderContext.Provider
|
|
@@ -10,7 +10,7 @@ import { type FormField } from '../types';
|
|
|
10
10
|
import { type FormNode } from './expression-runner';
|
|
11
11
|
import { isEmpty as isValueEmpty } from '../validators/form-validator';
|
|
12
12
|
import * as apiFunctions from '../api';
|
|
13
|
-
import {
|
|
13
|
+
import { getBmiForAgeRef, getHeightForAgeRef, getWeightForHeightRef } from './zscore-service';
|
|
14
14
|
import { formatDate, parseDate } from '@openmrs/esm-framework';
|
|
15
15
|
|
|
16
16
|
export class CommonExpressionHelpers {
|
|
@@ -496,7 +496,7 @@ export class CommonExpressionHelpers {
|
|
|
496
496
|
}
|
|
497
497
|
|
|
498
498
|
const birthDate = new Date(this.patient.birthDate);
|
|
499
|
-
const weightForHeightRef =
|
|
499
|
+
const weightForHeightRef = getWeightForHeightRef(this.patient.sex, birthDate, new Date());
|
|
500
500
|
|
|
501
501
|
const formattedHeight = height.toFixed(1);
|
|
502
502
|
const standardHeightMin = 45;
|
|
@@ -527,7 +527,7 @@ export class CommonExpressionHelpers {
|
|
|
527
527
|
}
|
|
528
528
|
|
|
529
529
|
const birthDate = new Date(this.patient.birthDate);
|
|
530
|
-
const bmiForAgeRef =
|
|
530
|
+
const bmiForAgeRef = getBmiForAgeRef(this.patient.sex, birthDate, new Date());
|
|
531
531
|
|
|
532
532
|
const heightInMeters = height / 100;
|
|
533
533
|
const bmi = parseFloat((weight / (heightInMeters * heightInMeters)).toFixed(1));
|
|
@@ -549,7 +549,7 @@ export class CommonExpressionHelpers {
|
|
|
549
549
|
}
|
|
550
550
|
|
|
551
551
|
const birthDate = new Date(this.patient.birthDate);
|
|
552
|
-
const heightForAgeRef =
|
|
552
|
+
const heightForAgeRef = getHeightForAgeRef(this.patient.sex, birthDate, new Date());
|
|
553
553
|
const refSectionObject = first(heightForAgeRef);
|
|
554
554
|
|
|
555
555
|
return this.calculateZScoreFromRef(refSectionObject, height);
|
|
@@ -1,46 +1,116 @@
|
|
|
1
1
|
import filter from 'lodash/filter';
|
|
2
|
-
import bfaMale5Above from '../zscore/bfa_boys_5_above.json';
|
|
3
|
-
import wflMaleBelow5 from '../zscore/wfl_boys_below5.json';
|
|
4
|
-
import hfaMale5Above from '../zscore/hfa_boys_5_above.json';
|
|
5
|
-
import hfaMaleBelow5 from '../zscore/hfa_boys_below5.json';
|
|
6
|
-
import bfaFemale5Above from '../zscore/bfa_girls_5_above.json';
|
|
7
|
-
import wflFemaleBelow5 from '../zscore/wfl_girls_below5.json';
|
|
8
|
-
import hfaFemale5Above from '../zscore/hfa_girls_5_above.json';
|
|
9
|
-
import hfaFemaleBelow5 from '../zscore/hfa_girls_below5.json';
|
|
10
2
|
import dayjs from 'dayjs';
|
|
3
|
+
import { type FormSchema } from '../types';
|
|
11
4
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
type ZScoreTable = Array<Record<string, any>>;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The WHO reference tables come to about 1.3 MB of JSON and few forms use them, so each one loads only
|
|
9
|
+
* when a form that calls a z-score helper opens. The helpers themselves stay synchronous: the expression
|
|
10
|
+
* evaluator doesn't wait for a promise a helper returns before comparing it or passing it to another
|
|
11
|
+
* function, so an async helper would break expressions like `calcBMIForAgeZscore(height, weight) < -2`.
|
|
12
|
+
*/
|
|
13
|
+
const tableLoaders = {
|
|
14
|
+
wflFemaleBelow5: () => import('../zscore/wfl_girls_below5.json'),
|
|
15
|
+
wflMaleBelow5: () => import('../zscore/wfl_boys_below5.json'),
|
|
16
|
+
hfaFemaleBelow5: () => import('../zscore/hfa_girls_below5.json'),
|
|
17
|
+
hfaMaleBelow5: () => import('../zscore/hfa_boys_below5.json'),
|
|
18
|
+
hfaFemale5Above: () => import('../zscore/hfa_girls_5_above.json'),
|
|
19
|
+
hfaMale5Above: () => import('../zscore/hfa_boys_5_above.json'),
|
|
20
|
+
bfaFemale5Above: () => import('../zscore/bfa_girls_5_above.json'),
|
|
21
|
+
bfaMale5Above: () => import('../zscore/bfa_boys_5_above.json'),
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type ZScoreTableName = keyof typeof tableLoaders;
|
|
25
|
+
|
|
26
|
+
const tablesByHelper: Record<string, Array<ZScoreTableName>> = {
|
|
27
|
+
calcWeightForHeightZscore: ['wflFemaleBelow5', 'wflMaleBelow5'],
|
|
28
|
+
calcHeightForAgeZscore: ['hfaFemaleBelow5', 'hfaMaleBelow5', 'hfaFemale5Above', 'hfaMale5Above'],
|
|
29
|
+
calcBMIForAgeZscore: ['bfaFemale5Above', 'bfaMale5Above'],
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const loadedTables = new Map<ZScoreTableName, ZScoreTable>();
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Loads the reference tables for the z-score helpers a form mentions anywhere in its schema. This has to
|
|
36
|
+
* finish before the form evaluates any expressions, because a helper whose tables aren't loaded returns
|
|
37
|
+
* `null`.
|
|
38
|
+
*/
|
|
39
|
+
export async function loadZScoreReferences(formJson: FormSchema) {
|
|
40
|
+
const schema = JSON.stringify(formJson ?? {});
|
|
41
|
+
const tableNames = Object.entries(tablesByHelper)
|
|
42
|
+
.filter(([helper]) => schema.includes(helper))
|
|
43
|
+
.flatMap(([, names]) => names)
|
|
44
|
+
.filter((name) => !loadedTables.has(name));
|
|
45
|
+
|
|
46
|
+
// Settled rather than all, so every table that can load has loaded by the time a failure is reported
|
|
47
|
+
const results = await Promise.allSettled(
|
|
48
|
+
tableNames.map(async (name) => {
|
|
49
|
+
const table = await tableLoaders[name]();
|
|
50
|
+
loadedTables.set(name, table.default);
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
const failure = results.find((result): result is PromiseRejectedResult => result.status === 'rejected');
|
|
54
|
+
if (failure) {
|
|
55
|
+
throw failure.reason;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getTable(name: ZScoreTableName) {
|
|
60
|
+
const table = loadedTables.get(name);
|
|
61
|
+
if (!table) {
|
|
62
|
+
console.warn(`Z-score calculation: the ${name} reference table hasn't been loaded`);
|
|
63
|
+
}
|
|
64
|
+
return table;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function getWeightForHeightRef(gender, birthDate, refdate) {
|
|
68
|
+
const age = getAge(birthDate, refdate, 'years');
|
|
69
|
+
|
|
70
|
+
if (gender === 'F' && age < 5) {
|
|
71
|
+
return getTable('wflFemaleBelow5');
|
|
72
|
+
}
|
|
73
|
+
if (gender === 'M' && age < 5) {
|
|
74
|
+
return getTable('wflMaleBelow5');
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function getHeightForAgeRef(gender, birthDate, refdate) {
|
|
18
80
|
const age = getAge(birthDate, refdate, 'years');
|
|
19
81
|
const ageInMonths = getAge(birthDate, refdate, 'months');
|
|
20
82
|
const ageInDays = getAge(birthDate, refdate, 'days');
|
|
21
83
|
|
|
22
84
|
if (gender === 'F') {
|
|
23
85
|
if (age < 5) {
|
|
24
|
-
|
|
25
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaFemaleBelow5, 'Day', ageInDays);
|
|
86
|
+
return getScoreReference(getTable('hfaFemaleBelow5'), 'Day', ageInDays);
|
|
26
87
|
}
|
|
27
88
|
if (age >= 5 && age < 18) {
|
|
28
|
-
|
|
29
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaFemale5Above, 'Month', ageInMonths);
|
|
89
|
+
return getScoreReference(getTable('hfaFemale5Above'), 'Month', ageInMonths);
|
|
30
90
|
}
|
|
31
91
|
}
|
|
32
92
|
if (gender === 'M') {
|
|
33
93
|
if (age < 5) {
|
|
34
|
-
|
|
35
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaMaleBelow5, 'Day', ageInDays);
|
|
94
|
+
return getScoreReference(getTable('hfaMaleBelow5'), 'Day', ageInDays);
|
|
36
95
|
}
|
|
37
|
-
|
|
38
96
|
if (age >= 5 && age < 18) {
|
|
39
|
-
|
|
40
|
-
scoreRefModel.heightForAgeRef = getScoreReference(hfaMale5Above, 'Month', ageInMonths);
|
|
97
|
+
return getScoreReference(getTable('hfaMale5Above'), 'Month', ageInMonths);
|
|
41
98
|
}
|
|
42
99
|
}
|
|
43
|
-
return
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function getBmiForAgeRef(gender, birthDate, refdate) {
|
|
104
|
+
const age = getAge(birthDate, refdate, 'years');
|
|
105
|
+
const ageInMonths = getAge(birthDate, refdate, 'months');
|
|
106
|
+
|
|
107
|
+
if (gender === 'F' && age >= 5 && age < 18) {
|
|
108
|
+
return getScoreReference(getTable('bfaFemale5Above'), 'Month', ageInMonths);
|
|
109
|
+
}
|
|
110
|
+
if (gender === 'M' && age >= 5 && age < 18) {
|
|
111
|
+
return getScoreReference(getTable('bfaMale5Above'), 'Month', ageInMonths);
|
|
112
|
+
}
|
|
113
|
+
return null;
|
|
44
114
|
}
|
|
45
115
|
|
|
46
116
|
function getScoreReference(refData, searchKey, searchValue): any {
|