2019-09-13 10:42:19 +00:00
|
|
|
import { findRuleByDottedName } from 'Engine/rules'
|
2019-01-23 17:04:22 +00:00
|
|
|
import React, { useEffect } from 'react'
|
2019-09-11 08:06:51 +00:00
|
|
|
import { Trans } from 'react-i18next'
|
2019-09-13 10:42:19 +00:00
|
|
|
import { useDispatch, useSelector } from 'react-redux'
|
2018-11-21 17:16:51 +00:00
|
|
|
import {
|
2018-11-29 16:22:46 +00:00
|
|
|
flatRulesSelector,
|
2019-09-13 10:42:19 +00:00
|
|
|
situationSelector
|
2018-11-21 17:16:51 +00:00
|
|
|
} from 'Selectors/analyseSelectors'
|
2018-11-29 16:22:46 +00:00
|
|
|
import './PeriodSwitch.css'
|
2018-11-14 10:24:13 +00:00
|
|
|
|
2019-09-13 10:42:19 +00:00
|
|
|
export default function PeriodSwitch() {
|
2019-09-12 15:02:07 +00:00
|
|
|
const dispatch = useDispatch()
|
2019-09-13 10:42:19 +00:00
|
|
|
const rules = useSelector(flatRulesSelector)
|
|
|
|
const situation = useSelector(situationSelector)
|
|
|
|
const initialPeriod = useSelector(
|
|
|
|
state => state.simulation?.config?.situation?.période
|
|
|
|
)
|
2019-01-23 17:04:22 +00:00
|
|
|
useEffect(() => {
|
2019-09-13 10:42:19 +00:00
|
|
|
!currentPeriod && updatePeriod(initialPeriod || 'année')
|
|
|
|
}, [])
|
|
|
|
const currentPeriod = situation.période
|
|
|
|
const updatePeriod = toPeriod => {
|
|
|
|
const needConversion = Object.keys(situation).filter(dottedName => {
|
|
|
|
const rule = findRuleByDottedName(rules, dottedName)
|
|
|
|
return rule?.période === 'flexible'
|
|
|
|
})
|
|
|
|
dispatch({ type: 'UPDATE_PERIOD', toPeriod, needConversion })
|
|
|
|
}
|
|
|
|
const periods = ['mois', 'année']
|
|
|
|
|
2018-11-14 10:24:13 +00:00
|
|
|
return (
|
2019-07-23 14:14:30 +00:00
|
|
|
<span id="PeriodSwitch">
|
|
|
|
<span className="base ui__ small toggle">
|
2019-09-13 10:42:19 +00:00
|
|
|
{periods.map(period => (
|
|
|
|
<label key={period}>
|
|
|
|
<input
|
|
|
|
name="période"
|
|
|
|
type="radio"
|
|
|
|
value={period}
|
|
|
|
onChange={() => updatePeriod(period)}
|
|
|
|
checked={currentPeriod === period}
|
|
|
|
/>
|
|
|
|
<span>
|
|
|
|
<Trans>{period}</Trans>
|
|
|
|
</span>
|
|
|
|
</label>
|
|
|
|
))}
|
2019-07-23 14:14:30 +00:00
|
|
|
</span>
|
|
|
|
</span>
|
2018-11-14 10:24:13 +00:00
|
|
|
)
|
2018-11-21 17:16:51 +00:00
|
|
|
}
|