2017-11-15 09:49:55 +00:00
|
|
|
import React, { Component } from 'react'
|
2018-03-29 07:58:20 +00:00
|
|
|
import { Trans, translate } from 'react-i18next'
|
2018-03-21 11:02:24 +00:00
|
|
|
import formValueTypes from 'Components/conversation/formValueTypes'
|
2018-02-28 16:45:13 +00:00
|
|
|
import { rules, findRuleByName } from 'Engine/rules'
|
2018-03-21 11:02:24 +00:00
|
|
|
import { propEq, contains, without, curry, append, ifElse } from 'ramda'
|
2017-11-16 17:11:42 +00:00
|
|
|
import './TargetSelection.css'
|
2018-01-10 16:51:35 +00:00
|
|
|
import BlueButton from './BlueButton'
|
2018-03-21 17:35:15 +00:00
|
|
|
import { Field, reduxForm, formValueSelector, change } from 'redux-form'
|
2018-03-29 09:50:34 +00:00
|
|
|
import { Link } from 'react-router-dom'
|
2018-02-28 16:45:13 +00:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import { RuleValue } from './rule/RuleValueVignette'
|
2018-03-14 17:20:09 +00:00
|
|
|
import classNames from 'classnames'
|
2018-03-21 11:02:24 +00:00
|
|
|
import { buildValidationFunction } from './conversation/FormDecorator'
|
2018-03-15 11:18:21 +00:00
|
|
|
export let salaries = ['salaire total', 'salaire de base', 'salaire net']
|
2018-03-12 15:22:16 +00:00
|
|
|
export let popularTargetNames = [...salaries, 'aides employeur']
|
2017-12-07 14:19:51 +00:00
|
|
|
|
2018-03-29 07:58:20 +00:00
|
|
|
@translate()
|
2018-02-28 16:45:13 +00:00
|
|
|
@reduxForm({
|
|
|
|
form: 'conversation'
|
|
|
|
})
|
|
|
|
@connect(
|
|
|
|
state => ({
|
|
|
|
getTargetValue: dottedName =>
|
|
|
|
formValueSelector('conversation')(state, dottedName),
|
|
|
|
targets: state.analysis ? state.analysis.targets : [],
|
|
|
|
flatRules: state.flatRules
|
2018-03-12 15:22:16 +00:00
|
|
|
conversationTargetNames: state.conversationTargetNames
|
2018-02-28 16:45:13 +00:00
|
|
|
}),
|
|
|
|
dispatch => ({
|
2018-03-22 10:04:47 +00:00
|
|
|
setFormValue: (field, name) =>
|
|
|
|
dispatch(change('conversation', field, name)),
|
2018-03-12 15:22:16 +00:00
|
|
|
setConversationTargets: (targetNames, fromScratch = false) =>
|
|
|
|
dispatch({ type: 'SET_CONVERSATION_TARGETS', targetNames, fromScratch })
|
2018-02-28 16:45:13 +00:00
|
|
|
})
|
|
|
|
)
|
2017-11-15 09:49:55 +00:00
|
|
|
export default class TargetSelection extends Component {
|
|
|
|
state = {
|
2018-03-12 09:38:29 +00:00
|
|
|
activeInput: null
|
2018-02-28 16:45:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 09:49:55 +00:00
|
|
|
render() {
|
2018-03-29 10:10:09 +00:00
|
|
|
let { targets, conversationTargetNames, colours } = this.props
|
2018-03-22 16:41:45 +00:00
|
|
|
this.firstEstimationComplete = this.state.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={{
|
2018-03-29 10:10:09 +00:00
|
|
|
background: colours.colour,
|
|
|
|
color: colours.textColour
|
2018-03-12 09:38:29 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{this.renderOutputList()}
|
|
|
|
</section>
|
2018-03-29 10:10:09 +00:00
|
|
|
{!this.firstEstimationComplete && (
|
|
|
|
<h1>
|
|
|
|
Entrez un salaire mensuel{' '}
|
|
|
|
<i
|
|
|
|
style={{ color: colours.textColourOnWhite }}
|
|
|
|
className="fa fa-calendar"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
</h1>
|
|
|
|
)}
|
2018-02-28 16:45:13 +00:00
|
|
|
|
2018-03-19 14:42:41 +00:00
|
|
|
{this.firstEstimationComplete && (
|
2018-02-28 16:45:13 +00:00
|
|
|
<div id="action">
|
2018-03-22 16:41:45 +00:00
|
|
|
{conversationTargetNames ? (
|
|
|
|
!conversationTargetNames.length && (
|
2018-03-29 10:18:33 +00:00
|
|
|
<p>Cochez ce que vous voulez affiner</p>
|
2018-03-01 18:07:40 +00:00
|
|
|
)
|
2018-03-01 14:32:14 +00:00
|
|
|
) : (
|
2018-03-12 09:38:29 +00:00
|
|
|
<>
|
2018-03-29 10:18:33 +00:00
|
|
|
<p>
|
2018-04-03 16:03:28 +00:00
|
|
|
<b>Estimation approximative</b> <br /> pour une situation par
|
|
|
|
défaut (CDI non cadre).
|
2018-03-29 10:18:33 +00:00
|
|
|
</p>
|
2018-03-12 15:22:16 +00:00
|
|
|
<BlueButton
|
|
|
|
onClick={() => {
|
2018-03-22 16:41:45 +00:00
|
|
|
this.props.setConversationTargets([])
|
2018-03-12 15:22:16 +00:00
|
|
|
}}
|
|
|
|
>
|
2018-03-29 10:18:33 +00:00
|
|
|
Affiner le calcul
|
2018-03-12 09:38:29 +00:00
|
|
|
</BlueButton>
|
|
|
|
</>
|
2018-03-01 14:32:14 +00:00
|
|
|
)}
|
2018-02-28 16:45:13 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2018-03-14 17:20:09 +00:00
|
|
|
</div>
|
2017-11-15 09:49:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderOutputList() {
|
2018-03-01 14:32:14 +00:00
|
|
|
let popularTargets = popularTargetNames.map(curry(findRuleByName)(flatRules)),
|
|
|
|
{
|
2018-03-12 15:22:16 +00:00
|
|
|
conversationTargetNames,
|
2018-03-01 14:32:14 +00:00
|
|
|
textColourOnWhite,
|
2018-03-12 15:22:16 +00:00
|
|
|
setConversationTargets
|
2018-03-01 14:32:14 +00:00
|
|
|
} = this.props,
|
2018-03-12 15:22:16 +00:00
|
|
|
optionIsChecked = s => (conversationTargetNames || []).includes(s.name),
|
2018-03-01 14:32:14 +00:00
|
|
|
visibleCheckbox = s =>
|
2018-03-22 16:41:45 +00:00
|
|
|
conversationTargetNames && s.dottedName !== this.state.activeInput,
|
2018-03-01 14:32:14 +00:00
|
|
|
toggleTarget = target =>
|
|
|
|
ifElse(contains(target), without(target), append(target))
|
2017-12-20 16:49:58 +00:00
|
|
|
|
2017-11-15 09:49:55 +00:00
|
|
|
return (
|
2017-11-16 17:11:42 +00:00
|
|
|
<div>
|
2018-03-22 17:54:19 +00:00
|
|
|
<ul id="targets">
|
2017-12-19 16:09:17 +00:00
|
|
|
{popularTargets.map(s => (
|
2018-03-22 17:54:19 +00:00
|
|
|
<li key={s.name}>
|
|
|
|
{visibleCheckbox(s) && (
|
|
|
|
<input
|
|
|
|
id={s.name}
|
|
|
|
type="checkbox"
|
|
|
|
checked={optionIsChecked(s)}
|
|
|
|
onChange={() =>
|
|
|
|
setConversationTargets(
|
|
|
|
toggleTarget(s.name)(
|
|
|
|
(conversationTargetNames || []).filter(
|
|
|
|
t => !this.state.activeInput.includes(t)
|
2018-03-01 14:32:14 +00:00
|
|
|
)
|
|
|
|
)
|
2018-03-22 17:54:19 +00:00
|
|
|
)
|
2018-03-07 14:14:52 +00:00
|
|
|
}
|
2018-03-21 11:02:24 +00:00
|
|
|
/>
|
2018-03-22 17:54:19 +00:00
|
|
|
)}
|
|
|
|
{conversationTargetNames && (
|
|
|
|
<label htmlFor={s.name} key={s.name}>
|
|
|
|
<i
|
|
|
|
style={{
|
|
|
|
visibility: visibleCheckbox(s) ? 'visible' : 'hidden'
|
|
|
|
}}
|
|
|
|
className={`fa fa${
|
|
|
|
optionIsChecked(s) ? '-check' : ''
|
|
|
|
}-square-o fa-2x`}
|
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
)}
|
|
|
|
<span className="texts">
|
2018-03-29 09:50:34 +00:00
|
|
|
<span className="optionTitle">
|
|
|
|
<Link to={'/règle/' + s.dottedName}>{s.title || s.name}</Link>
|
|
|
|
</span>
|
2018-03-22 17:54:19 +00:00
|
|
|
<p>{s['résumé']}</p>
|
|
|
|
</span>
|
|
|
|
<TargetInputOrValue
|
|
|
|
{...{
|
|
|
|
s,
|
|
|
|
targets: this.props.targets,
|
|
|
|
firstEstimationComplete: this.firstEstimationComplete,
|
|
|
|
activeInput: this.state.activeInput,
|
|
|
|
setActiveInput: name => this.setState({ activeInput: name }),
|
|
|
|
setFormValue: this.props.setFormValue
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</li>
|
2017-12-19 16:09:17 +00:00
|
|
|
))}
|
2018-03-22 17:54:19 +00:00
|
|
|
</ul>
|
2017-11-16 17:11:42 +00:00
|
|
|
</div>
|
2017-11-15 09:49:55 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 15:12:13 +00:00
|
|
|
let validate = buildValidationFunction(formValueTypes['euros'])
|
2018-03-21 11:02:24 +00:00
|
|
|
let InputComponent = ({ input, meta: { dirty, error } }) => (
|
|
|
|
<span>
|
2018-03-21 15:12:13 +00:00
|
|
|
{dirty && error && <span className="input-error">{error}</span>}
|
2018-03-21 15:21:18 +00:00
|
|
|
<input type="number" {...input} autoFocus />
|
2018-03-21 11:02:24 +00:00
|
|
|
</span>
|
|
|
|
)
|
2018-03-21 17:35:15 +00:00
|
|
|
let TargetInputOrValue = ({
|
2018-03-21 11:02:24 +00:00
|
|
|
s,
|
|
|
|
targets,
|
|
|
|
firstEstimationComplete,
|
|
|
|
activeInput,
|
2018-03-21 17:35:15 +00:00
|
|
|
setActiveInput,
|
|
|
|
clearFormValue
|
2018-03-21 11:02:24 +00:00
|
|
|
}) => (
|
|
|
|
<span className="targetInputOrValue">
|
|
|
|
{activeInput === s.dottedName ? (
|
2018-03-21 15:12:13 +00:00
|
|
|
<Field
|
|
|
|
name={s.dottedName}
|
|
|
|
component={InputComponent}
|
|
|
|
type="text"
|
|
|
|
validate={validate}
|
|
|
|
/>
|
2018-03-21 11:02:24 +00:00
|
|
|
) : (
|
2018-03-22 10:04:47 +00:00
|
|
|
<TargetValue {...{ targets, s, activeInput, setActiveInput }} />
|
2018-03-21 11:02:24 +00:00
|
|
|
)}
|
|
|
|
{(firstEstimationComplete || s.question) && <span className="unit">€</span>}
|
|
|
|
</span>
|
|
|
|
)
|
2018-03-22 10:04:47 +00:00
|
|
|
|
|
|
|
@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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|