2017-01-10 18:22:44 +00:00
|
|
|
import React from 'react'
|
2016-11-15 18:46:17 +00:00
|
|
|
import { combineReducers } from 'redux'
|
2017-01-10 18:22:44 +00:00
|
|
|
import reduceReducers from 'reduce-reducers'
|
|
|
|
import {reducer as formReducer, formValueSelector} from 'redux-form'
|
2017-01-20 10:52:39 +00:00
|
|
|
import {analyseSituation, variableType} from './engine/traverse'
|
2017-01-10 18:22:44 +00:00
|
|
|
import { euro } from './components/conversation/formValueTypes.js'
|
|
|
|
|
|
|
|
import Question from './components/conversation/Question'
|
|
|
|
import Input from './components/conversation/Input'
|
|
|
|
import RhetoricalQuestion from './components/conversation/RhetoricalQuestion'
|
|
|
|
|
2017-02-09 17:15:25 +00:00
|
|
|
import { STEP_ACTION, UNSUBMIT_ALL, START_CONVERSATION, EXPLAIN_VARIABLE} from './actions'
|
2017-01-10 18:22:44 +00:00
|
|
|
import R from 'ramda'
|
2017-01-23 18:06:46 +00:00
|
|
|
|
2017-01-26 12:19:04 +00:00
|
|
|
import {findGroup, findRuleByDottedName, dottedName, parentName} from './engine/rules'
|
2017-01-24 15:22:40 +00:00
|
|
|
import {constructStepMeta} from './engine/conversation'
|
2017-01-10 18:22:44 +00:00
|
|
|
|
|
|
|
import computeThemeColours from './components/themeColours'
|
|
|
|
|
|
|
|
function steps(steps = [], {type}) {
|
|
|
|
switch (type) {
|
|
|
|
case UNSUBMIT_ALL:
|
|
|
|
return []
|
|
|
|
default:
|
|
|
|
return steps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function themeColours(state = computeThemeColours(), {type, colour}) {
|
|
|
|
if (type == 'CHANGE_THEME_COLOUR')
|
|
|
|
return computeThemeColours(colour)
|
|
|
|
else return state
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:19:04 +00:00
|
|
|
let situationGate = state =>
|
|
|
|
name => formValueSelector('conversation')(state, name)
|
|
|
|
|
2017-02-09 17:38:51 +00:00
|
|
|
function explainedVariable(state = null, {type, variableName=null}) {
|
|
|
|
switch (type) {
|
|
|
|
case EXPLAIN_VARIABLE:
|
2017-02-09 17:15:25 +00:00
|
|
|
return variableName
|
2017-02-09 17:38:51 +00:00
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
2017-02-08 16:50:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 18:22:44 +00:00
|
|
|
export default reduceReducers(
|
|
|
|
combineReducers({
|
|
|
|
// this is handled by redux-form, pas touche !
|
|
|
|
form: formReducer,
|
|
|
|
|
|
|
|
/* Have forms been filled or ignored ?
|
|
|
|
false means the user is reconsidering its previous input */
|
|
|
|
steps,
|
|
|
|
|
|
|
|
analysedSituation: (state = []) => state,
|
|
|
|
|
2017-02-08 16:50:22 +00:00
|
|
|
themeColours,
|
|
|
|
|
2017-02-09 17:15:25 +00:00
|
|
|
explainedVariable
|
2017-01-10 18:22:44 +00:00
|
|
|
}),
|
|
|
|
// cross-cutting concerns because here `state` is the whole state tree
|
|
|
|
(state, action) => {
|
|
|
|
if (action.type == STEP_ACTION || action.type == START_CONVERSATION) {
|
|
|
|
let {newState, name} = action
|
|
|
|
// une étape vient d'être validée : on va changer son état
|
|
|
|
let newSteps = R.pipe(
|
|
|
|
R.map(step => step.name == name ? {...step, state: newState} : step),
|
|
|
|
R.reject(R.whereEq({theEnd: true}))
|
|
|
|
)(state.steps)
|
|
|
|
|
2017-01-26 12:19:04 +00:00
|
|
|
window.situationGate = situationGate(state)
|
|
|
|
|
2017-01-10 18:22:44 +00:00
|
|
|
// on calcule la prochaine étape, à ajouter sur la pile
|
2017-01-26 12:19:04 +00:00
|
|
|
let
|
|
|
|
analysedSituation = analyseSituation(
|
|
|
|
situationGate(state)
|
|
|
|
),
|
|
|
|
|
2017-02-10 14:12:00 +00:00
|
|
|
y = console.log('analysedSituation', analysedSituation),
|
|
|
|
|
2017-01-17 09:04:34 +00:00
|
|
|
missingVariables = R.pipe(
|
2017-02-07 19:10:04 +00:00
|
|
|
R.map( ({name, derived: {missingVariables}}) =>
|
2017-01-18 08:48:46 +00:00
|
|
|
(missingVariables || []).map(mv => [mv, name])
|
|
|
|
),
|
|
|
|
R.unnest,
|
|
|
|
//groupBy but remove mv from value, it's now in the key
|
|
|
|
R.reduce( (memo, [mv, dependencyOf]) => ({...memo, [mv]: [...(memo[mv] || []), dependencyOf] }), {})
|
2017-01-23 18:06:46 +00:00
|
|
|
)(analysedSituation),
|
|
|
|
missingVariablesList = R.keys(missingVariables),
|
|
|
|
|
2017-01-24 15:22:40 +00:00
|
|
|
groups = R.groupBy(
|
2017-01-26 12:19:04 +00:00
|
|
|
parentName
|
2017-01-24 15:22:40 +00:00
|
|
|
)(missingVariablesList),
|
|
|
|
|
|
|
|
// on va maintenant construire la liste des composants React correspondant aux questions pour obtenir les variables manquantes
|
|
|
|
yyoo = R.pipe(
|
|
|
|
R.mapObjIndexed((variables, group) =>
|
|
|
|
R.pipe(
|
|
|
|
findGroup,
|
|
|
|
R.cond([
|
|
|
|
// Pas de groupe trouvé : ce sont des variables individuelles
|
2017-01-26 10:27:24 +00:00
|
|
|
[R.isNil, () => variables.map(dottedName => {
|
|
|
|
let rule = findRuleByDottedName(dottedName)
|
2017-01-24 15:22:40 +00:00
|
|
|
return Object.assign(constructStepMeta(rule),
|
2017-02-13 14:50:51 +00:00
|
|
|
rule.format == 'nombre positif' ||
|
|
|
|
rule.format == 'période' ?
|
2017-01-24 15:22:40 +00:00
|
|
|
{
|
|
|
|
component: Input,
|
2017-02-07 19:10:04 +00:00
|
|
|
defaultValue: 1,
|
2017-01-24 15:22:40 +00:00
|
|
|
valueType: euro,
|
|
|
|
attributes: {
|
|
|
|
inputMode: 'numeric',
|
|
|
|
placeholder: 'votre réponse'
|
|
|
|
}
|
|
|
|
} : {
|
|
|
|
component: Question,
|
2017-01-26 12:19:04 +00:00
|
|
|
choices: [
|
|
|
|
{value: 'non', label: 'Non'},
|
|
|
|
{value: 'oui', label: 'Oui'}
|
|
|
|
],
|
2017-01-24 15:22:40 +00:00
|
|
|
defaultValue: 'Non',
|
|
|
|
}
|
|
|
|
)})],
|
|
|
|
[R.T, group =>
|
|
|
|
Object.assign(
|
|
|
|
constructStepMeta(group),
|
|
|
|
{
|
|
|
|
component: Question,
|
2017-02-09 17:15:25 +00:00
|
|
|
choices:
|
2017-02-13 14:50:51 +00:00
|
|
|
group['une possibilité'].concat(
|
2017-02-09 17:15:25 +00:00
|
|
|
[{value: 'aucun', label: 'Aucun'}]
|
|
|
|
),
|
2017-02-09 12:58:12 +00:00
|
|
|
// defaultValue: 'Non',
|
2017-01-24 15:22:40 +00:00
|
|
|
}
|
|
|
|
)]
|
|
|
|
])
|
|
|
|
)(group)
|
|
|
|
),
|
|
|
|
R.values,
|
|
|
|
R.unnest
|
2017-01-26 12:19:04 +00:00
|
|
|
)(groups)
|
2017-01-23 18:06:46 +00:00
|
|
|
|
|
|
|
// la question doit pouvoir stocker tout ça dans la situation (redux-form) correctement
|
|
|
|
|
|
|
|
|
2017-01-24 15:22:40 +00:00
|
|
|
return {...state, steps: yyoo, analysedSituation}
|
2017-01-23 18:06:46 +00:00
|
|
|
|
|
|
|
|
2017-01-17 09:04:34 +00:00
|
|
|
|
2017-01-26 12:19:04 +00:00
|
|
|
|
|
|
|
// return {...state, steps: [...newSteps, stepData], analysedSituation}
|
2017-01-10 18:22:44 +00:00
|
|
|
// ... do stuff
|
|
|
|
} else {
|
|
|
|
return state
|
|
|
|
}
|
2016-11-15 18:46:17 +00:00
|
|
|
|
2017-01-10 18:22:44 +00:00
|
|
|
}
|
|
|
|
)
|