Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
952 B
1
Indexable
Never
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;
Leave a Comment