Files
Eventment/app/actions/items.js
2019-07-24 00:53:01 -04:00

39 lines
994 B
JavaScript

import { List } from 'immutable';
import { getEndpointUrl } from '../api/index.js';
import {
GET_ITEMS,
ITEMS_LOADED,
} from '../constants/actionTypes.js';
import { blockUI, unblockUI } from './index.js';
import { API_ENDPOINTS } from '../constants/constants.js';
import Item from '../domain/Item.js';
const itemsLoadSuccess = (items, dispatch) => {
const payload = List(items).map((i) => Item.fromJS(i));
dispatch({ type: ITEMS_LOADED, payload });
dispatch(unblockUI);
};
export const fetchItems = () => (dispatch, getState) => {
const state = getState();
const activeEvent = state.get('activeEvent');
let apiUrl = getEndpointUrl(API_ENDPOINTS.GET_ITEMS);
apiUrl = apiUrl.replace(/:event_id$/, '');
if (activeEvent) {
apiUrl = `${apiUrl}${activeEvent}`;
}
dispatch(blockUI());
fetch(apiUrl)
.then(response => response.json())
.then(payload => itemsLoadSuccess(payload, dispatch))
.catch(err => console.error('[actions::getItems]', err));
};