diff --git a/app/javascript/components/record-form/form/record-form.jsx b/app/javascript/components/record-form/form/record-form.jsx index 83757c45db..26ffbeda6a 100644 --- a/app/javascript/components/record-form/form/record-form.jsx +++ b/app/javascript/components/record-form/form/record-form.jsx @@ -18,6 +18,7 @@ import { AUDIO_FIELD, DOCUMENT_FIELD, PHOTO_FIELD } from "../constants"; import { LEGITIMATE_BASIS } from "../../record-creation-flow/components/consent-prompt/constants"; import renderFormSections from "../components/render-form-sections"; import { useApp } from "../../application"; +import { parseExpression } from "../../../libs/expressions"; import { RECORD_FORM_NAME } from "./constants"; import { fieldValidations } from "./validations"; @@ -54,6 +55,7 @@ function RecordForm({ const formikValues = useRef(); const bindedSetValues = useRef(null); const bindedResetForm = useRef(null); + const bindedRecalculateFields = useRef(null); const bindSetValues = setValues => { bindedSetValues.current = setValues; @@ -63,6 +65,10 @@ function RecordForm({ bindedResetForm.current = resetForm; }; + const bindRecalculateFields = recalculateFields => { + bindedRecalculateFields.current = recalculateFields; + }; + const buildValidationSchema = formSections => { const schema = formSections.reduce((obj, item) => { return Object.assign( @@ -174,6 +180,14 @@ function RecordForm({ } }, [mode.isNew, dataProtectionInitialValues]); + const calculatedFields = forms.flatMap(fs => fs.fields.filter(field => field.calculation?.expression)); + + useEffect(() => { + if (typeof bindedRecalculateFields.current === "function") { + bindedRecalculateFields.current(); + } + }); + const handleConfirm = onConfirm => { onConfirm(); if (incidentFromCase?.size) { @@ -207,11 +221,23 @@ function RecordForm({ > {props => { // eslint-disable-next-line react/prop-types - const { submitForm, values } = props; + const { submitForm, values, setFieldValue } = props; bindSubmitForm(submitForm); setFormikValuesForNav(values); + bindRecalculateFields(() => { + if (values) { + calculatedFields.forEach(field => { + const result = parseExpression(field.calculation.expression).evaluate(values); + + if (values[field.name] !== result) { + setFieldValue(field.name, result, false); + } + }); + } + }); + return (