solicitudes.js 4.41 KB
import qs from 'query-string'
import { merge, reject, isNil, tap } from 'ramda'
import { SENATICS_URL } from '../../constants'
import { notify } from '../../lib/notify'

export const SOLICITUDES_REQUESTED = 'SOLICITUDES_REQUESTED'
export const solicitudesRequested = () => ({
    type: SOLICITUDES_REQUESTED
  })


export const SOLICITUDES_RECEIVED = 'SOLICITUDES_RECEIVED'
export const solicitudesReceived = (solicitudes) => ({
  type: SOLICITUDES_RECEIVED,
  solicitudes: solicitudes.results,
  pages: solicitudes.meta
})


export const SOLICITUDES_FAILED = 'SOLICITUDES_FAILED'
export const solicitudesFailed = (error) => ({
  type: SOLICITUDES_FAILED,
  error
})


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

    const params = merge({
      page: 1,
    }, reject(isNil, data))
    const url = `${SENATICS_URL}/solicitudes?${qs.stringify(params)}`
    const internal = () => fetch(url, options)
      .then(response => response.text())
      .then(tap(text => dispatch({ type: 'TEXT_RECEIVED', text })))
      .then(JSON.parse)
      .then(json => dispatch(solicitudesReceived(json)))
      .catch(error => dispatch(solicitudesFailed(error)))
    return internal()
  }


export const VER_DETALLE_SOLICITUD = 'VER_DETALLE_SOLICITUD'
export const verDetalleSolicitud = (solicitud) => ({
  type: VER_DETALLE_SOLICITUD,
  solicitud,
})


export const FLUJOS_REQUESTED = 'FLUJOS_REQUESTED'
export const flujosRequested = () => ({
  type: FLUJOS_REQUESTED,
})


export const FLUJOS_RECEIVED = 'FLUJOS_RECEIVED'
export const flujosReceived = (flujos) => ({
  type: FLUJOS_RECEIVED,
  flujos
})


export const FLUJOS_FAILED = 'FLUJOS_FAILED'
export const flujosFailed = (error) => ({
  type: FLUJOS_FAILED,
  error
})


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

    const url = `${SENATICS_URL}/flujos-solicitud/${solicitudId}`

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


export const SOLICITUD_POSTED = 'SOLICITUD_POSTED'
export const solicitudPosted = (solicitud) => ({
  type: SOLICITUD_POSTED,
  solicitud
})
export const SOLICITUD_POST_FAILED = 'SOLICITUD_POST_FAILED'
export const solicitudPostFailed = () => ({
  type: SOLICITUD_POST_FAILED
})
export const SOLICITUD_POST_SUCCESS = 'SOLICITUD_POST_SUCCESS'
export const solicitudPostSuccess = (result) => ({
  type: SOLICITUD_POST_SUCCESS,
  result
})


// esto es un thunk para mantener consistencia
// como integre redux-observables muy tarde, prefiero mantener
// convencion y tener todos los efectos secundarios en los thunks
export const notifySolicitud = ({ titulo, institucion }) => () =>
  notify({
    title: `Solicitud ${titulo} creada.`,
    body: `Tu solicitud ${titulo} a ${institucion.nombre}, fue creada exitosamente.`
  })

export const postSolicitud = (token, solicitud) => (dispatch) => {
  dispatch(solicitudPosted(solicitud))
  const options = {
    method: 'POST',
    body: JSON.stringify(solicitud),
    headers: {
      Accept: 'application/json',
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json',
    },
  }


  const url = `${SENATICS_URL}/solicitudes`

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

export const CAMBIAR_BUSQUEDA_SOLICITUD = 'CAMBIAR_BUSQUEDA_SOLICITUD'
export const cambiarBusquedaSolicitud = (busqueda) => ({
  type: CAMBIAR_BUSQUEDA_SOLICITUD,
  busqueda,
})


export const CAMBIAR_PAGINA_SOLICITUD = 'CAMBIAR_PAGINA_SOLICITUD'
export const cambiarPaginaSolicitud = (busqueda) => ({
  type: CAMBIAR_PAGINA_SOLICITUD,
  busqueda,
})