Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const fetchTrafficUsage = data => dispatch =>
- dispatch(
- callAPI({
- method: 'POST',
- endpoint: '/api/v1/metrics/connections-data/',
- headers: {
- Authorization: `JWT ${sessionStorage.getItem('token')}`,
- },
- types: [
- types.FETCH_TRAFFIC_USAGE_REQUEST,
- {
- type: types.FETCH_TRAFFIC_USAGE_SUCCESS,
- payload: (action, state, res) =>
- res.json().then(json => {
- dispatch(calculateTrafficUsageData(json.connections_data));
- }),
- },
- types.FETCH_TRAFFIC_USAGE_FAILURE,
- ],
- body: JSON.stringify(data),
- })
- );
- export const callAPI = (apiOptions, options = {}) => {
- const reduxAction = Object.assign({}, config.DEFAULT_EVENT, apiOptions, {
- headers: {
- ...DEFAULT_HEADERS,
- ...apiOptions.headers,
- 'X-Fingerprint': Cookies.get('fingerprint'),
- },
- });
- let reduxErrorAction = reduxAction.types[2];
- reduxErrorAction =
- typeof reduxErrorAction === 'string'
- ? { type: reduxErrorAction }
- : reduxErrorAction;
- if (typeof reduxErrorAction.payload === 'undefined') {
- let errorInterceptor = options.errorInterceptor;
- if (typeof errorInterceptor === 'undefined') {
- errorInterceptor = config.ERROR_INTERCEPTOR;
- }
- reduxErrorAction.payload = (action, state, res) =>
- getJSON(res).then(json => {
- const error = new ApiError(res.status, res.statusText, json);
- if (errorInterceptor) errorInterceptor(error);
- return error;
- });
- reduxAction.types[2] = reduxErrorAction;
- }
- return { [RSAA]: reduxAction };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement