All files / app/assets/javascripts/reports/accessibility_report/store actions.js

75% Statements 24/32
33.33% Branches 4/12
69.23% Functions 9/13
75.86% Lines 22/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                4x 2x     4x 2x     4x       4x                     4x 2x   2x     2x         1x 1x     2x 2x               2x                 4x 2x 1x   1x       4x 1x 1x    
import Visibility from 'visibilityjs';
import axios from '~/lib/utils/axios_utils';
import httpStatusCodes from '~/lib/utils/http_status';
import Poll from '~/lib/utils/poll';
import * as types from './mutation_types';
 
let eTagPoll;
 
export const clearEtagPoll = () => {
  eTagPoll = null;
};
 
export const stopPolling = () => {
  Eif (eTagPoll) eTagPoll.stop();
};
 
export const restartPolling = () => {
  if (eTagPoll) eTagPoll.restart();
};
 
export const setEndpoint = ({ commit }, endpoint) => commit(types.SET_ENDPOINT, endpoint);
 
/**
 * We need to poll the report endpoint while they are being parsed in the Backend.
 * This can take up to one minute.
 *
 * Poll.js will handle etag response.
 * While http status code is 204, it means it's parsing, and we'll keep polling
 * When http status code is 200, it means parsing is done, we can show the results & stop polling
 * When http status code is 500, it means parsing went wrong and we stop polling
 */
export const fetchReport = ({ state, dispatch, commit }) => {
  commit(types.REQUEST_REPORT);
 
  eTagPoll = new Poll({
    resource: {
      getReport(endpoint) {
        return axios.get(endpoint);
      },
    },
    data: state.endpoint,
    method: 'getReport',
    successCallback: ({ status, data }) => dispatch('receiveReportSuccess', { status, data }),
    errorCallback: () => dispatch('receiveReportError'),
  });
 
  Eif (!Visibility.hidden()) {
    eTagPoll.makeRequest();
  } else {
    axios
      .get(state.endpoint)
      .then(({ status, data }) => dispatch('receiveReportSuccess', { status, data }))
      .catch(() => dispatch('receiveReportError'));
  }
 
  Visibility.change(() => {
    if (!Visibility.hidden() && state.isLoading) {
      dispatch('restartPolling');
    } else {
      dispatch('stopPolling');
    }
  });
};
 
export const receiveReportSuccess = ({ commit, dispatch }, { status, data }) => {
  if (status === httpStatusCodes.OK) {
    commit(types.RECEIVE_REPORT_SUCCESS, data);
    // Stop polling since we have the information already parsed and it won't be changing
    dispatch('stopPolling');
  }
};
 
export const receiveReportError = ({ commit, dispatch }) => {
  commit(types.RECEIVE_REPORT_ERROR);
  dispatch('stopPolling');
};