solicitudes.js 1.13 KB
import { Observable } from 'rxjs'

import {
  SOLICITUD_POST_SUCCESS,
  CAMBIAR_BUSQUEDA_SOLICITUD,
  CAMBIAR_PAGINA_SOLICITUD,
  fetchSolicitudes,
  notifySolicitud
} from '../actions/solicitudes'

// it is kind of ugly to access the store each time
const getToken = (store) => (store.getState().autenticacion.token)
const getFilter = (store) => ({ by_usuario: store.getState().usuario.datos.mail })
const getBusqueda = (store) => store.getState().solicitudes.busqueda

export const fetchAfterPost = (action$, store) =>
  action$.ofType(SOLICITUD_POST_SUCCESS)
    .flatMap((action) =>
      Observable.from([
          fetchSolicitudes(getToken(store), getFilter(store)),
          notifySolicitud(action.result)
        ]))

export const searchDebounce = (action$, store) =>
  action$.ofType(CAMBIAR_BUSQUEDA_SOLICITUD)
    .debounceTime(300)
    .map(() => fetchSolicitudes(getToken(store), getBusqueda(store)))

export const changePage = (action$, store) =>
  action$.ofType(CAMBIAR_PAGINA_SOLICITUD)
    .map(() => fetchSolicitudes(getToken(store), getBusqueda(store)))

export const logAll = (action$) =>
  action$
  .do(console.log)
  .ignoreElements()