- Registration screens stubbing and partial buildouts
This commit is contained in:
@@ -1,38 +1,39 @@
|
||||
import { List } from 'immutable';
|
||||
|
||||
import { getEndpointUrl } from '../api/index.js';
|
||||
|
||||
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 { API_ENDPOINTS } from '../constants/constants.js';
|
||||
|
||||
import Item from '../domain/Item.js';
|
||||
|
||||
const itemsLoaded = (payload) => ({ type: ITEMS_LOADED, payload });
|
||||
|
||||
const itemsLoadSuccess = (items, dispatch) => {
|
||||
const itemsLoadError = (payload) => ({ type: ITEMS_LOAD_FAILED, payload });
|
||||
|
||||
const itemsFetchSuccess = (items) => (dispatch) => {
|
||||
const payload = List(items).map((i) => Item.fromJS(i));
|
||||
dispatch({ type: ITEMS_LOADED, payload });
|
||||
|
||||
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 state = getState();
|
||||
const activeEvent = state.get('activeEvent');
|
||||
const eventId = getActiveEventId(getState());
|
||||
const authToken = getLoginToken(getState());
|
||||
|
||||
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));
|
||||
fetchItems(activeEvent, authToken)
|
||||
.then(payload => dispatch(itemsFetchSuccess(payload)))
|
||||
.catch(err => dispatch(itemsFetchFailure(err));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user