activitiesReducer.js 2.55 KB
import {
  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,
  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
      };

    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;
  }
};