mon-entreprise/source/components/TargetSelection.js

205 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-11-15 09:49:55 +00:00
import React, { Component } from 'react'
import { Trans, translate } from 'react-i18next'
import formValueTypes from 'Components/conversation/formValueTypes'
import { rules, findRuleByName } from 'Engine/rules'
import { propEq, path, contains, without, curry, append, ifElse } from 'ramda'
import './TargetSelection.css'
import BlueButton from './BlueButton'
import { Field, reduxForm, formValueSelector, change } from 'redux-form'
2018-03-29 09:50:34 +00:00
import { Link } from 'react-router-dom'
import { connect } from 'react-redux'
import { RuleValue } from './rule/RuleValueVignette'
2018-03-14 17:20:09 +00:00
import classNames from 'classnames'
import { buildValidationFunction } from './conversation/FormDecorator'
2018-03-15 11:18:21 +00:00
export let salaries = ['salaire total', 'salaire de base', 'salaire net']
export let popularTargetNames = [...salaries, 'aides employeur']
import { Circle } from 'rc-progress'
@translate()
@reduxForm({
form: 'conversation'
})
@connect(
state => ({
getTargetValue: dottedName =>
formValueSelector('conversation')(state, dottedName),
targets: state.analysis ? state.analysis.targets : [],
flatRules: state.flatRules,
conversationStarted: state.conversationStarted,
missingVariablesByTarget: state.missingVariablesByTarget,
activeInput: state.activeTargetInput
}),
dispatch => ({
setFormValue: (field, name) =>
dispatch(change('conversation', field, name)),
startConversation: () => dispatch({ type: 'START_CONVERSATION' }),
setActiveInput: name => dispatch({ type: 'SET_ACTIVE_TARGET_INPUT', name })
})
)
2017-11-15 09:49:55 +00:00
export default class TargetSelection extends Component {
render() {
let { targets, conversationStarted, colours } = this.props
this.firstEstimationComplete = this.props.activeInput && targets.length > 0
2017-11-15 09:49:55 +00:00
return (
2018-03-14 17:20:09 +00:00
<div id="targetSelection">
2018-03-12 09:38:29 +00:00
<section
2018-03-14 17:20:09 +00:00
id="targetsContainer"
2018-03-12 09:38:29 +00:00
style={{
background: colours.colour,
color: colours.textColour
}}>
2018-03-12 09:38:29 +00:00
{this.renderOutputList()}
</section>
2018-04-05 16:09:18 +00:00
{!this.firstEstimationComplete && <h1>Entrez un salaire mensuel </h1>}
{this.firstEstimationComplete &&
!conversationStarted && (
<div id="action">
<p>
<b>Estimation approximative</b> <br /> pour une situation par
défaut (CDI non cadre).
</p>
<BlueButton onClick={this.props.startConversation}>
Affiner le calcul
</BlueButton>
</div>
)}
2018-03-14 17:20:09 +00:00
</div>
2017-11-15 09:49:55 +00:00
)
}
renderOutputList() {
let popularTargets = popularTargetNames.map(
curry(findRuleByName)(flatRules)
),
{
textColourOnWhite,
missingVariablesByTarget,
conversationStarted,
activeInput,
setActiveInput
} = this.props
2017-11-15 09:49:55 +00:00
return (
<div>
2018-03-22 17:54:19 +00:00
<ul id="targets">
{popularTargets.map(s => (
2018-03-22 17:54:19 +00:00
<li key={s.name}>
<span className="header">
{conversationStarted && (
<span
className="progressCircle"
style={{
visibility:
activeInput === s.dottedName ? 'hidden' : 'visible'
}}
>
{do {
let mv = missingVariablesByTarget[s.dottedName],
number = mv && mv.missingVariables.length,
ratio = number / 16
ratio === 0 ? (
<i className="fa fa-check" aria-hidden="true" />
) : (
<Circle
percent={100 - ratio * 100}
strokeWidth="15"
strokeColor="#5de662"
trailColor="#fff"
trailWidth="5"
/>
)
}}
</span>
)}
<span className="texts">
<span className="optionTitle">
<Link to={'/règle/' + s.dottedName}>
{s.title || s.name}
</Link>
</span>
{!conversationStarted && <p>{s['résumé']}</p>}
2018-03-29 09:50:34 +00:00
</span>
2018-03-22 17:54:19 +00:00
</span>
<TargetInputOrValue
{...{
s,
targets: this.props.targets,
firstEstimationComplete: this.firstEstimationComplete,
activeInput,
setActiveInput,
2018-03-22 17:54:19 +00:00
setFormValue: this.props.setFormValue
}}
/>
</li>
))}
2018-03-22 17:54:19 +00:00
</ul>
</div>
2017-11-15 09:49:55 +00:00
)
}
}
let validate = buildValidationFunction(formValueTypes['euros'])
let InputComponent = ({ input, meta: { dirty, error } }) => (
<span>
{dirty && error && <span className="input-error">{error}</span>}
<input type="number" {...input} autoFocus />
</span>
)
let TargetInputOrValue = ({
s,
targets,
firstEstimationComplete,
activeInput,
setActiveInput,
clearFormValue
}) => (
<span className="targetInputOrValue">
{activeInput === s.dottedName ? (
<Field
name={s.dottedName}
component={InputComponent}
type="text"
validate={validate}
/>
) : (
<TargetValue {...{ targets, s, activeInput, setActiveInput }} />
)}
{(firstEstimationComplete || s.question) && <span className="unit"></span>}
</span>
)
@connect(
() => ({}),
dispatch => ({
setFormValue: (field, name) => dispatch(change('conversation', field, name))
})
)
class TargetValue extends Component {
render() {
let { targets, s, setFormValue, activeInput, setActiveInput } = this.props,
rule = targets.find(propEq('dottedName', s.dottedName)),
value = rule && rule.nodeValue,
humanValue = value != null && value.toFixed(0)
return (
<span
className={classNames({
editable: s.question,
attractClick: s.question && targets.length === 0
})}
onClick={() => {
if (!s.question) return
if (value != null) {
setFormValue(s.dottedName, humanValue + '')
setFormValue(activeInput, '')
}
setActiveInput(s.dottedName)
}}>
<RuleValue value={value} />
</span>
)
}
}