Files
Eventment/app/actions/events.js

32 lines
1.1 KiB
JavaScript

import { List } from 'immutable';
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 beginFetchEvents = () => ({ type: GET_EVENTS });
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));
};
const eventsFetchFailure = (error) => (dispatch) => {
console.error('[actions::events::eventsFetchFailure]', error);
dispatch(eventsLoadError(error));
};
export const fetchEvents = () => (dispatch, getState) => {
const authToken = getAuthToken(getState());
dispatch(beginFetchEvents());
fetchEventsApi(authToken)
.then((payload) => dispatch(eventsFetchSuccess(payload)))
.catch((err) => dispatch(eventsFetchFailure(err)));
};