Files
Eventment/app/actions/events.js
2019-08-06 01:36:08 -04:00

37 lines
1.1 KiB
JavaScript

import { List } from 'immutable';
import { blockUI, unblockUI } from './index.js';
import { fetchEvents as fetchEventsApi } from '../api/events.js';
import {
EVENTS_LOAD_FAILED,
EVENTS_LOADED,
GET_EVENTS,
} from '../constants/actionTypes.js';
import Event from '../domain/Event.js';
import { getAuthToken } from '../selectors/auth.js';
const eventsLoaded = (payload) => ({ type: EVENTS_LOADED, payload });
const eventsLoadError = (payload) => ({ type: EVENTS_LOAD_FAILED, payload });
const eventsFetchSuccess = (items) => (dispatch) => {
const payload = List(items).map((i) => Event.fromJS(i));
dispatch(eventsLoaded(payload));
dispatch(unblockUI);
};
const eventsFetchFailure = (error) => (dispatch) => {
console.error('[actions::events::eventsFetchFailure]', error);
dispatch(eventsLoadError(error));
dispatch(unblockUI);
};
export const fetchEvents = () => (dispatch, getState) => {
const authToken = getAuthToken(getState());
fetchEventsApi(authToken)
.then(payload => dispatch(eventsFetchSuccess(payload)))
.catch(err => dispatch(eventsFetchFailure(err)));
};