40 lines
1.1 KiB
JavaScript
40 lines
1.1 KiB
JavaScript
import { List } from 'immutable';
|
|
|
|
import { fetchItems } from '../api/items.js';
|
|
import {
|
|
GET_ITEMS,
|
|
ITEMS_LOADED,
|
|
} from '../constants/actionTypes.js';
|
|
import { getActiveEventId } from '../selectors/activeEvent.js';
|
|
import { getLoginToken } from '../selectors/profile.js';
|
|
|
|
import { blockUI, unblockUI } from './index.js';
|
|
|
|
import Item from '../domain/Item.js';
|
|
|
|
const itemsLoaded = (payload) => ({ type: ITEMS_LOADED, payload });
|
|
|
|
const itemsLoadError = (payload) => ({ type: ITEMS_LOAD_FAILED, payload });
|
|
|
|
const itemsFetchSuccess = (items) => (dispatch) => {
|
|
const payload = List(items).map((i) => Item.fromJS(i));
|
|
|
|
dispatch(itemsLoaded(payload));
|
|
dispatch(unblockUI);
|
|
};
|
|
|
|
const itemsFetchFailure = (error) => (dispatch) => {
|
|
console.error('[actions::getItems]', error));
|
|
dispatch(itemsLoadFailure(error));
|
|
dispatch(unblockUI);
|
|
};
|
|
|
|
export const fetchItems = () => (dispatch, getState) => {
|
|
const eventId = getActiveEventId(getState());
|
|
const authToken = getLoginToken(getState());
|
|
|
|
fetchItems(activeEvent, authToken)
|
|
.then(payload => dispatch(itemsFetchSuccess(payload)))
|
|
.catch(err => dispatch(itemsFetchFailure(err));
|
|
};
|