vendorReducers.js 2.99 KB
import {
  UPDATE_VENDOR_DETAILS_FAIL,
  UPDATE_VENDOR_DETAILS_REQUEST,
  UPDATE_VENDOR_DETAILS_SUCCESS,
  CLEAR_ERRORS,
  GET_LOGGED_IN_VENDOR_REQUEST,
  GET_LOGGED_IN_VENDOR_SUCCESS,
  GET_LOGGED_IN_VENDOR_FAIL,
  GET_VENDOR_DETAILS_FAIL,
  GET_VENDOR_DETAILS_SUCCESS,
  GET_VENDOR_DETAILS_REQUEST,
  GET_ALL_VENDORS_REQUEST,
  GET_ALL_VENDORS_SUCCESS,
  GET_ALL_VENDORS_FAIL
} from "../constants/vendorConstants";

// Load user reducer
export const loggedInVendorReducer = (state = { loading: true, success: false, loggedInVendor: null }, action) => {
  switch (action.type) {
    case GET_LOGGED_IN_VENDOR_REQUEST:
      return {
        loading: true,
        isAuthenticated: false
      };

    case GET_LOGGED_IN_VENDOR_SUCCESS:
      return {
        loading: false,
        isAuthenticated: true,
        loggedInVendor: action.payload
      };

    case GET_LOGGED_IN_VENDOR_FAIL:
      return {
        loading: false,
        isAuthenticated: true,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

export const updateVendorReducer = (state = {}, action) => {
  switch (action.type) {
    case UPDATE_VENDOR_DETAILS_REQUEST:
      return { loading: true };

    case UPDATE_VENDOR_DETAILS_SUCCESS:
      return {
        loading: false,
        updatedVendorData: action.payload
      };

    case UPDATE_VENDOR_DETAILS_FAIL:
      return {
        loading: false,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

export const getVendorDetailsReducer = (state = { loading: true, success: false, vendorDetails: null }, action) => {
  switch (action.type) {
    case GET_VENDOR_DETAILS_REQUEST:
      return {
        loading: true,
        isAuthenticated: false
      };

    case GET_VENDOR_DETAILS_SUCCESS:
      return {
        loading: false,
        isAuthenticated: true,
        vendorDetails: action.payload
      };

    case GET_VENDOR_DETAILS_FAIL:
      return {
        loading: false,
        isAuthenticated: true,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

export const getAllVendorsReducer = (state = { loading: false, success: false, allVendors: null }, action) => {
  switch (action.type) {
    case GET_ALL_VENDORS_REQUEST:
      return {
        loading: true
      };

    case GET_ALL_VENDORS_SUCCESS:
      return {
        loading: false,
        allVendors: action.payload,
        totalCount: action.totalCount,
        resultsPerPage: action.resultsPerPage
      };

    case GET_ALL_VENDORS_FAIL:
      return {
        loading: false,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};