2017-07-02 17:12:02 +00:00
|
|
|
import R from 'ramda'
|
2017-04-18 17:37:38 +00:00
|
|
|
import React, {Component} from 'react'
|
2017-07-02 17:12:02 +00:00
|
|
|
import Helmet from 'react-helmet'
|
2017-09-28 12:59:10 +00:00
|
|
|
import {formValueSelector, reset} from 'redux-form'
|
2017-04-18 17:37:38 +00:00
|
|
|
import {connect} from 'react-redux'
|
2017-04-24 18:03:38 +00:00
|
|
|
import {Redirect, Link, withRouter} from 'react-router-dom'
|
2017-07-02 17:12:02 +00:00
|
|
|
import classNames from 'classnames'
|
|
|
|
|
|
|
|
import {START_CONVERSATION} from '../actions'
|
|
|
|
import {createMarkdownDiv} from 'Engine/marked'
|
|
|
|
import {rules, findRuleByName, decodeRuleName} from 'Engine/rules'
|
2017-07-01 15:49:32 +00:00
|
|
|
import './conversation/conversation.css'
|
|
|
|
import './Simulateur.css'
|
2017-05-07 17:45:44 +00:00
|
|
|
import {capitalise0} from '../utils'
|
2017-09-28 12:59:10 +00:00
|
|
|
import Conversation from './conversation/Conversation'
|
|
|
|
|
2017-10-18 09:24:50 +00:00
|
|
|
import ReactPiwik from './Tracker'
|
2017-05-07 17:45:44 +00:00
|
|
|
|
2017-04-18 17:37:38 +00:00
|
|
|
let situationSelector = formValueSelector('conversation')
|
|
|
|
|
2017-04-24 18:03:38 +00:00
|
|
|
@withRouter
|
2017-04-18 17:37:38 +00:00
|
|
|
@connect(
|
|
|
|
state => ({
|
|
|
|
situation: variableName => situationSelector(state, variableName),
|
|
|
|
foldedSteps: state.foldedSteps,
|
|
|
|
unfoldedSteps: state.unfoldedSteps,
|
2017-09-21 13:46:59 +00:00
|
|
|
extraSteps: state.extraSteps,
|
2017-04-18 17:37:38 +00:00
|
|
|
themeColours: state.themeColours,
|
|
|
|
analysedSituation: state.analysedSituation,
|
2017-09-28 12:59:10 +00:00
|
|
|
situationGate: state.situationGate,
|
2017-04-18 17:37:38 +00:00
|
|
|
}),
|
|
|
|
dispatch => ({
|
|
|
|
startConversation: rootVariable => dispatch({type: START_CONVERSATION, rootVariable}),
|
2017-05-18 14:04:23 +00:00
|
|
|
resetForm: () => dispatch(reset('conversation'))
|
2017-04-18 17:37:38 +00:00
|
|
|
})
|
|
|
|
)
|
2017-10-18 09:24:50 +00:00
|
|
|
export default class extends Component {
|
2017-04-18 17:37:38 +00:00
|
|
|
componentWillMount() {
|
|
|
|
let {
|
2017-10-18 09:24:50 +00:00
|
|
|
match: {
|
|
|
|
params: {
|
|
|
|
name: encodedName
|
|
|
|
}
|
2017-04-18 17:37:38 +00:00
|
|
|
}
|
2017-10-18 09:24:50 +00:00
|
|
|
} = this.props,
|
2017-07-31 16:16:25 +00:00
|
|
|
name = decodeRuleName(encodedName),
|
2017-08-01 14:31:50 +00:00
|
|
|
existingConversation = this.props.foldedSteps.length > 0
|
2017-04-18 17:37:38 +00:00
|
|
|
|
2017-05-09 14:33:35 +00:00
|
|
|
this.encodedName = encodedName
|
2017-05-07 17:45:44 +00:00
|
|
|
this.name = name
|
2017-06-27 17:53:37 +00:00
|
|
|
this.rule = findRuleByName(rules, name)
|
2017-04-18 17:37:38 +00:00
|
|
|
|
|
|
|
// C'est ici que la génération du formulaire, et donc la traversée des variables commence
|
2017-07-31 16:16:25 +00:00
|
|
|
if (!existingConversation)
|
2017-05-07 17:45:44 +00:00
|
|
|
this.props.startConversation(name)
|
2017-04-18 17:37:38 +00:00
|
|
|
}
|
|
|
|
render(){
|
2017-07-31 16:16:25 +00:00
|
|
|
if (!this.rule.formule) return <Redirect to={"/regle/" + this.name}/>
|
2017-04-18 17:37:38 +00:00
|
|
|
|
|
|
|
let
|
2017-04-24 18:03:38 +00:00
|
|
|
started = !this.props.match.params.intro,
|
2017-09-28 12:59:10 +00:00
|
|
|
{foldedSteps, extraSteps, unfoldedSteps, situation, situationGate} = this.props,
|
2017-04-18 17:37:38 +00:00
|
|
|
sim = path =>
|
2017-05-07 17:45:44 +00:00
|
|
|
R.path(R.unless(R.is(Array), R.of)(path))(this.rule.simulateur || {}),
|
2017-05-02 15:24:28 +00:00
|
|
|
reinitalise = () => {
|
2017-10-18 09:24:50 +00:00
|
|
|
ReactPiwik.push(['trackEvent', 'restart', ''])
|
2017-05-18 14:04:23 +00:00
|
|
|
this.props.resetForm(this.name)
|
|
|
|
this.props.startConversation(this.name)
|
2017-05-19 09:25:13 +00:00
|
|
|
},
|
|
|
|
title = sim('titre') || capitalise0(this.rule['titre'] || this.rule['nom'])
|
2017-05-02 15:24:28 +00:00
|
|
|
|
2017-04-18 17:37:38 +00:00
|
|
|
return (
|
|
|
|
<div id="sim" className={classNames({started})}>
|
2017-05-19 09:25:13 +00:00
|
|
|
<Helmet>
|
|
|
|
<title>{title}</title>
|
|
|
|
{sim('sous-titre') &&
|
|
|
|
<meta name="description" content={sim('sous-titre')} />}
|
|
|
|
</Helmet>
|
|
|
|
<h1>{title}</h1>
|
2017-10-18 09:15:22 +00:00
|
|
|
{sim('sous-titre') && started &&
|
2017-05-07 17:45:44 +00:00
|
|
|
<div id="simSubtitle">{sim('sous-titre')}</div>
|
|
|
|
}
|
|
|
|
{sim(['introduction', 'notes']) &&
|
|
|
|
<div className="intro centered">
|
|
|
|
{sim(['introduction', 'notes']).map( ({icône, texte, titre}) =>
|
|
|
|
<div key={titre}>
|
|
|
|
<i title={titre} className={"fa "+icône} aria-hidden="true"></i>
|
|
|
|
<span>
|
|
|
|
{texte}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
}
|
2017-04-18 17:37:38 +00:00
|
|
|
{
|
|
|
|
// Tant que le bouton 'C'est parti' n'est pas cliqué, on affiche l'intro
|
2017-04-24 18:03:38 +00:00
|
|
|
!started ?
|
2017-10-18 09:24:50 +00:00
|
|
|
<div>
|
|
|
|
<div className="action centered">
|
|
|
|
{createMarkdownDiv(sim(['sous-titre'])) || <p>Simulez cette règle en quelques clics</p>}
|
|
|
|
<button onClick={() => this.props.history.push(`/simu/${this.encodedName}`) }>
|
2017-05-07 17:45:44 +00:00
|
|
|
C'est parti !
|
2017-10-18 09:24:50 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
2017-04-18 17:37:38 +00:00
|
|
|
</div>
|
2017-10-18 09:24:50 +00:00
|
|
|
: <Conversation initialValues={ R.pathOr({},['simulateur','par défaut'], sim) } {...{foldedSteps, unfoldedSteps, extraSteps, reinitalise, situation, situationGate}}/>}
|
2017-05-07 17:45:44 +00:00
|
|
|
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|