UNPKG

@coorpacademy/progression-engine

Version:

51 lines (45 loc) 2.38 kB
import update from 'lodash/fp/update'; import map from 'lodash/fp/map'; import pipe from 'lodash/fp/pipe'; import reduce from 'lodash/fp/reduce'; import isEmpty from 'lodash/fp/isEmpty'; import allAnswers from './reducers/all-answers'; import content from './reducers/content'; import hasViewedAResourceAtThisStep from './reducers/has-viewed-a-resource-at-this-step'; import isCorrect from './reducers/is-correct'; import lives from './reducers/lives'; import livesDisabled from './reducers/lives-disabled'; import nextContent from './reducers/next-content'; import remainingLifeRequests from './reducers/remaining-life-requests'; import requestedClues from './reducers/requested-clues'; import slides from './reducers/slides'; import stars from './reducers/stars'; import step from './reducers/step'; import validate from './reducers/validate'; import viewedResources from './reducers/viewed-resources'; import variables from './reducers/variables'; function combineReducers(fnMap // eslint-disable-line flowtype/no-weak-types ) { // eslint-disable-next-line flowtype/require-return-type const fns = map(({ fn, key }) => { return (config, action) => state => { if (!key) return fn(config)(state, action); const newState = update(key, value => fn(config)(value, action, state), state); return newState; }; }, fnMap); return config => { return (state, action) => { validate(config)(state, action); return pipe(...map(fn => fn(config, action), fns))(state); }; }; } const reduceAction = combineReducers([{ key: 'livesDisabled', fn: livesDisabled }, { key: 'isCorrect', fn: isCorrect }, { key: 'slides', fn: slides }, { key: 'lives', fn: lives }, { key: 'step', fn: step }, { key: 'stars', fn: stars }, { key: 'requestedClues', fn: requestedClues }, { key: 'viewedResources', fn: viewedResources }, { key: 'remainingLifeRequests', fn: remainingLifeRequests }, { key: 'hasViewedAResourceAtThisStep', fn: hasViewedAResourceAtThisStep }, { key: 'content', fn: content }, { key: 'nextContent', fn: nextContent }, { key: 'allAnswers', fn: allAnswers }, { fn: variables }]); export default function updateState(config, state, actions) { if (isEmpty(actions)) { return reduce(reduceAction(config), state, [{ type: 'init' }]); } return reduce(reduceAction(config), state, actions); } //# sourceMappingURL=update-state.js.map