83 lines
2.1 KiB
JavaScript
83 lines
2.1 KiB
JavaScript
import { List } from 'immutable';
|
|
|
|
import { getEndpointUrl } from '../api/index.js';
|
|
|
|
import Auction from '../domain/Auction.js';
|
|
import Event from '../domain/Event.js';
|
|
import Item from '../domain/Item.js';
|
|
|
|
import {
|
|
AUCTIONS_UPDATED,
|
|
BLOCK_UI,
|
|
EVENTS_LOADED,
|
|
GET_EVENTS,
|
|
GET_ITEMS,
|
|
ITEMS_LOADED,
|
|
UNBLOCK_UI,
|
|
} from '../constants/actionTypes.js';
|
|
|
|
import { API_ENDPOINTS } from '../constants/constants.js';
|
|
|
|
export const getEvents = () => (dispatch) => {
|
|
dispatch(blockUI());
|
|
fetch(getEndpointUr(API_ENDPOINTS.GET_EVENTS))
|
|
.then(response => response.json())
|
|
.then((payload) => {
|
|
const events = List(payload).map((i) => Event.fromJS(i));
|
|
dispatch({ type: EVENTS_LOADED, payload: events });
|
|
dispatch(unblockUI);
|
|
});
|
|
};
|
|
|
|
export const getItems = () => (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 => {
|
|
const items = List(payload).map(i => Item.fromJS(i));
|
|
dispatch({ type: ITEMS_LOADED, payload: items });
|
|
dispatch(unblockUI());
|
|
})
|
|
.catch(err => console.error('[actions::getItems]', err));
|
|
};
|
|
|
|
export const getStatus = () => (dispatch, getState) => {
|
|
const state = getState();
|
|
const activeEvent = state.get('activeEvent');
|
|
|
|
let apiUrl = getEndpointUrl(API_ENDPOINTS.GET_STATUS);
|
|
apiUrl = apiUrl.replace(/:event_id$/, '');
|
|
if (activeEvent) {
|
|
apiUrl = `${apiUrl}${activeEvent}`;
|
|
}
|
|
|
|
dispatch(blockUI());
|
|
|
|
return fetch(apiUrl)
|
|
.then(response => response.json())
|
|
.then(payload => {
|
|
const auctions = List(payload).map(i => Auction.fromJS(i));
|
|
dispatch(unblockUI());
|
|
dispatch({ type: AUCTIONS_UPDATED, payload: auctions });
|
|
})
|
|
.catch(err => console.error('[actions::getStatus]', err));
|
|
};
|
|
|
|
export const blockUI = () => ({
|
|
type: BLOCK_UI,
|
|
});
|
|
|
|
export const unblockUI = () => ({
|
|
type: UNBLOCK_UI,
|
|
});
|