2019-09-17 18:10:27 +00:00
|
|
|
import React 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'
|
2019-09-17 18:10:27 +00:00
|
|
|
import { situationSelector } 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 situation = useSelector(situationSelector)
|
2019-09-17 18:10:27 +00:00
|
|
|
const defaultPeriod = useSelector(
|
|
|
|
state => state.simulation?.config?.situation?.période || 'année'
|
2019-09-13 10:42:19 +00:00
|
|
|
)
|
2019-09-15 20:51:13 +00:00
|
|
|
const currentPeriod = situation.période
|
2019-09-17 10:13:14 +00:00
|
|
|
let periods = ['mois', 'année']
|
2019-09-17 18:10:27 +00:00
|
|
|
const updatePeriod = toPeriod => dispatch({ type: 'UPDATE_PERIOD', toPeriod })
|
|
|
|
|
|
|
|
if (!currentPeriod) {
|
|
|
|
updatePeriod(defaultPeriod)
|
|
|
|
}
|
|
|
|
if (defaultPeriod === 'année') {
|
2019-09-17 10:13:14 +00:00
|
|
|
periods.reverse()
|
|
|
|
}
|
2019-09-13 10:42:19 +00:00
|
|
|
|
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
|
|
|
}
|