Skip to content
Snippets Groups Projects
recommendations.js 1.93 KiB
Newer Older
import {
  get as apiGet,
  create,
  remove,
  update,
} from 'pubsweet-client/src/helpers/api'

const REQUEST = 'recommendations/REQUEST'
const ERROR = 'recommendations/ERROR'

const GET_RECOMMENDATIONS_SUCCESS = 'recommendations/GET_SUCCESS'
const GET_RECOMMENDATION_SUCCESS = 'recommendations/GET_ITEM_SUCCESS'
const UPDATE_RECOMMENDATION_SUCCESS = 'recommendations/UPDATE_SUCCESS'

export const recommendationsRequest = () => ({
  type: REQUEST,
})

export const recommendationsError = error => ({
  type: ERROR,
  error,
})

export const getRecommendationsSuccess = recommendations => ({
  type: GET_RECOMMENDATIONS_SUCCESS,
  payload: { recommendations },
})

export const getRecommendationSuccess = recommendation => ({
  type: GET_RECOMMENDATION_SUCCESS,
  payload: { recommendation },
})

export const updateRecommendationSuccess = recommendation => ({
  type: UPDATE_RECOMMENDATION_SUCCESS,
  payload: { recommendation },
})

// Actions

// State
const initialState = {
  fetching: false,
  error: null,
  recommendations: [],
  recommendation: {},
}

export default (state = initialState, action = {}) => {
  switch (action.type) {
    case REQUEST:
      return {
        ...state,
        fetching: true,
        recommendations: [],
        recommendation: {},
      }
    case ERROR:
      return {
        ...state,
        fetching: false,
        error: action.error,
      }
    case GET_RECOMMENDATIONS_SUCCESS:
      return {
        ...state,
        fetching: false,
        error: null,
        recommendations: action.payload.recommendations,
      }
    case GET_RECOMMENDATION_SUCCESS:
      return {
        ...state,
        fetching: false,
        error: null,
        recommendation: action.payload.recommendation,
      }
    case UPDATE_RECOMMENDATION_SUCCESS:
      return {
        ...state,
        fetching: false,
        error: null,
        recommendation: action.payload.recommendation,
      }
    default:
      return state
  }
}