import { updateSituation } from 'Actions/actions' import { T } from 'Components' import InputSuggestions from 'Components/conversation/InputSuggestions' import PeriodSwitch from 'Components/PeriodSwitch' import RuleLink from 'Components/RuleLink' import { ThemeColoursContext } from 'Components/utils/withColours' import withSitePaths from 'Components/utils/withSitePaths' import { encodeRuleName } from 'Engine/rules' import { isEmpty, isNil } from 'ramda' import React, { useEffect, useState, useContext } from 'react' import emoji from 'react-easy-emoji' import { useTranslation } from 'react-i18next' import { useDispatch, useSelector } from 'react-redux' import { Link } from 'react-router-dom' import { formatCurrency } from 'Engine/format' import { analysisWithDefaultsSelector, useSituation, useSituationValue, useTarget } from 'Selectors/analyseSelectors' import Animate from 'Ui/animate' import AnimatedTargetValue from 'Ui/AnimatedTargetValue' import CurrencyInput from './CurrencyInput/CurrencyInput' import './TargetSelection.css' export default function TargetSelection() { const [initialRender, setInitialRender] = useState(true) const analysis = useSelector(analysisWithDefaultsSelector) const objectifs = useSelector( state => state.simulation?.config.objectifs || [] ) const secondaryObjectives = useSelector( state => state.simulation?.config['objectifs secondaires'] || [] ) const situation = useSituation() const dispatch = useDispatch() const colours = useContext(ThemeColoursContext) const targets = analysis?.targets.filter( t => !secondaryObjectives.includes(t.dottedName) && t.dottedName !== 'contrat salarié . aides employeur' ) || [] useEffect(() => { // Initialize defaultValue for target that can't be computed // TODO: this logic shouldn't be here targets .filter( target => (!target.formule || isEmpty(target.formule)) && (!isNil(target.defaultValue) || !isNil(target.explanation?.defaultValue)) && !situation[target.dottedName] ) .forEach(target => { dispatch( updateSituation( target.dottedName, !isNil(target.defaultValue) ? target.defaultValue : target.explanation?.defaultValue ) ) }) setInitialRender(false) // eslint-disable-next-line react-hooks/exhaustive-deps }, []) return (
{target.summary}
) }) let TargetInputOrValue = ({ target, isActiveInput, isSmallTarget }) => { const { language } = useTranslation().i18n const colors = useContext(ThemeColoursContext) const dispatch = useDispatch() const situationValue = Math.round(useSituationValue(target.dottedName)) const targetWithValue = useTarget(target.dottedName) const value = targetWithValue?.nodeValue ? Math.round(targetWithValue?.nodeValue) : undefined const inversionFail = useSelector( state => analysisWithDefaultsSelector(state)?.cache.inversionFail ) const blurValue = inversionFail && !isActiveInput && value return ( {target.question ? ( <> {!isActiveInput &&