summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/reports/store/actions.js
blob: db8ab5ccb8026d0803bd8d3a747f73bb216acd16 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
import Visibility from 'visibilityjs';
import $ from 'jquery';
import axios from '../../lib/utils/axios_utils';
import Poll from '../../lib/utils/poll';
import * as types from './mutation_types';
import httpStatusCodes from '../../lib/utils/http_status';

export const setEndpoint = ({ commit }, endpoint) => commit(types.SET_ENDPOINT, endpoint);

export const requestReports = ({ commit }) => commit(types.REQUEST_REPORTS);

let eTagPoll;

export const clearEtagPoll = () => {
  eTagPoll = null;
};

export const stopPolling = () => {
  if (eTagPoll) eTagPoll.stop();
};

export const restartPolling = () => {
  if (eTagPoll) eTagPoll.restart();
};

/**
 * We need to poll the reports 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 fetchReports = ({ state, dispatch }) => {
  dispatch('requestReports');

  eTagPoll = new Poll({
    resource: {
      getReports(endpoint) {
        return axios.get(endpoint);
      },
    },
    data: state.endpoint,
    method: 'getReports',
    successCallback: ({ data, status }) =>
      dispatch('receiveReportsSuccess', {
        data,
        status,
      }),
    errorCallback: () => dispatch('receiveReportsError'),
  });

  if (!Visibility.hidden()) {
    eTagPoll.makeRequest();
  } else {
    axios
      .get(state.endpoint)
      .then(({ data, status }) => dispatch('receiveReportsSuccess', { data, status }))
      .catch(() => dispatch('receiveReportsError'));
  }

  Visibility.change(() => {
    if (!Visibility.hidden()) {
      dispatch('restartPolling');
    } else {
      dispatch('stopPolling');
    }
  });
};

export const receiveReportsSuccess = ({ commit }, response) => {
  // With 204 we keep polling and don't update the state
  if (response.status === httpStatusCodes.OK) {
    commit(types.RECEIVE_REPORTS_SUCCESS, response.data);
  }
};

export const receiveReportsError = ({ commit }) => commit(types.RECEIVE_REPORTS_ERROR);

export const openModal = ({ dispatch }, payload) => {
  dispatch('setModalData', payload);

  $('#modal-mrwidget-reports').modal('show');
};

export const setModalData = ({ commit }, payload) => commit(types.SET_ISSUE_MODAL_DATA, payload);

// prevent babel-plugin-rewire from generating an invalid default during karma tests
export default () => {};