Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import produce from 'immer';
- import {
- GET_ALL_QUESTIONS,
- UPDATE_ANSWERED_QUESTIONS,
- SET_NEXT_QUESTION,
- } from '../../constants/index.constants';
- import {
- IOnboardingReducer,
- IOnboardingReducerActions,
- } from '../../interfaces/break-the-ice-controller.interface';
- const initialState: IOnboardingReducer = {
- type: null,
- questions: {
- all: [],
- answered: [],
- lastAnswered: {},
- active: {
- index: null,
- question: null
- },
- },
- };
- /**
- * The Authentication Reducer
- *
- * @param {any} [state=initialState]
- * @param {any} action
- * @returns
- */
- function onboarding(
- state: IOnboardingReducer = initialState,
- action: IOnboardingReducerActions,
- ) {
- switch (action.type) {
- case GET_ALL_QUESTIONS:
- //should only be executed at the beggining of onboarding quiz because
- // it defined the initial question
- let startIndex = 0;
- return produce(state, draft => {
- draft.questions = {
- all: actions.questions;
- active: {
- index: startIndex,
- question: actions.questions[startIndex],
- }
- }
- });
- /*
- return {
- ...state,
- questions: Object.assign({}, initialState.questions, {
- all: action.questions,
- active: {
- index: startIndex,
- question: action.questions[startIndex],
- },
- }),
- };
- */
- case UPDATE_ANSWERED_QUESTIONS:
- const lastQuestion =
- action.answered &&
- action.answered.index === state.questions.all.length - 1
- ? true
- : false;
- const answered = state.questions.answered.push({
- question: action.answered,
- lastQuestion: lastQuestion,
- });
- return {
- ...state,
- questions: Object.assign({}, state.questions, {
- answered: answered,
- lastAnswered: action.answered,
- }),
- };
- case SET_NEXT_QUESTION:
- //get next INDEX by incrementing index from last active question
- const nextIndex = state.questions.active.index + 1;
- let lastQuestionNext =
- state.questions.active.index === state.questions.all.length - 1;
- if (!lastQuestionNext) {
- return {
- ...state,
- questions: Object.assign({}, state.questions, {
- active: {
- index: nextIndex,
- question: state.questions.all[nextIndex],
- },
- }),
- };
- }
- //if last question don't set a next question
- return { ...state };
- default:
- return state;
- }
- }
- export default onboarding;
Add Comment
Please, Sign In to add comment