mon-entreprise/source/components/TargetSelection.js

208 lines
5.0 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 { connect } from 'react-redux'
import { rules, findRuleByName } from 'Engine/rules'
import {
reject,
propEq,
curry,
pipe,
equals,
filter,
contains,
2018-03-01 14:32:14 +00:00
length,
without,
append,
ifElse
} from 'ramda'
2017-11-15 09:49:55 +00:00
import { Link } from 'react-router-dom'
import './TargetSelection.css'
import BlueButton from './BlueButton'
import { Field, reduxForm, formValueSelector } from 'redux-form'
import { connect } from 'react-redux'
import { RuleValue } from './rule/RuleValueVignette'
export let salaries = ['salaire net', 'salaire de base', 'salaire total']
let popularTargetNames = [...salaries, 'aides employeur']
@translate()
@reduxForm({
form: 'conversation'
})
@connect(
state => ({
getTargetValue: dottedName =>
formValueSelector('conversation')(state, dottedName),
targets: state.analysis ? state.analysis.targets : [],
flatRules: state.flatRules
2018-03-01 14:32:14 +00:00
targetNames: state.targetNames
}),
dispatch => ({
startConversation: (targetNames, fromScratch = false) =>
dispatch({ type: 'START_CONVERSATION', targetNames, fromScratch })
})
)
2017-11-15 09:49:55 +00:00
export default class TargetSelection extends Component {
state = {
2018-03-01 14:32:14 +00:00
activeInput: null,
affinage: false
}
componentWillMount() {
this.props.startConversation(popularTargetNames)
2017-11-15 09:49:55 +00:00
}
render() {
if (this.props.targets.length == 0) return null
2017-11-15 09:49:55 +00:00
return (
<section
id="targetSelection"
style={{
background: this.props.colours.colour,
color: this.props.colours.textColour
}}
>
2017-11-15 09:49:55 +00:00
{this.renderOutputList()}
{this.state.activeInput ? (
<div id="action">
2018-03-01 14:32:14 +00:00
{this.state.affinage ? (
!this.props.conversationVisible && (
<p style={{ color: this.props.colours.textColour }}>
Cochez un ou plusieurs objectifs
</p>
)
2018-03-01 14:32:14 +00:00
) : (
<BlueButton onClick={() => this.setState({ affinage: true })}>
2018-03-01 14:32:14 +00:00
Affiner
</BlueButton>
)}
</div>
) : (
<h1>Entrez un salaire mensuel</h1>
)}
2017-11-15 09:49:55 +00:00
</section>
)
}
renderOutputList() {
2018-03-01 14:32:14 +00:00
let popularTargets = popularTargetNames.map(curry(findRuleByName)(flatRules)),
{
targets,
targetNames,
textColourOnWhite,
startConversation
} = this.props,
optionIsChecked = s => targetNames.includes(s.name),
visibleCheckbox = s =>
this.state.affinage && s.dottedName !== this.state.activeInput,
toggleTarget = target =>
ifElse(contains(target), without(target), append(target))
2017-11-15 09:49:55 +00:00
return (
<div>
<div id="targets">
{popularTargets.map(s => (
<div key={s.name}>
<div className="main">
2018-03-01 14:32:14 +00:00
{visibleCheckbox(s) && (
<input
id={s.name}
type="checkbox"
checked={optionIsChecked(s)}
onClick={() =>
console.log('iazdo') || this.props.showConversation()
}
2018-03-01 14:32:14 +00:00
onChange={() =>
startConversation(
toggleTarget(s.name)(
targetNames.filter(
t => !this.state.activeInput.includes(t)
)
)
)
}
/>
)}
<label
htmlFor={s.name}
key={s.name}
style={
optionIsChecked(s)
? {
color: textColourOnWhite
}
: {}
2018-03-01 14:32:14 +00:00
}
>
{
<span
style={{
visibility: visibleCheckbox(s) ? 'visible' : 'hidden'
}}
>
{optionIsChecked(s) ? (
<i
className="fa fa-check-square-o fa-2x"
style={{ color: textColourOnWhite }}
/>
) : (
<i
className="fa fa-square-o fa-2x"
style={{ color: '#4b4b66' }}
/>
)}
</span>
}
<span className="optionTitle">{s.title || s.name}</span>
</label>
<span className="targetInputOrValue">
{s.name.includes('salaire') &&
this.state.activeInput === s.dottedName ? (
<Field
name={s.dottedName}
component="input"
type="text"
placeholder="mon salaire"
autoFocus
/>
) : (
<>
{this.state.activeInput && (
<i className="fa fa-calculator" aria-hidden="true" />
)}
<span
className="targetValue"
style={{ width: '6em' }}
onClick={() =>
this.setState({ activeInput: s.dottedName })
}
>
{do {
let rule = this.props.targets.find(
propEq('dottedName', s.dottedName)
),
value = rule && rule.nodeValue
;<RuleValue value={value} />
}}
</span>
</>
)}
</span>
</div>
<p
style={
optionIsChecked(s)
? { color: textColourOnWhite }
: { color: '#4b4b66' }
2018-02-08 12:45:53 +00:00
}>
{s['résumé']}
</p>
</div>
))}
</div>
</div>
2017-11-15 09:49:55 +00:00
)
}
}