Guest User

Untitled

a guest
Jan 22nd, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. import produce from 'immer';
  2. import {
  3. GET_ALL_QUESTIONS,
  4. UPDATE_ANSWERED_QUESTIONS,
  5. SET_NEXT_QUESTION,
  6. } from '../../constants/index.constants';
  7.  
  8. import {
  9. IOnboardingReducer,
  10. IOnboardingReducerActions,
  11. } from '../../interfaces/break-the-ice-controller.interface';
  12.  
  13. const initialState: IOnboardingReducer = {
  14. type: null,
  15. questions: {
  16. all: [],
  17. answered: [],
  18. lastAnswered: {},
  19. active: {
  20. index: null,
  21. question: null
  22. },
  23. },
  24. };
  25.  
  26. /**
  27. * The Authentication Reducer
  28. *
  29. * @param {any} [state=initialState]
  30. * @param {any} action
  31. * @returns
  32. */
  33. function onboarding(
  34. state: IOnboardingReducer = initialState,
  35. action: IOnboardingReducerActions,
  36. ) {
  37. switch (action.type) {
  38. case GET_ALL_QUESTIONS:
  39. //should only be executed at the beggining of onboarding quiz because
  40. // it defined the initial question
  41. let startIndex = 0;
  42. return produce(state, draft => {
  43. draft.questions = {
  44. all: actions.questions;
  45. active: {
  46. index: startIndex,
  47. question: actions.questions[startIndex],
  48. }
  49. }
  50.  
  51. });
  52. /*
  53. return {
  54. ...state,
  55. questions: Object.assign({}, initialState.questions, {
  56. all: action.questions,
  57. active: {
  58. index: startIndex,
  59. question: action.questions[startIndex],
  60. },
  61. }),
  62. };
  63. */
  64. case UPDATE_ANSWERED_QUESTIONS:
  65. const lastQuestion =
  66. action.answered &&
  67. action.answered.index === state.questions.all.length - 1
  68. ? true
  69. : false;
  70. const answered = state.questions.answered.push({
  71. question: action.answered,
  72. lastQuestion: lastQuestion,
  73. });
  74.  
  75. return {
  76. ...state,
  77. questions: Object.assign({}, state.questions, {
  78. answered: answered,
  79. lastAnswered: action.answered,
  80. }),
  81. };
  82. case SET_NEXT_QUESTION:
  83. //get next INDEX by incrementing index from last active question
  84. const nextIndex = state.questions.active.index + 1;
  85. let lastQuestionNext =
  86. state.questions.active.index === state.questions.all.length - 1;
  87. if (!lastQuestionNext) {
  88. return {
  89. ...state,
  90. questions: Object.assign({}, state.questions, {
  91. active: {
  92. index: nextIndex,
  93. question: state.questions.all[nextIndex],
  94. },
  95. }),
  96. };
  97. }
  98. //if last question don't set a next question
  99. return { ...state };
  100. default:
  101. return state;
  102. }
  103. }
  104.  
  105. export default onboarding;
Add Comment
Please, Sign In to add comment