diff --git a/source/components/TargetSelection.js b/source/components/TargetSelection.js index 5d5074a81..faca0497b 100644 --- a/source/components/TargetSelection.js +++ b/source/components/TargetSelection.js @@ -252,12 +252,6 @@ let TargetInputOrValue = ({ target, isActiveInput, isSmallTarget }) => { type: 'SET_ACTIVE_TARGET_INPUT', name: target.dottedName }) - // TODO: This shouldn't be necessary: we don't need to recalculate the situation - // when the user just focus another field. Removing this line is almost working - // however there is a weird bug in the selection of the next question. - if (value) { - dispatch(updateSituation(target.dottedName, value)) - } }} {...(isActiveInput ? { autoFocus: true } : {})} language={i18n.language} diff --git a/source/reducers/rootReducer.js b/source/reducers/rootReducer.js index de2612ee3..c6ec826f1 100644 --- a/source/reducers/rootReducer.js +++ b/source/reducers/rootReducer.js @@ -18,6 +18,7 @@ import i18n from '../i18n' import inFranceAppReducer from './inFranceAppReducer' import storageReducer from './storageReducer' import { findRuleByDottedName } from 'Engine/rules' +import { targetNamesSelector } from 'Selectors/analyseSelectors' import type { Action } from 'Types/ActionsTypes' function explainedVariable(state = null, { type, variableName = null }) { @@ -100,9 +101,16 @@ function conversationSteps( return state } -function updateSituation(situation, { fieldName, value, config }) { - const removePreviousTarget = config.objectifs.includes(fieldName) - ? omit(config.objectifs) +function updateSituation(situation, { fieldName, value, config, rules }) { + const goals = targetNamesSelector({ simulation: { config } }).filter( + dottedName => { + const target = rules.find(r => r.dottedName === dottedName) + const isSmallTarget = !target.question || !target.formule + return !isSmallTarget + } + ) + const removePreviousTarget = goals.includes(fieldName) + ? omit(goals) : identity return { ...removePreviousTarget(situation), [fieldName]: value } } @@ -154,7 +162,8 @@ function simulation(state = null, action, rules) { situation: updateSituation(state.situation, { fieldName: action.fieldName, value: action.value, - config: state.config + config: state.config, + rules }) } case 'UPDATE_PERIOD': @@ -162,7 +171,7 @@ function simulation(state = null, action, rules) { ...state, situation: updatePeriod(state.situation, { toPeriod: action.toPeriod, - rules: rules + rules }) } } diff --git a/source/selectors/analyseSelectors.js b/source/selectors/analyseSelectors.js index df197b4ee..39f97d4a8 100644 --- a/source/selectors/analyseSelectors.js +++ b/source/selectors/analyseSelectors.js @@ -116,11 +116,8 @@ export let firstStepCompletedSelector = createSelector( ) let validatedStepsSelector = createSelector( - [ - state => state.conversationSteps.foldedSteps, - state => state.activeTargetInput - ], - (foldedSteps, target) => [...foldedSteps, target] + [state => state.conversationSteps.foldedSteps, targetNamesSelector], + (foldedSteps, targetNames) => [...foldedSteps, ...targetNames] ) let branchesSelector = state => state.simulation?.config.branches let configSituationSelector = state => state.simulation?.config.situation || {}