Skip to content
Snippets Groups Projects
technicalCheck.js 1.43 KiB
Newer Older
import { get } from 'lodash'
import { update } from 'pubsweet-client/src/helpers/api'

const DECISION_REQUEST = 'tc/DECISION_REQUEST'
const DECISION_SUCCESS = 'tc/DECISION_SUCCESS'
const DECISION_ERROR = 'tc/DECISION_ERROR'

const decisionRequest = () => ({
  type: DECISION_REQUEST,
})

const decisionSuccess = () => ({
  type: DECISION_SUCCESS,
})

const decisionError = error => ({
  type: DECISION_ERROR,
  error,
})

export const technicalDecision = ({
  step,
  agree,
  token,
  collectionId,
}) => dispatch => {
  dispatch(decisionRequest())
  return update(`/collections/${collectionId}/status`, {
    step,
    token,
    agree,
  }).then(
    r => {
      dispatch(decisionSuccess())
      return r
    },
    err => {
      const errorMessage = get(
        JSON.parse(err.response),
        'error',
        'Oops! Something went wrong!',
      )
      dispatch(decisionError(errorMessage))
      throw errorMessage
export const technicalCheckFetching = state =>
  get(state, 'technicalCheck.fetching', false)

export default (state = {}, action = {}) => {
  switch (action.type) {
    case DECISION_REQUEST:
      return {
        ...state,
        fetching: true,
      }
    case DECISION_SUCCESS:
      return {
        error: null,
        fetching: false,
      }
    case DECISION_ERROR:
      return {
        error: action.error,
        fetching: false,
      }
    default:
      return state
  }
}