categoryReducer.js 1.48 KB
import { GET_CATEGORIES_REQUEST, CLEAR_ERRORS, GET_CATEGORIES_SUCCESS, GET_CATEGORIES_FAIL, GET_SUB_CATEGORIES_REQUEST, GET_SUB_CATEGORIES_FAIL, GET_SUB_CATEGORIES_SUCCESS } from "../constants/categoryConstants";

export const getAllCategoriesReducer = (state = { loading: true, success: false, categories: null }, action) => {
    switch (action.type) {
      case GET_CATEGORIES_REQUEST:
        return {
          loading: true,
        };
  
      case GET_CATEGORIES_SUCCESS:
        return {
          loading: false,
          categories: action.payload
        };
  
      case GET_CATEGORIES_FAIL:
        return {
          loading: false,
          error: action.payload.error.message
        };
  
      case CLEAR_ERRORS:
        return {
          ...state,
          error: null
        };
  
      default:
        return state;
    }
  };


export const getAllSubCategoriesReducer = (state = { loading: true, success: false, subCategories: null }, action) => {
    switch (action.type) {
      case GET_SUB_CATEGORIES_REQUEST:
        return {
          loading: true,
        };
  
      case GET_SUB_CATEGORIES_SUCCESS:
        return {
          loading: false,
          subCategories: action.payload
        };
  
      case GET_SUB_CATEGORIES_FAIL:
        return {
          loading: false,
          error: action.payload.error.message
        };
  
      case CLEAR_ERRORS:
        return {
          ...state,
          error: null
        };
  
      default:
        return state;
    }
  };