diff --git a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/helpers/getRulesActions.js b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/helpers/getRulesActions.js index a052465d9e..ce36f91b0d 100644 --- a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/helpers/getRulesActions.js +++ b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/helpers/getRulesActions.js @@ -5,27 +5,20 @@ import { getCurrentClientMainData, getApplicableRuleEffectsForEventProgram, updateRulesEffects, - validateAssignEffects, } from '../../../../../../rules'; import type { RenderFoundation, EventProgram } from '../../../../../../metaData'; import { dataEntryId, itemId, formId } from './constants'; -import type { QuerySingleResource } from '../../../../../../utils/api'; -import { rulesExecutedPostLoadDataEntry } from '../../../../../DataEntry'; -export const getRulesActions = async ({ +export const getRulesActions = ({ state, // temporary program, formFoundation, orgUnit, - querySingleResource, - uid, }: { state: ReduxState, program: EventProgram, formFoundation: RenderFoundation, orgUnit: OrgUnit, - querySingleResource: QuerySingleResource, - uid: string, }) => { const formValuesClient = getCurrentClientValues(state, formFoundation, formId); const dataEntryValuesClient = getCurrentClientMainData(state, itemId, dataEntryId, formFoundation); @@ -37,14 +30,5 @@ export const getRulesActions = async ({ currentEvent, }); - const effectsWithValidations = await validateAssignEffects({ - dataElements: formFoundation.getElements(), - effects, - querySingleResource, - }); - - return [ - updateRulesEffects(effectsWithValidations, formId), - rulesExecutedPostLoadDataEntry(dataEntryId, itemId, uid), - ]; + return updateRulesEffects(effects, formId); }; diff --git a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/index.js b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/index.js index 8e4453d8b3..47038dcfc7 100644 --- a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/index.js +++ b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/DataEntry/index.js @@ -3,4 +3,3 @@ export { DataEntry } from './DataEntry.container'; export { getOpenDataEntryActions } from './helpers/getOpenDataEntryActions'; export { getRulesActions } from './helpers/getRulesActions'; -export { dataEntryId, itemId } from './helpers/constants'; diff --git a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/useRulesEngine.js b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/useRulesEngine.js index dec1061b8b..61e646ec29 100644 --- a/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/useRulesEngine.js +++ b/src/core_modules/capture-core/components/DataEntries/SingleEventRegistrationEntry/DataEntryWrapper/useRulesEngine.js @@ -1,15 +1,11 @@ // @flow -import { v4 as uuid } from 'uuid'; import { useEffect, useRef, useMemo } from 'react'; import { useDispatch, useSelector } from 'react-redux'; -import { useDataEngine } from '@dhis2/app-runtime'; -import { makeQuerySingleResource } from 'capture-core/utils/api'; import { batchActions } from 'redux-batched-actions'; import type { OrgUnit } from '@dhis2/rules-engine-javascript'; import { getEventProgramThrowIfNotFound } from '../../../../metaData'; -import { getRulesActions, dataEntryId, itemId } from './DataEntry'; +import { getRulesActions } from './DataEntry'; import type { RenderFoundation } from '../../../../metaData'; -import { startRunRulesPostLoadDataEntry } from '../../../DataEntry'; export const useRulesEngine = ({ programId, @@ -20,7 +16,6 @@ export const useRulesEngine = ({ orgUnit: ?OrgUnit, formFoundation: ?RenderFoundation, }) => { - const dataEngine = useDataEngine(); const dispatch = useDispatch(); const program = useMemo(() => programId && getEventProgramThrowIfNotFound(programId), [programId]); const orgUnitRef = useRef(); @@ -31,17 +26,14 @@ export const useRulesEngine = ({ const state = useSelector(stateArg => stateArg); useEffect(() => { if (orgUnit && program && !!formFoundation) { - const uid = uuid(); - dispatch(startRunRulesPostLoadDataEntry(dataEntryId, itemId, uid)); - const querySingleResource = makeQuerySingleResource(dataEngine.query.bind(dataEngine)); - getRulesActions({ - state, - program, - orgUnit, - formFoundation, - querySingleResource, - uid, - }).then(rulesActions => dispatch(batchActions(rulesActions))); + dispatch(batchActions([ + getRulesActions({ + state, + program, + orgUnit, + formFoundation, + }), + ])); orgUnitRef.current = orgUnit; } // Ignoring state (due to various reasons, bottom line being that field updates are handled in epic) diff --git a/src/core_modules/capture-core/components/DataEntry/actions/dataEntry.actions.js b/src/core_modules/capture-core/components/DataEntry/actions/dataEntry.actions.js index 59fe158f19..5b5bf1a6f6 100644 --- a/src/core_modules/capture-core/components/DataEntry/actions/dataEntry.actions.js +++ b/src/core_modules/capture-core/components/DataEntry/actions/dataEntry.actions.js @@ -18,12 +18,10 @@ export const actionTypes = { SAVE_ABORT: 'SaveAbortedForDataEntry', UPDATE_FIELD: 'UpdateDataEntryField', UPDATE_FORM_FIELD: 'UpdateDataEntryFormField', - RULES_EXECUTED_POST_LOAD_DATA_ENTRY: 'RulesExecutedPostLoadDataEntry', RULES_EXECUTED_POST_UPDATE_FIELD: 'RulesExecutedPostUpdateFieldDataEntry', ADD_DATA_ENTRY_NOTE: 'AddDataEntryNote', REMOVE_DATA_ENTRY_NOTE: 'RemoveDataEntryNote', SET_CURRENT_DATA_ENTRY: 'SetCurrentDataEntry', - START_RUN_RULES_POST_LOAD_DATA_ENTRY: 'StartRunRulesPostLoadDataEntry', START_RUN_RULES_POST_UPDATE_FIELD: 'StartRunRulesPostUpdateFieldDataEntry', REMOVE_DATA_ENTRY_RELATIONSHIP: 'RemoveDataEntryRelationship', ADD_DATA_ENTRY_RELATIONSHIP: 'AddDataEntryRelationship', @@ -118,14 +116,6 @@ export const updateFormField = updateCompleteUid, }); -export const startRunRulesPostLoadDataEntry = - (dataEntryId: string, itemId: string, uid: string) => - actionCreator(actionTypes.START_RUN_RULES_POST_LOAD_DATA_ENTRY)({ dataEntryId, itemId, uid }); - -export const rulesExecutedPostLoadDataEntry = - (dataEntryId: string, itemId: string, uid: string) => - actionCreator(actionTypes.RULES_EXECUTED_POST_LOAD_DATA_ENTRY)({ dataEntryId, itemId, uid }); - export const startRunRulesPostUpdateField = (dataEntryId: string, itemId: string, uid: string) => actionCreator(actionTypes.START_RUN_RULES_POST_UPDATE_FIELD)({ dataEntryId, itemId, uid }); diff --git a/src/core_modules/capture-core/components/DataEntry/index.js b/src/core_modules/capture-core/components/DataEntry/index.js index ac3a14f0f7..97ce464d1a 100644 --- a/src/core_modules/capture-core/components/DataEntry/index.js +++ b/src/core_modules/capture-core/components/DataEntry/index.js @@ -26,9 +26,7 @@ export { placements } from './constants/placements.const'; export { actionTypes as mainActionTypes, startRunRulesPostUpdateField, - startRunRulesPostLoadDataEntry, rulesExecutedPostUpdateField, - rulesExecutedPostLoadDataEntry, } from './actions/dataEntry.actions'; export { actionTypes as loadNewActionTypes } from './actions/dataEntryLoadNew.actions'; export { actionTypes as loadEditActionTypes, cleanUpDataEntry } from './actions/dataEntry.actions'; diff --git a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/helpers/getRulesActions.js b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/helpers/getRulesActions.js index 16575f74dd..d8aaa50247 100644 --- a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/helpers/getRulesActions.js +++ b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/DataEntry/helpers/getRulesActions.js @@ -6,14 +6,11 @@ import { getCurrentClientMainData, getApplicableRuleEffectsForTrackerProgram, updateRulesEffects, - validateAssignEffects, } from '../../../../rules'; import type { RenderFoundation, TrackerProgram, ProgramStage } from '../../../../metaData'; import type { EnrollmentEvents, AttributeValuesClientFormatted, EnrollmentData } from '../../common.types'; -import type { QuerySingleResource } from '../../../../utils/api'; -import { rulesExecutedPostLoadDataEntry } from '../../../DataEntry'; -export const getRulesActions = async ({ +export const getRulesActions = ({ state, // temporary program, stage, @@ -24,8 +21,6 @@ export const getRulesActions = async ({ eventsRulesDependency, attributesValuesRulesDependency, enrollmentDataRulesDependency, - querySingleResource, - uid, }: { state: ReduxState, program: TrackerProgram, @@ -37,8 +32,6 @@ export const getRulesActions = async ({ eventsRulesDependency: EnrollmentEvents, attributesValuesRulesDependency: AttributeValuesClientFormatted, enrollmentDataRulesDependency: EnrollmentData, - querySingleResource: QuerySingleResource, - uid: string, }) => { const formId = getDataEntryKey(dataEntryId, itemId); @@ -56,14 +49,5 @@ export const getRulesActions = async ({ enrollmentData: enrollmentDataRulesDependency, }); - const effectsWithValidations = await validateAssignEffects({ - dataElements: formFoundation.getElements(), - effects, - querySingleResource, - }); - - return [ - updateRulesEffects(effectsWithValidations, formId), - rulesExecutedPostLoadDataEntry(dataEntryId, itemId, uid), - ]; + return updateRulesEffects(effects, formId); }; diff --git a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/useLifecycle.js b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/useLifecycle.js index fa430e1731..bf41bf3e53 100644 --- a/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/useLifecycle.js +++ b/src/core_modules/capture-core/components/WidgetEnrollmentEventNew/Validated/useLifecycle.js @@ -1,16 +1,12 @@ // @flow import { useEffect, useRef, useState } from 'react'; -import { v4 as uuid } from 'uuid'; import { useDispatch, useSelector } from 'react-redux'; -import { useDataEngine } from '@dhis2/app-runtime'; -import { makeQuerySingleResource } from 'capture-core/utils/api'; import { batchActions } from 'redux-batched-actions'; import type { OrgUnit } from '@dhis2/rules-engine-javascript'; import { getOpenDataEntryActions, getRulesActions } from '../DataEntry'; import type { TrackerProgram, ProgramStage, RenderFoundation } from '../../../metaData'; import type { RulesExecutionDependenciesClientFormatted } from '../common.types'; import { useCategoryCombinations } from '../../DataEntryDhis2Helpers/AOC/useCategoryCombinations'; -import { startRunRulesPostLoadDataEntry } from '../../DataEntry'; export const useLifecycle = ({ program, @@ -33,7 +29,6 @@ export const useLifecycle = ({ itemId: string, rulesExecutionDependenciesClientFormatted: RulesExecutionDependenciesClientFormatted, }) => { - const dataEngine = useDataEngine(); const dispatch = useDispatch(); const [rulesExecutionTrigger, setRulesExecutionTrigger] = useState(1); @@ -67,24 +62,20 @@ export const useLifecycle = ({ delayRulesExecutionRef.current = false; setRulesExecutionTrigger(-rulesExecutionTrigger); } else { - const uid = uuid(); - dispatch(startRunRulesPostLoadDataEntry(dataEntryId, itemId, uid)); - const querySingleResource = makeQuerySingleResource(dataEngine.query.bind(dataEngine)); - - getRulesActions({ - state, - program, - stage, - formFoundation, - dataEntryId, - itemId, - orgUnit, - eventsRulesDependency, - attributesValuesRulesDependency, - enrollmentDataRulesDependency, - querySingleResource, - uid, - }).then(rulesActions => dispatch(batchActions(rulesActions))); + dispatch(batchActions([ + getRulesActions({ + state, + program, + stage, + formFoundation, + dataEntryId, + itemId, + orgUnit, + eventsRulesDependency, + attributesValuesRulesDependency, + enrollmentDataRulesDependency, + }), + ])); eventsRef.current = eventsRulesDependency; attributesRef.current = attributesValuesRulesDependency; enrollmentDataRef.current = enrollmentDataRulesDependency; diff --git a/src/core_modules/capture-core/reducers/descriptions/dataEntry.reducerDescription.js b/src/core_modules/capture-core/reducers/descriptions/dataEntry.reducerDescription.js index b79bcbe349..362c0c993d 100644 --- a/src/core_modules/capture-core/reducers/descriptions/dataEntry.reducerDescription.js +++ b/src/core_modules/capture-core/reducers/descriptions/dataEntry.reducerDescription.js @@ -336,17 +336,6 @@ export const dataEntriesInProgressListDesc = createReducerDescription({ ], }; }, - [actionTypes.START_RUN_RULES_POST_LOAD_DATA_ENTRY]: (state, action) => { - const { dataEntryId, itemId, uid } = action.payload; - const dataEntryKey = getDataEntryKey(dataEntryId, itemId); - return { - ...state, - [dataEntryKey]: [ - ...(state[dataEntryKey] || []), - uid, - ], - }; - }, [actionTypes.RULES_EXECUTED_POST_UPDATE_FIELD]: (state, action) => { const { dataEntryId, itemId, uid } = action.payload; const dataEntryKey = getDataEntryKey(dataEntryId, itemId); @@ -356,13 +345,4 @@ export const dataEntriesInProgressListDesc = createReducerDescription({ [dataEntryKey]: updatedList, }; }, - [actionTypes.RULES_EXECUTED_POST_LOAD_DATA_ENTRY]: (state, action) => { - const { dataEntryId, itemId, uid } = action.payload; - const dataEntryKey = getDataEntryKey(dataEntryId, itemId); - const updatedList = (state[dataEntryKey] || []).filter(item => item !== uid); - return { - ...state, - [dataEntryKey]: updatedList, - }; - }, }, 'dataEntriesInProgressList'); diff --git a/src/core_modules/capture-core/reducers/descriptions/form.reducerDescription.js b/src/core_modules/capture-core/reducers/descriptions/form.reducerDescription.js index 2499f29815..bed2120f6e 100644 --- a/src/core_modules/capture-core/reducers/descriptions/form.reducerDescription.js +++ b/src/core_modules/capture-core/reducers/descriptions/form.reducerDescription.js @@ -205,6 +205,7 @@ export const formsSectionsFieldsUIDesc = createReducerDescription({ [newPageActionTypes.CLEAN_UP_DATA_ENTRY]: cleanUp, [rulesEffectsActionTypes.UPDATE_RULES_EFFECTS]: (state, action) => { const { formId, rulesEffects } = action.payload; + const formSectionFields = state[formId]; const assignEffects: { [id: string]: Array } = rulesEffects && rulesEffects[effectActions.ASSIGN_VALUE]; @@ -214,14 +215,21 @@ export const formsSectionsFieldsUIDesc = createReducerDescription({ const updatedFields = Object.keys(assignEffects).reduce((acc, id) => { const effect = assignEffects[id][0]; - acc[id] = { - valid: effect.valid, - errorData: effect.errorData, - errorMessage: effect.errorMessage, - errorType: effect.errorType, - touched: true, - validatingMessage: null, - }; + const isEffectWithValidations = effect.hasOwnProperty('valid'); + if (formSectionFields?.[id] && isEffectWithValidations) { + acc[id] = { + valid: effect.valid, + errorData: effect.errorData, + errorMessage: effect.errorMessage, + errorType: effect.errorType, + touched: true, + validatingMessage: null, + }; + } else { + acc[id] = { + touched: true, + }; + } return acc; }, {});