2018-11-29 16:22:46 +00:00
|
|
|
import { findRuleByDottedName, nestedSituationToPathMap } from 'Engine/rules'
|
|
|
|
import { compose, filter, map, toPairs } from 'ramda'
|
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-12 15:02:07 +00:00
|
|
|
import { connect, useDispatch } from 'react-redux'
|
2018-11-29 16:22:46 +00:00
|
|
|
import { batchActions } from 'redux-batched-actions'
|
|
|
|
import { change, Field, reduxForm } from 'redux-form'
|
2018-11-21 17:16:51 +00:00
|
|
|
import {
|
2018-11-29 16:22:46 +00:00
|
|
|
flatRulesSelector,
|
2019-01-23 17:04:22 +00:00
|
|
|
situationSelector,
|
|
|
|
situationsWithDefaultsSelector
|
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
|
|
|
|
2018-11-16 14:13:18 +00:00
|
|
|
export default compose(
|
|
|
|
reduxForm({
|
|
|
|
form: 'conversation',
|
2019-01-23 17:04:22 +00:00
|
|
|
destroyOnUnmount: false
|
2018-11-16 14:13:18 +00:00
|
|
|
}),
|
2018-11-21 17:16:51 +00:00
|
|
|
connect(
|
2019-01-23 17:04:22 +00:00
|
|
|
state => {
|
|
|
|
let situation = situationsWithDefaultsSelector(state)
|
|
|
|
if (Array.isArray(situation)) {
|
|
|
|
situation = situation[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
rules: flatRulesSelector(state),
|
|
|
|
situation: nestedSituationToPathMap(situationSelector(state)),
|
|
|
|
initialPériode: situation.période
|
|
|
|
}
|
|
|
|
},
|
2018-11-21 17:16:51 +00:00
|
|
|
dispatch => ({
|
|
|
|
batchPeriodChange: actions => dispatch(batchActions(actions))
|
|
|
|
})
|
|
|
|
)
|
2019-01-23 17:04:22 +00:00
|
|
|
)(function PeriodSwitch({
|
|
|
|
situation,
|
|
|
|
rules,
|
|
|
|
batchPeriodChange,
|
|
|
|
initialPériode
|
|
|
|
}) {
|
2019-09-12 15:02:07 +00:00
|
|
|
const dispatch = useDispatch()
|
2019-01-23 17:04:22 +00:00
|
|
|
useEffect(() => {
|
|
|
|
!situation.période &&
|
|
|
|
updateSituation(
|
|
|
|
initialPériode || 'année',
|
|
|
|
batchPeriodChange,
|
|
|
|
situation,
|
2019-09-12 15:02:07 +00:00
|
|
|
rules,
|
|
|
|
updatePeriod
|
2019-01-23 17:04:22 +00:00
|
|
|
)
|
|
|
|
return
|
|
|
|
})
|
2019-09-12 15:02:07 +00:00
|
|
|
const updatePeriod = (toPeriod, needConvertion) =>
|
|
|
|
dispatch({ type: 'UPDATE_PERIOD', toPeriod, needConvertion })
|
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-03-27 16:05:27 +00:00
|
|
|
<label>
|
|
|
|
<Field
|
|
|
|
name="période"
|
|
|
|
component="input"
|
|
|
|
type="radio"
|
2019-04-03 14:02:36 +00:00
|
|
|
value="année"
|
2019-03-27 16:05:27 +00:00
|
|
|
onChange={() =>
|
2019-09-12 15:02:07 +00:00
|
|
|
updateSituation(
|
|
|
|
'année',
|
|
|
|
batchPeriodChange,
|
|
|
|
situation,
|
|
|
|
rules,
|
|
|
|
updatePeriod
|
|
|
|
)
|
2019-03-27 16:05:27 +00:00
|
|
|
}
|
|
|
|
/>
|
2019-04-02 16:56:44 +00:00
|
|
|
<span>
|
2019-04-03 14:02:36 +00:00
|
|
|
<Trans>année</Trans>
|
2019-03-27 16:05:27 +00:00
|
|
|
</span>
|
|
|
|
</label>
|
|
|
|
<label>
|
|
|
|
<Field
|
|
|
|
name="période"
|
|
|
|
component="input"
|
|
|
|
type="radio"
|
2019-04-03 14:02:36 +00:00
|
|
|
value="mois"
|
2019-03-27 16:05:27 +00:00
|
|
|
onChange={() =>
|
2019-09-12 15:02:07 +00:00
|
|
|
updateSituation(
|
|
|
|
'mois',
|
|
|
|
batchPeriodChange,
|
|
|
|
situation,
|
|
|
|
rules,
|
|
|
|
updatePeriod
|
|
|
|
)
|
2019-03-27 16:05:27 +00:00
|
|
|
}
|
|
|
|
/>
|
2019-04-02 16:56:44 +00:00
|
|
|
<span>
|
2019-04-03 14:02:36 +00:00
|
|
|
<Trans>mois</Trans>
|
2019-03-27 16:05:27 +00:00
|
|
|
</span>
|
|
|
|
</label>
|
2019-07-23 14:14:30 +00:00
|
|
|
</span>
|
|
|
|
</span>
|
2018-11-14 10:24:13 +00:00
|
|
|
)
|
2018-11-14 17:24:57 +00:00
|
|
|
})
|
2018-11-21 17:16:51 +00:00
|
|
|
|
2019-09-12 15:02:07 +00:00
|
|
|
let updateSituation = (
|
|
|
|
toPeriod,
|
|
|
|
batchPeriodChange,
|
|
|
|
situation,
|
|
|
|
rules,
|
|
|
|
updatePeriod
|
|
|
|
) => {
|
2018-12-07 15:28:39 +00:00
|
|
|
let needConvertion = filter(([dottedName, value]) => {
|
2018-11-21 17:16:51 +00:00
|
|
|
let rule = findRuleByDottedName(rules, dottedName)
|
2018-12-07 15:28:39 +00:00
|
|
|
return value != null && rule?.période === 'flexible'
|
2018-11-21 17:16:51 +00:00
|
|
|
})(toPairs(situation))
|
2019-09-12 15:02:07 +00:00
|
|
|
|
|
|
|
updatePeriod(toPeriod, needConvertion.map(([fieldName]) => fieldName))
|
|
|
|
|
2018-11-21 17:16:51 +00:00
|
|
|
let actions = [
|
|
|
|
...map(
|
|
|
|
([dottedName, value]) =>
|
|
|
|
change(
|
|
|
|
'conversation',
|
|
|
|
dottedName,
|
2018-11-23 12:57:20 +00:00
|
|
|
Math.round(
|
|
|
|
situation.période === 'mois' && toPeriod === 'année'
|
|
|
|
? value * 12
|
|
|
|
: situation.période === 'année' && toPeriod === 'mois'
|
|
|
|
? value / 12
|
2019-09-11 08:04:19 +00:00
|
|
|
: (function() {
|
2018-11-23 12:57:20 +00:00
|
|
|
throw new Error('Oups, changement de période invalide')
|
2019-09-11 08:04:19 +00:00
|
|
|
})()
|
2018-12-17 10:45:06 +00:00
|
|
|
) + ''
|
2018-11-21 17:16:51 +00:00
|
|
|
),
|
2018-12-07 15:28:39 +00:00
|
|
|
needConvertion
|
2018-11-21 17:16:51 +00:00
|
|
|
),
|
|
|
|
change('conversation', 'période', toPeriod)
|
|
|
|
]
|
|
|
|
|
|
|
|
batchPeriodChange(actions)
|
|
|
|
}
|