soportes.js 1.08 KB
import { tap } from 'ramda'
import { SENATICS_URL } from '../../constants'


export const REQUEST_SOPORTES = 'REQUEST_SOPORTES'
export const requestSoportes = () => ({
    type: REQUEST_SOPORTES
  })


export const RECEIVE_SOPORTES = 'RECEIVE_SOPORTES'
export const receiveSoportes = (soportes) => ({
  type: RECEIVE_SOPORTES,
  soportes: soportes.results,
  pages: soportes.meta
})


export const ERROR_SOPORTES = 'ERROR_SOPORTES'
export const errorSoportes = (error) => ({
  type: ERROR_SOPORTES,
  error,
})


export const fetchSoportes = (token) => (dispatch) => {
    dispatch(requestSoportes())
    const options = {
      method: 'GET',
      headers: {
        Accept: 'application/json',
        Authorization: `Bearer ${token}`
      },
    }

    const url = `${SENATICS_URL}/soportes`

    const internal = () => fetch(url, options)
      .then(response => response.text())
      .then(tap(text => dispatch({ type: 'TEXT_RECEIVED', text })))
      .then(JSON.parse)
      .then(json => dispatch(receiveSoportes(json)))
      .catch(error => dispatch(errorSoportes(error)))
    return internal()
  }