From ebd159081eadf066c51b1a7dd756d79c2c97276f Mon Sep 17 00:00:00 2001 From: mama Date: Tue, 6 Feb 2018 17:02:13 +0100 Subject: [PATCH] Simplification gen qestions (2) --- source/components/Simulateur.js | 14 +++--- .../components/conversation/FormDecorator.js | 6 +-- source/engine/generateQuestions.js | 45 ++++++++++--------- source/engine/mecanisms.js | 1 + source/engine/rules.js | 2 + 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/source/components/Simulateur.js b/source/components/Simulateur.js index 3b46b1117..1162b9f79 100644 --- a/source/components/Simulateur.js +++ b/source/components/Simulateur.js @@ -15,7 +15,7 @@ import { import './conversation/conversation.css' import './Simulateur.css' import Conversation from './conversation/Conversation' -import { makeQuestion } from 'Engine/generateQuestions' +import { getInputComponent } from 'Engine/generateQuestions' import ReactPiwik from './Tracker' @@ -80,10 +80,10 @@ export default class extends Component { flatRules, foldedSteps, currentQuestion, - situationGate, themeColours, inputInversions, - done + done, + parsedRules } = this.props, reinitalise = () => { ReactPiwik.push(['trackEvent', 'restart', '']) @@ -109,14 +109,14 @@ export default class extends Component { reinitalise, currentQuestion: currentQuestion && - this.buildStep({ unfolded: true, flatRules })( - situationGate, + getInputComponent({ unfolded: true })( + flatRules, this.targetNames, inputInversions )(currentQuestion), foldedSteps: map( - this.buildStep({ unfolded: false, flatRules })( - situationGate, + getInputComponent({ unfolded: false })( + flatRules, this.targetNames, inputInversions ), diff --git a/source/components/conversation/FormDecorator.js b/source/components/conversation/FormDecorator.js index 2690de9a0..9a047d1ec 100644 --- a/source/components/conversation/FormDecorator.js +++ b/source/components/conversation/FormDecorator.js @@ -46,10 +46,7 @@ export var FormDecorator = formType => RenderField => return (
-
+
{/* Une étape déjà répondue est marquée 'folded'. Dans ce dernier cas, un résumé de la réponse est affiché */} {unfolded ? this.renderUnfolded() : this.renderFolded()} @@ -68,7 +65,6 @@ export var FormDecorator = formType => RenderField => valueType, fieldName, inversion, - inverted, themeColours } = this.props, { i18n } = this.context diff --git a/source/engine/generateQuestions.js b/source/engine/generateQuestions.js index 3034bcd21..820366cf6 100644 --- a/source/engine/generateQuestions.js +++ b/source/engine/generateQuestions.js @@ -16,14 +16,18 @@ import { reject, identity } from 'ramda' - +import React from 'react' import Question from 'Components/conversation/Question' import Input from 'Components/conversation/Input' import Select from 'Components/conversation/select/Select' import SelectAtmp from 'Components/conversation/select/SelectTauxRisque' import formValueTypes from 'Components/conversation/formValueTypes' -import { findRuleByDottedName, disambiguateRuleReference } from './rules' +import { + findRuleByDottedName, + disambiguateRuleReference, + queryRule +} from './rules' /* COLLECTE DES VARIABLES MANQUANTES @@ -74,14 +78,13 @@ let buildVariantTree = (allRules, path) => { return rec(path) } -let buildPossibleInversion = (rule, flatRules, targetNames) => { - let ruleInversions = path(['formule', 'inversion', 'avec'])(rule) - if (!ruleInversions) return null - let inversionObjects = ruleInversions.map(i => - findRuleByDottedName( - flatRules, - disambiguateRuleReference(flatRules, rule, i) - ) +let buildPossibleInversion = (rule, rules, targetNames) => { + let query = queryRule(rule), + invertible = query('formule . inversion') + + if (!invertible) return null + let inversionObjects = query('inversion . avec').map(i => + findRuleByDottedName(rules, disambiguateRuleReference(rules, rule, i)) ), inversions = reject(({ name }) => targetNames.includes(name))( [rule].concat(inversionObjects) @@ -89,7 +92,7 @@ let buildPossibleInversion = (rule, flatRules, targetNames) => { return { inversions, - question: rule.formule.inversion.question + question: query('formule . inversion . question') } } @@ -100,15 +103,11 @@ export let getInputComponent = ({ unfolded }) => ( ) => dottedName => { let rule = findRuleByDottedName(rules, dottedName) - let fieldName = - (inputInversions && path(dottedName.split('.'), inputInversions)) || - dottedName, - fieldTitle = findRuleByDottedName(rules, fieldName).title - let commonProps = { unfolded, - fieldName, - title: rule.title + fieldName: dottedName, + title: rule.title, + question: rule.question } if (isVariant(rule)) @@ -156,14 +155,20 @@ export let getInputComponent = ({ unfolded }) => ( /> ) + let fieldName = + (inputInversions && path(dottedName.split('.'), inputInversions)) || + commonProps.fieldName, + inversion = buildPossibleInversion(rule, rules, targetNames) + return ( diff --git a/source/engine/mecanisms.js b/source/engine/mecanisms.js index 6b1836992..84a8fe88f 100644 --- a/source/engine/mecanisms.js +++ b/source/engine/mecanisms.js @@ -491,6 +491,7 @@ export let mecanismInversion = dottedName => (recurse, k, v) => { } return { + ...v, evaluate, jsx: nodeValue => ( /********************************* Autres */ +export let queryRule = rule => query => path(query.split(' . '))(rule) + let isVariant = path(['formule', 'une possibilité']) export let formatInputs = (flatRules, formValueSelector) => state => name => {