2018-05-24 15:36:17 +00:00
|
|
|
import { popularTargetNames } from 'Components/TargetSelection'
|
2018-04-25 14:23:15 +00:00
|
|
|
import computeThemeColours from 'Components/themeColours'
|
2018-06-06 07:07:11 +00:00
|
|
|
import { defaultTo, without } from 'ramda'
|
2018-05-24 15:36:17 +00:00
|
|
|
import reduceReducers from 'reduce-reducers'
|
|
|
|
import { combineReducers } from 'redux'
|
2018-06-06 07:07:11 +00:00
|
|
|
import { reducer as formReducer } from 'redux-form'
|
2018-05-24 15:36:17 +00:00
|
|
|
import storageReducer from '../storage/reducer'
|
2018-06-15 13:46:36 +00:00
|
|
|
import defaultLang from '../i18n'
|
2018-04-25 14:23:15 +00:00
|
|
|
|
|
|
|
function themeColours(state = computeThemeColours(), { type, colour }) {
|
|
|
|
if (type == 'CHANGE_THEME_COLOUR') return computeThemeColours(colour)
|
|
|
|
else return state
|
|
|
|
}
|
|
|
|
|
|
|
|
function explainedVariable(state = null, { type, variableName = null }) {
|
|
|
|
switch (type) {
|
|
|
|
case 'EXPLAIN_VARIABLE':
|
|
|
|
return variableName
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:26:27 +00:00
|
|
|
function currentExample(state = null, { type, situation, name, dottedName }) {
|
2018-04-25 14:23:15 +00:00
|
|
|
switch (type) {
|
|
|
|
case 'SET_EXAMPLE':
|
2018-06-13 08:26:27 +00:00
|
|
|
return name != null ? { name, situation, dottedName } : null
|
2018-04-25 14:23:15 +00:00
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function conversationStarted(state = false, { type }) {
|
|
|
|
switch (type) {
|
|
|
|
case 'START_CONVERSATION':
|
|
|
|
return true
|
2018-05-15 14:50:21 +00:00
|
|
|
case 'RESET_SIMULATION':
|
|
|
|
return false
|
2018-04-25 14:23:15 +00:00
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function activeTargetInput(state = null, { type, name }) {
|
|
|
|
switch (type) {
|
|
|
|
case 'SET_ACTIVE_TARGET_INPUT':
|
|
|
|
return name
|
2018-05-15 14:50:21 +00:00
|
|
|
case 'RESET_SIMULATION':
|
|
|
|
return null
|
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 07:07:11 +00:00
|
|
|
|
2018-06-15 13:46:36 +00:00
|
|
|
function lang(state = defaultLang, { type, lang }) {
|
2018-05-15 14:50:21 +00:00
|
|
|
switch (type) {
|
2018-06-06 07:07:11 +00:00
|
|
|
case 'SWITCH_LANG':
|
|
|
|
return lang
|
2018-04-25 14:23:15 +00:00
|
|
|
default:
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
function conversationSteps(
|
|
|
|
state = { foldedSteps: [], currentQuestion: null },
|
2018-06-11 14:55:30 +00:00
|
|
|
{ type, name, step }
|
2018-06-06 07:07:11 +00:00
|
|
|
) {
|
|
|
|
if (type === 'RESET_SIMULATION') return { foldedSteps: [], unfolded: null }
|
|
|
|
if (type !== 'STEP_ACTION') return state
|
|
|
|
|
2018-06-18 10:12:08 +00:00
|
|
|
if (name === 'fold')
|
|
|
|
return { foldedSteps: [...state.foldedSteps, step], unfoldedStep: null }
|
2018-06-13 15:01:45 +00:00
|
|
|
if (name === 'unfold') {
|
|
|
|
// if a step had already been unfolded, bring it back !
|
2018-06-06 07:07:11 +00:00
|
|
|
return {
|
2018-06-13 15:01:45 +00:00
|
|
|
foldedSteps: [
|
|
|
|
...without([step], state.foldedSteps),
|
|
|
|
...(state.unfoldedStep ? [state.unfoldedStep] : [])
|
|
|
|
],
|
2018-06-06 07:07:11 +00:00
|
|
|
unfoldedStep: step
|
|
|
|
}
|
2018-06-13 15:01:45 +00:00
|
|
|
}
|
2018-05-15 14:50:21 +00:00
|
|
|
}
|
2018-04-25 14:23:15 +00:00
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
export default reduceReducers(
|
|
|
|
storageReducer,
|
|
|
|
combineReducers({
|
|
|
|
sessionId: defaultTo(Math.floor(Math.random() * 1000000000000) + ''),
|
|
|
|
// this is handled by redux-form, pas touche !
|
|
|
|
form: formReducer,
|
2018-04-25 14:23:15 +00:00
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
conversationSteps,
|
|
|
|
lang,
|
2018-04-25 14:23:15 +00:00
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
targetNames: defaultTo(popularTargetNames),
|
2018-04-25 14:23:15 +00:00
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
iframe: defaultTo(false),
|
2018-04-25 14:23:15 +00:00
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
themeColours,
|
2018-04-25 14:23:15 +00:00
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
explainedVariable,
|
|
|
|
previousSimulation: defaultTo(null),
|
2018-04-25 14:23:15 +00:00
|
|
|
|
2018-06-06 07:07:11 +00:00
|
|
|
currentExample,
|
|
|
|
conversationStarted,
|
|
|
|
activeTargetInput
|
|
|
|
})
|
|
|
|
)
|