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)); };