valoraciones.js 935 Bytes
import { merge } from 'ramda'

import {
  VALORACION_REQUESTED,
  VALORACION_RECEIVED,
  VALORACION_FAILED,
  VALORACION_POSTED,
  VALORACION_POST_SUCCESS
} from '../actions/valoraciones'


const valoracion = {
  data: null,
  loading: true,
  error: null
}

const toggle = (state, val) => {
  if (state.valoracion === val) {
    return null
  }
  return val
}

export default (state, action) => {
  switch (action.type) {
  case VALORACION_REQUESTED:
    return merge(state, valoracion)
  case VALORACION_POSTED:
    return merge(state, { data: toggle(state, action.valoracion) })
  case VALORACION_POST_SUCCESS:
    return merge(state, { data: action.result.valoracion })
  case VALORACION_RECEIVED:
    return merge(state, { loading: false, error: null, data: action.valoracion })
  case VALORACION_FAILED:
    return merge(state, { loading: false, data: null, error: action.error })
  default:
    return state || valoracion
  }
}