Skip to content

Commit

Permalink
chore: skip assigned values validation when opening new event forms
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru committed Nov 25, 2024
1 parent 8dfd816 commit 360933e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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();
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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 });
Expand Down
2 changes: 0 additions & 2 deletions src/core_modules/capture-core/components/DataEntry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,8 +21,6 @@ export const getRulesActions = async ({
eventsRulesDependency,
attributesValuesRulesDependency,
enrollmentDataRulesDependency,
querySingleResource,
uid,
}: {
state: ReduxState,
program: TrackerProgram,
Expand All @@ -37,8 +32,6 @@ export const getRulesActions = async ({
eventsRulesDependency: EnrollmentEvents,
attributesValuesRulesDependency: AttributeValuesClientFormatted,
enrollmentDataRulesDependency: EnrollmentData,
querySingleResource: QuerySingleResource,
uid: string,
}) => {
const formId = getDataEntryKey(dataEntryId, itemId);

Expand All @@ -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);
};
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -33,7 +29,6 @@ export const useLifecycle = ({
itemId: string,
rulesExecutionDependenciesClientFormatted: RulesExecutionDependenciesClientFormatted,
}) => {
const dataEngine = useDataEngine();
const dispatch = useDispatch();
const [rulesExecutionTrigger, setRulesExecutionTrigger] = useState(1);

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssignOutputEffectWithValidations> } =
rulesEffects && rulesEffects[effectActions.ASSIGN_VALUE];

Expand All @@ -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;
}, {});

Expand Down

0 comments on commit 360933e

Please sign in to comment.