From da077aa8518c511357e74fc3fba8cee99fc19f85 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Mon, 23 Sep 2019 09:44:22 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20d'un=20param=C3=A8tre=20explicite=20"ru?= =?UTF-8?q?les"=20au=20reducer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/actions/actions.js | 5 +++ source/components/PeriodSwitch.js | 6 ++-- source/reducers/rootReducer.js | 52 ++++++++++++++++--------------- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/source/actions/actions.js b/source/actions/actions.js index 6f330e6237..1fe6bf54a0 100644 --- a/source/actions/actions.js +++ b/source/actions/actions.js @@ -67,6 +67,11 @@ export const updateSituation = (fieldName, value) => ({ value }) +export const updatePeriod = toPeriod => ({ + type: 'UPDATE_PERIOD', + toPeriod +}) + // $FlowFixMe export function setExample(name, situation, dottedName) { return { type: 'SET_EXAMPLE', name, situation, dottedName } diff --git a/source/components/PeriodSwitch.js b/source/components/PeriodSwitch.js index e0225cc412..34bba977d8 100644 --- a/source/components/PeriodSwitch.js +++ b/source/components/PeriodSwitch.js @@ -1,3 +1,4 @@ +import { updatePeriod } from 'Actions/actions' import React from 'react' import { Trans } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' @@ -12,10 +13,9 @@ export default function PeriodSwitch() { ) const currentPeriod = situation.période let periods = ['année', 'mois'] - const updatePeriod = toPeriod => dispatch({ type: 'UPDATE_PERIOD', toPeriod }) if (!currentPeriod) { - updatePeriod(defaultPeriod) + dispatch(updatePeriod(defaultPeriod)) } return ( @@ -27,7 +27,7 @@ export default function PeriodSwitch() { name="période" type="radio" value={period} - onChange={() => updatePeriod(period)} + onChange={() => dispatch(updatePeriod(period))} checked={currentPeriod === period} /> diff --git a/source/reducers/rootReducer.js b/source/reducers/rootReducer.js index a5cc2cd370..25f9f00ae2 100644 --- a/source/reducers/rootReducer.js +++ b/source/reducers/rootReducer.js @@ -135,7 +135,7 @@ function updatePeriod(situation, { toPeriod, rules }) { } } -function simulation(state = null, action) { +function simulation(state = null, action, rules) { if (action.type === 'SET_SIMULATION') { const { config, url } = action return { config, url, hiddenControls: [], situation: {} } @@ -162,7 +162,7 @@ function simulation(state = null, action) { ...state, situation: updatePeriod(state.situation, { toPeriod: action.toPeriod, - rules: action.rules + rules: rules }) } } @@ -201,26 +201,28 @@ const existingCompanyReducer = (state, action) => { } return newState } -export default (state, action) => { - // Enrich the action - if (action.type === 'UPDATE_PERIOD') { - action.rules = state.rules - } - return reduceReducers( - existingCompanyReducer, - storageReducer, - combineReducers({ - sessionId: defaultTo(Math.floor(Math.random() * 1000000000000) + ''), - conversationSteps, - lang, - rules: defaultTo(null), - simulation, - explainedVariable, - previousSimulation: defaultTo(null), - currentExample, - situationBranch, - activeTargetInput, - inFranceApp: inFranceAppReducer - }) - )(state, action) -} + +// We need to access the `rules` in the simulation reducer, so we can't call it from the +// global combineReducers below. +const simulationReducer = (state = null, action) => ({ + ...state, + simulation: simulation(state.simulation, action, state.rules) +}) + +export default reduceReducers( + existingCompanyReducer, + storageReducer, + simulationReducer, + combineReducers({ + sessionId: defaultTo(Math.floor(Math.random() * 1000000000000) + ''), + conversationSteps, + lang, + rules: defaultTo(null), + explainedVariable, + previousSimulation: defaultTo(null), + currentExample, + situationBranch, + activeTargetInput, + inFranceApp: inFranceAppReducer + }) +)