Skip to content

Commit

Permalink
Utilisation de redux-thunk pour updatePeriod
Browse files Browse the repository at this point in the history
  • Loading branch information
mquandalle committed Sep 23, 2019
1 parent 7a965e6 commit f224373
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
7 changes: 7 additions & 0 deletions source/actions/actions.js
Expand Up @@ -67,6 +67,13 @@ export const updateSituation = (fieldName, value) => ({
value
})

export const updatePeriod = toPeriod => (dispatch, getState) =>
dispatch({
type: 'UPDATE_PERIOD',
rules: getState().rules,
toPeriod
})

// $FlowFixMe
export function setExample(name, situation, dottedName) {
return { type: 'SET_EXAMPLE', name, situation, dottedName }
Expand Down
6 changes: 3 additions & 3 deletions 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'
Expand All @@ -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 (
Expand All @@ -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}
/>
<span>
Expand Down
40 changes: 17 additions & 23 deletions source/reducers/rootReducer.js
Expand Up @@ -201,26 +201,20 @@ 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)
}
export default 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
})
)

0 comments on commit f224373

Please sign in to comment.