Untitled

 avatar
unknown
plain_text
a year ago
952 B
2
Indexable
import { SET_CATEGORIES, SET_QUESTIONS, SETUP_QUIZ, ANSWER_QUESTION, RESET_QUIZ } from '../actionTypes';

const initialState = {
  categories: [],
  questions: [],
  currentQuestionIndex: 0,
  score: 0,
  quizSetup: null,
};

const quizReducer = (state = initialState, action) => {
  switch (action.type) {
    case SET_CATEGORIES:
      return { ...state, categories: action.payload };
    case SET_QUESTIONS:
      return { ...state, questions: action.payload };
    case SETUP_QUIZ:
      return { ...state, quizSetup: action.payload, currentQuestionIndex: 0, score: 0 };
    case ANSWER_QUESTION:
      const isCorrect = action.payload;
      return {
        ...state,
        score: isCorrect ? state.score + 1 : state.score,
        currentQuestionIndex: state.currentQuestionIndex + 1
      };
    case RESET_QUIZ:
      return initialState;
    default:
      return state;
  }
};

export default quizReducer;
Editor is loading...
Leave a Comment