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


export const REQUEST_FORMATOS = 'REQUEST_FORMATOS'
export const requestFormatos = () => ({
    type: REQUEST_FORMATOS
  })


export const RECEIVE_FORMATOS = 'RECEIVE_FORMATOS'
export const receiveFormatos = (formatos) => ({
  type: RECEIVE_FORMATOS,
  formatos: formatos.results,
  pages: formatos.meta
})


export const ERROR_FORMATOS = 'ERROR_FORMATOS'
export const errorFormatos = (error) => ({
  type: ERROR_FORMATOS,
  error,
})


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

    const url = `${SENATICS_URL}/formatos`

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