UNPKG

@coorpacademy/progression-engine

Version:

174 lines 4.37 kB
import _isEmpty from "lodash/fp/isEmpty"; import checkAnswer from '../check-answer'; import { computeNextStep, computeNextStepForReview } from './compute-next-step'; export const computeInitialStep = (config, availableContent = []) => { const defaultSuccess = { type: 'move', payload: { nextContent: { type: 'success', ref: 'successExitNode' }, instructions: null } }; if (_isEmpty(availableContent)) return defaultSuccess; const initialStep = computeNextStep(config, null, availableContent, null); if (!initialStep) { return defaultSuccess; } const { nextContent, instructions } = initialStep; return { type: 'move', payload: { instructions, nextContent } }; }; export const computeInitialStepForReview = (config, availableContent = []) => { const defaultSuccess = { type: 'move', payload: { nextContent: { type: 'success', ref: 'successExitNode' }, instructions: null } }; if (_isEmpty(availableContent)) return defaultSuccess; const initialStep = computeNextStepForReview(config, null, availableContent, null); if (!initialStep) throw new Error('no slide in availableContent'); const { instructions, nextContent } = initialStep; return { type: 'move', payload: { instructions, nextContent } }; }; export const computeNextStepAfterAnswer = (config, state, availableContent, currentSlide, action) => { const answerIsCorrect = action.payload.godMode || checkAnswer(config, currentSlide.question, action.payload.answer); const actionWithIsCorrect = { type: 'answer', payload: { answer: action.payload.answer, content: action.payload.content, godMode: action.payload.godMode, isCorrect: answerIsCorrect } }; const stepResult = computeNextStep(config, state, availableContent, actionWithIsCorrect); if (!stepResult) { return null; } const { nextContent, instructions, isCorrect } = stepResult; return { type: 'answer', payload: { answer: action.payload.answer, content: action.payload.content, godMode: action.payload.godMode, nextContent, isCorrect, instructions } }; }; export const computeNextStepAfterAnswerForReview = (config, state, availableContent, currentSlide, action) => { const answerIsCorrect = action.payload.godMode || checkAnswer(config, currentSlide.question, action.payload.answer); const actionWithIsCorrect = { type: 'answer', payload: { answer: action.payload.answer, content: action.payload.content, godMode: action.payload.godMode, isCorrect: answerIsCorrect } }; const stepResult = computeNextStepForReview(config, state, availableContent, actionWithIsCorrect); if (!stepResult) { return { type: 'answer', payload: { answer: action.payload.answer, content: action.payload.content, godMode: action.payload.godMode, nextContent: { type: 'success', ref: 'successExitNode' }, isCorrect: answerIsCorrect, instructions: null } }; } const { nextContent, instructions, isCorrect } = stepResult; return { type: 'answer', payload: { answer: action.payload.answer, content: action.payload.content, godMode: action.payload.godMode, nextContent, isCorrect, instructions } }; }; export const computeNextStepOnAcceptExtraLife = (config, state, availableContent) => { const partialAction = { type: 'extraLifeAccepted' }; const stepResult = computeNextStep(config, state, availableContent, partialAction); if (!stepResult) { return null; } const { nextContent, instructions } = stepResult; return { type: 'extraLifeAccepted', payload: { content: { ref: 'extraLife', type: 'node' }, nextContent, instructions } }; }; export const computeNextStepOnRefuseExtraLife = (config, state) => { return { type: 'extraLifeRefused', payload: { content: { ref: 'extraLife', type: 'node' }, nextContent: { ref: 'failExitNode', type: 'failure' } } }; }; //# sourceMappingURL=index.js.map