Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const fetchTrafficUsage = data => dispatch =>
  2.   dispatch(
  3.     callAPI({
  4.       method: 'POST',
  5.       endpoint: '/api/v1/metrics/connections-data/',
  6.       headers: {
  7.         Authorization: `JWT ${sessionStorage.getItem('token')}`,
  8.       },
  9.       types: [
  10.         types.FETCH_TRAFFIC_USAGE_REQUEST,
  11.         {
  12.           type: types.FETCH_TRAFFIC_USAGE_SUCCESS,
  13.           payload: (action, state, res) =>
  14.             res.json().then(json => {
  15.               dispatch(calculateTrafficUsageData(json.connections_data));
  16.             }),
  17.         },
  18.         types.FETCH_TRAFFIC_USAGE_FAILURE,
  19.       ],
  20.       body: JSON.stringify(data),
  21.     })
  22.   );
  23.  
  24.  
  25. export const callAPI = (apiOptions, options = {}) => {
  26.   const reduxAction = Object.assign({}, config.DEFAULT_EVENT, apiOptions, {
  27.     headers: {
  28.       ...DEFAULT_HEADERS,
  29.       ...apiOptions.headers,
  30.       'X-Fingerprint': Cookies.get('fingerprint'),
  31.     },
  32.   });
  33.   let reduxErrorAction = reduxAction.types[2];
  34.   reduxErrorAction =
  35.     typeof reduxErrorAction === 'string'
  36.       ? { type: reduxErrorAction }
  37.       : reduxErrorAction;
  38.  
  39.   if (typeof reduxErrorAction.payload === 'undefined') {
  40.     let errorInterceptor = options.errorInterceptor;
  41.     if (typeof errorInterceptor === 'undefined') {
  42.       errorInterceptor = config.ERROR_INTERCEPTOR;
  43.     }
  44.  
  45.     reduxErrorAction.payload = (action, state, res) =>
  46.       getJSON(res).then(json => {
  47.         const error = new ApiError(res.status, res.statusText, json);
  48.         if (errorInterceptor) errorInterceptor(error);
  49.         return error;
  50.       });
  51.     reduxAction.types[2] = reduxErrorAction;
  52.   }
  53.   return { [RSAA]: reduxAction };
  54. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement