import classNames from 'classnames' import { T } from 'Components' import InputSuggestions from 'Components/conversation/InputSuggestions' import PercentageField from 'Components/PercentageField' import PeriodSwitch from 'Components/PeriodSwitch' import withColours from 'Components/utils/withColours' import withLanguage from 'Components/utils/withLanguage' import withSitePaths from 'Components/utils/withSitePaths' import { encodeRuleName } from 'Engine/rules' import { compose, isEmpty, isNil, propEq } from 'ramda' import React, { Component, PureComponent } from 'react' import emoji from 'react-easy-emoji' import { withTranslation } from 'react-i18next' import { connect } from 'react-redux' import { Link } from 'react-router-dom' import { change, Field, formValueSelector, reduxForm } from 'redux-form' import { analysisWithDefaultsSelector, flatRulesSelector } from 'Selectors/analyseSelectors' import Animate from 'Ui/animate' import AnimatedTargetValue from 'Ui/AnimatedTargetValue' import CurrencyInput from './CurrencyInput/CurrencyInput' import './TargetSelection.css' import { serialiseUnit } from 'Engine/units' export default compose( withTranslation(), withColours, reduxForm({ form: 'conversation', destroyOnUnmount: false }), connect( state => ({ getTargetValue: dottedName => formValueSelector('conversation')(state, dottedName), analysis: analysisWithDefaultsSelector(state), flatRules: flatRulesSelector(state), activeInput: state.activeTargetInput, objectifs: state.simulation?.config.objectifs || [], secondaryObjectives: state.simulation?.config['objectifs secondaires'] || [] }), dispatch => ({ setFormValue: (field, name) => dispatch(change('conversation', field, name)), setActiveInput: name => dispatch({ type: 'SET_ACTIVE_TARGET_INPUT', name }) }) ) )( class TargetSelection extends PureComponent { state = { initialRender: true } getTargets() { let { secondaryObjectives, analysis } = this.props if (!analysis) return [] return analysis.targets.filter( t => !secondaryObjectives.includes(t.dottedName) ) } componentDidMount() { const props = this.props let targets = this.getTargets() // Initialize defaultValue for target that can't be computed targets .filter( target => (!target.formule || isEmpty(target.formule)) && (!isNil(target.defaultValue) || !isNil(target.explanation?.defaultValue)) && !props.getTargetValue(target.dottedName) ) .forEach(target => { props.setFormValue( target.dottedName, !isNil(target.defaultValue) ? target.defaultValue : target.explanation?.defaultValue ) }) if (this.state.initialRender) { this.setState({ initialRender: false }) } } render() { let { colours, activeInput, setActiveInput, setFormValue, objectifs } = this.props, targets = this.getTargets() return (
{(typeof objectifs[0] === 'string' ? [{ objectifs }] : objectifs).map( ({ icône, objectifs: groupTargets, nom }, index) => (
{nom && (

{emoji(icône)} {nom}

)}
{index === 0 && }
groupTargets.includes(dottedName) ), initialRender: this.state.initialRender }} />
) )}
) } } ) let Targets = ({ activeInput, setActiveInput, setFormValue, targets, initialRender }) => (
) const Target = ({ target, activeInput, targets, setActiveInput, setFormValue, initialRender }) => { const isSmallTarget = !target.question || !target.formule || isEmpty(target.formule) return (
  • {isSmallTarget && ( )}
    {activeInput === target.dottedName && ( { setFormValue(target.dottedName, '' + value) }} rulePeriod={target.période} colouredBackground={true} /> )}
  • ) } let Header = withSitePaths(({ target, sitePaths }) => { const ruleLink = sitePaths.documentation.index + '/' + encodeRuleName(target.dottedName) return ( {target.title || target.name}

    {target.summary}

    ) }) let CurrencyField = withColours(props => { return ( ) }) let DebouncedPercentageField = props => ( ) let TargetInputOrValue = withLanguage( ({ target, targets, activeInput, setActiveInput, language, firstStepCompleted }) => { let inputIsActive = activeInput === target.dottedName return ( {inputIsActive || !target.formule || isEmpty(target.formule) ? ( event.preventDefault()} component={ { '€': CurrencyField, '%': DebouncedPercentageField }[ serialiseUnit(target.unit) ] } {...(inputIsActive ? { autoFocus: true } : {})} language={language} /> ) : ( )} ) } ) const TargetValue = connect( state => ({ blurValue: analysisWithDefaultsSelector(state)?.cache.inversionFail }), dispatch => ({ setFormValue: (field, name) => dispatch(change('conversation', field, name)) }) )( class TargetValue extends Component { render() { let { targets, target, blurValue } = this.props let targetWithValue = targets && targets.find(propEq('dottedName', target.dottedName)), value = targetWithValue && targetWithValue.nodeValue return (
    ) } showField(value) { let { target, setFormValue, activeInput, setActiveInput } = this.props return () => { if (!target.question) return if (value != null && !Number.isNaN(value)) setFormValue(target.dottedName, Math.round(value) + '') if (activeInput) setFormValue(activeInput, '') setActiveInput(target.dottedName) } } } )