⚙️ Remplacer unfoldedSteps par currentQuestion

pull/110/head
Laurent Bossavit 2017-11-04 15:39:40 +01:00
parent 353d9711ec
commit fabd778817
3 changed files with 21 additions and 34 deletions

View File

@ -22,8 +22,8 @@ let situationSelector = formValueSelector('conversation')
@connect(
state => ({
situation: variableName => situationSelector(state, variableName),
currentQuestion: state.currentQuestion,
foldedSteps: state.foldedSteps,
unfoldedSteps: state.unfoldedSteps,
extraSteps: state.extraSteps,
themeColours: state.themeColours,
analysedSituation: state.analysedSituation,
@ -62,7 +62,7 @@ export default class extends Component {
let
{started} = this.state,
{foldedSteps, extraSteps, unfoldedSteps, situation, situationGate, themeColours} = this.props,
{foldedSteps, extraSteps, currentQuestion, situation, situationGate, themeColours} = this.props,
sim = path =>
R.path(R.unless(R.is(Array), R.of)(path))(this.rule.simulateur || {}),
reinitalise = () => {
@ -80,8 +80,6 @@ export default class extends Component {
answer={accessor(step.name)}
/>
let currentQuestion = R.head(unfoldedSteps)
return (
<div id="sim" className={classNames({started})}>
<Helmet>

View File

@ -5,7 +5,7 @@ import reduceReducers from 'reduce-reducers'
import {reducer as formReducer, formValueSelector} from 'redux-form'
import {rules, findRuleByName } from 'Engine/rules'
import {buildNextSteps} from 'Engine/generateQuestions'
import {buildNextSteps, makeQuestion} from 'Engine/generateQuestions'
import computeThemeColours from 'Components/themeColours'
import { STEP_ACTION, START_CONVERSATION, EXPLAIN_VARIABLE, CHANGE_THEME_COLOUR} from './actions'
@ -50,21 +50,21 @@ export let reduceSteps = (tracker, flatRules, answerSource) => (state, action) =
}
if (action.type == START_CONVERSATION) {
let unfoldedSteps = buildNextSteps(situationGate, flatRules, newState.analysedSituation)
let next = buildNextSteps(situationGate, flatRules, newState.analysedSituation)
return {
...newState,
foldedSteps: [],
unfoldedSteps
currentQuestion: R.head(next)
}
}
if (action.type == STEP_ACTION && action.name == 'fold') {
tracker.push(['trackEvent', 'answer', action.step+": "+situationGate(action.step)]);
let foldedSteps = [...state.foldedSteps, R.head(state.unfoldedSteps)],
unfoldedSteps = buildNextSteps(situationGate, flatRules, newState.analysedSituation),
let foldedSteps = [...state.foldedSteps, state.currentQuestion],
next = buildNextSteps(situationGate, flatRules, newState.analysedSituation),
assumptionsMade = !R.isEmpty(softAssumptions),
done = unfoldedSteps.length == 0
done = next.length == 0
// The simulation is "over" - except we can now fill in extra questions
// where the answers were previously given default reasonable assumptions
@ -79,7 +79,7 @@ export let reduceSteps = (tracker, flatRules, answerSource) => (state, action) =
...newState,
foldedSteps,
extraSteps,
unfoldedSteps: []
currentQuestion: null
}
}
@ -90,29 +90,23 @@ export let reduceSteps = (tracker, flatRules, answerSource) => (state, action) =
return {
...newState,
foldedSteps,
unfoldedSteps
currentQuestion: R.head(next)
}
}
if (action.type == STEP_ACTION && action.name == 'unfold') {
tracker.push(['trackEvent', 'unfold', action.step]);
// The logic here is a bit hard to follow (TODO: refactor),
// it guarantees that (foldedSteps+unfoldedSteps) as a set is invariant
// foldedSteps should only have questions that have been answered
// unfoldedSteps is all the questions that must still be answered
let stepFinder = R.propEq('name', action.step),
previous = R.head(state.unfoldedSteps),
previous = state.currentQuestion,
prevFinder = R.propEq('name', previous && previous.name),
answered = previous && (answerSource(state)(previous.name) != undefined),
foldable = answered ? [previous] : [],
foldedSteps = R.reject(stepFinder)(R.concat(state.foldedSteps, foldable)),
unfolded = R.find(stepFinder)(R.concat(state.foldedSteps, state.extraSteps)),
unfoldedSteps = R.concat([unfolded], answered ? R.reject(prevFinder)(state.unfoldedSteps) : state.unfoldedSteps)
foldedSteps = R.reject(stepFinder)(R.concat(state.foldedSteps, foldable))
return {
...newState,
foldedSteps,
unfoldedSteps
currentQuestion: makeQuestion(flatRules)(action.step)
}
}
}
@ -143,7 +137,7 @@ export default reduceReducers(
false means the user is reconsidering its previous input */
foldedSteps: (steps = []) => steps,
extraSteps: (steps = []) => steps,
unfoldedSteps: (steps = []) => steps,
currentQuestion: (state = null) => state,
analysedSituation: (state = []) => state,

View File

@ -12,7 +12,7 @@ let tracker = {push: array => null}
describe('fold', function() {
it('should start conversation with only unfolded questions', function() {
it('should start conversation with the first missing variable', function() {
let rawRules = [
// TODO - this won't work without the indirection, figure out why
{nom: "startHere", formule: {somme: ["a","b"]}, espace: "top"},
@ -28,10 +28,8 @@ describe('fold', function() {
// missing = collectMissingVariables()(stateSelector({}),situation),
result = reducer({},action)
expect(result).to.have.property('unfoldedSteps')
expect(result.unfoldedSteps).to.have.lengthOf(2)
expect(result.unfoldedSteps[0]).to.have.deep.property("name","top . aa")
expect(result.unfoldedSteps[1]).to.have.deep.property("name","top . bb")
expect(result).to.have.property('currentQuestion')
expect(result.currentQuestion).to.have.deep.property("name","top . aa")
});
it('should deal with double unfold', function() {
@ -60,10 +58,8 @@ describe('fold', function() {
let result = step5
expect(result).to.have.property('unfoldedSteps')
expect(result.unfoldedSteps).to.have.lengthOf(2)
expect(result.unfoldedSteps[0]).to.have.deep.property("name","top . bb")
expect(result.unfoldedSteps[1]).to.have.deep.property("name","top . cc")
expect(result).to.have.property('currentQuestion')
expect(result.currentQuestion).to.have.deep.property("name","top . bb")
expect(result).to.have.property('foldedSteps')
expect(result.foldedSteps).to.have.lengthOf(1)
expect(result.foldedSteps[0]).to.have.deep.property("name","top . aa")
@ -96,9 +92,8 @@ describe('fold', function() {
let result = step6
expect(result).to.have.property('unfoldedSteps')
expect(result.unfoldedSteps).to.have.lengthOf(1)
expect(result.unfoldedSteps[0]).to.have.deep.property("name","top . cc")
expect(result).to.have.property('currentQuestion')
expect(result.currentQuestion).to.have.deep.property("name","top . cc")
expect(result).to.have.property('foldedSteps')
expect(result.foldedSteps).to.have.lengthOf(2)
expect(result.foldedSteps[0]).to.have.deep.property("name","top . aa")