- Cleanup
This commit is contained in:
@@ -1,33 +1,36 @@
|
||||
import { List } from 'immutable';
|
||||
|
||||
import { getEndpointUrl } from '../api/index.js';
|
||||
|
||||
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 { blockUI, unblockUI } from './index.js';
|
||||
import { API_ENDPOINTS } from '../constants/constants.js';
|
||||
|
||||
import Event from '../domain/Event.js';
|
||||
import { getAuthToken } from '../selectors/auth.js';
|
||||
|
||||
const eventsLoaded = (payload) => ({ type: EVENTS_LOADED, payload });
|
||||
|
||||
const eventsLoadSuccess = (events, dispatch) => {
|
||||
const payload = List(events).map((i) => Event.fromJS(i));
|
||||
dispatch({ 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);
|
||||
};
|
||||
|
||||
export const setActiveEvent = (eventId) => ({
|
||||
type: SET_ACTIVE_EVENT,
|
||||
payload: eventId,
|
||||
});
|
||||
|
||||
export const fetchEvents = () => (dispatch) => {
|
||||
dispatch(blockUI());
|
||||
fetch(getEndpointUr(API_ENDPOINTS.GET_EVENTS))
|
||||
.then(response => response.json())
|
||||
.then(payload => eventsLoadSuccess(payload, dispatch))
|
||||
.catch(err => console.error('[actions::getEvents]', err));
|
||||
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)));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user