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-02-08 11:45:14 +00:00
|
|
|
import { Trans, withTranslation } from 'react-i18next'
|
2018-11-21 17:16:51 +00:00
|
|
|
import { connect } 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
|
|
|
}),
|
2019-02-08 11:45:14 +00:00
|
|
|
withTranslation(),
|
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
|
|
|
|
}) {
|
|
|
|
useEffect(() => {
|
|
|
|
!situation.période &&
|
|
|
|
updateSituation(
|
|
|
|
initialPériode || 'année',
|
|
|
|
batchPeriodChange,
|
|
|
|
situation,
|
|
|
|
rules
|
|
|
|
)
|
|
|
|
return
|
|
|
|
})
|
2018-11-14 10:24:13 +00:00
|
|
|
return (
|
2018-11-14 17:24:57 +00:00
|
|
|
<div id="PeriodSwitch">
|
2019-04-02 16:56:44 +00:00
|
|
|
<div 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-04-03 14:02:36 +00:00
|
|
|
updateSituation('année', batchPeriodChange, situation, rules)
|
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-04-03 14:02:36 +00:00
|
|
|
updateSituation('mois', batchPeriodChange, situation, rules)
|
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-04-02 16:56:44 +00:00
|
|
|
</div>
|
2018-11-14 17:24:57 +00:00
|
|
|
</div>
|
2018-11-14 10:24:13 +00:00
|
|
|
)
|
2018-11-14 17:24:57 +00:00
|
|
|
})
|
2018-11-21 17:16:51 +00:00
|
|
|
|
|
|
|
let updateSituation = (toPeriod, batchPeriodChange, situation, rules) => {
|
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))
|
|
|
|
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
|
|
|
|
: do {
|
|
|
|
throw new Error('Oups, changement de période invalide')
|
|
|
|
}
|
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)
|
|
|
|
}
|