projectsReducer.js 1.89 KB
import {
  FETCH_PROJECTS_REQUEST,
  FETCH_PROJECTS_SUCCESS,
  FETCH_PROJECTS_FAIL,
  FETCH_PROJECT_REQUEST,
  FETCH_PROJECT_SUCCESS,
  FETCH_PROJECT_FAIL,
  CLEAR_ERRORS,
  FETCH_SIMILAR_PROJECTS_REQUEST,
  FETCH_SIMILAR_PROJECTS_SUCCESS,
  FETCH_SIMILAR_PROJECTS_FAIL
} from "../constants/projectsConstants";

// Room details reducer.
export const projectsReducer = (state = { projects: [] }, action) => {
  switch (action.type) {
    case FETCH_PROJECTS_REQUEST:
      return {
        loading: true
      };

    case FETCH_PROJECTS_SUCCESS:
      return {
        loading: false,
        totalCount: action.payload.meta.pagination.total,
        resultsPerPage: action.payload.meta.pagination.pageSize,
        projects: action.payload.data
      };

    case FETCH_PROJECTS_FAIL:
      return {
        error: action.payload.error.message
      };

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

    default:
      return state;
  }
};

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

    case FETCH_PROJECT_SUCCESS:
      return {
        project: action.payload.data
      };

    case FETCH_PROJECT_FAIL:
      return {
        error: action.payload.error.message
      };

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

    default:
      return state;
  }
};

export const similarProjectsReducer = (state = { similarProjects: [] }, action) => {
  switch (action.type) {
    case FETCH_SIMILAR_PROJECTS_REQUEST:
      return {
        loading: true
      };

    case FETCH_SIMILAR_PROJECTS_SUCCESS:
      return {
        similarProjects: action.payload.data
      };

    case FETCH_SIMILAR_PROJECTS_FAIL:
      return {
        error: action.payload.error.message
      };

      default:
        return state;
  }
};