activitiesReducer.js 3.56 KB
import {
  ACTIVITY_FILTERS_CONSTANT,
  CREATE_ACTIVITY_FAIL,
  CREATE_ACTIVITY_REQUEST,
  CREATE_ACTIVITY_SUCCESS,
  GET_ACTIVITIES_FAIL,
  GET_ACTIVITIES_REQUEST,
  GET_ACTIVITIES_SUCCESS,
  GET_ACTIVITY_BY_ID_FAIL,
  GET_ACTIVITY_BY_ID_REQUEST,
  GET_ACTIVITY_BY_ID_SUCCESS,
  GET_WISHLISTS_FAIL,
  GET_WISHLISTS_REQUEST,
  GET_WISHLISTS_SUCCESS,
  UPDATE_ACTIVITY_BY_ID_FAIL,
  UPDATE_ACTIVITY_BY_ID_REQUEST,
  UPDATE_ACTIVITY_BY_ID_SUCCESS
} from "../constants/activitiesConstants";
import { CLEAR_ERRORS } from "../constants/vendorConstants";

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

    case CREATE_ACTIVITY_SUCCESS:
      return {
        loading: false,
        activityData: action.payload
      };

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

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

    default:
      return state;
  }
};

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

    case GET_ACTIVITIES_SUCCESS:
      return {
        loading: false,
        allActivitiesData: action.payload,
        totalCount: action.payload.meta.pagination.total,
        resultsPerPage: action.payload.meta.pagination.pageSize
      };

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

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

    default:
      return state;
  }
};

export const getActivityByIdReducer = (state = {}, action) => {
  switch (action.type) {
    case GET_ACTIVITY_BY_ID_REQUEST:
      return {
        loading: true
      };
    case GET_ACTIVITY_BY_ID_SUCCESS:
      return {
        loading: false,
        activityById: action.payload
      };
    case GET_ACTIVITY_BY_ID_FAIL:
      return {
        loading: false,
        error: action.payload.error.message
      };

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

    default:
      return state;
  }
};

export const updateActivityByIdReducer = (state = {}, action) => {
  switch (action.type) {
    case UPDATE_ACTIVITY_BY_ID_REQUEST:
      return {
        loading: true
      };
    case UPDATE_ACTIVITY_BY_ID_SUCCESS:
      return {
        loading: true,
        updatedActivity: action.payload
      };
    case UPDATE_ACTIVITY_BY_ID_FAIL:
      return {
        loading: true,
        error: action.payload.error.message
      };
    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };
    default:
      return state;
  }
};

export const setActivityFilterReducer = (state = { activityFilters: {priceLowerLimit: 0, priceUpperLimit: 5000} }, action) => {
  switch (action.type) {
    case ACTIVITY_FILTERS_CONSTANT:
      return {
        activityFilters: action.payload
      };

    default:
      return state;
  }
};

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

    case GET_WISHLISTS_SUCCESS:
      return {
        loading: false,
        wishlists: action.payload
      };

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

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

    default:
      return state;
  }
};