diff options
Diffstat (limited to 'app/assets/javascripts/reports/store/actions.js')
-rw-r--r-- | app/assets/javascripts/reports/store/actions.js | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/app/assets/javascripts/reports/store/actions.js b/app/assets/javascripts/reports/store/actions.js new file mode 100644 index 00000000000..c18885f23a4 --- /dev/null +++ b/app/assets/javascripts/reports/store/actions.js @@ -0,0 +1,92 @@ +import Visibility from 'visibilityjs'; +import $ from 'jquery'; +import axios from '../../lib/utils/axios_utils'; +import Poll from '../../lib/utils/poll'; +import flash from '../../flash'; +import { s__ } from '../../locale'; +import * as types from './mutation_types'; + +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 parsins 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 }) => dispatch('receiveReportsSuccess', data), + errorCallback: () => dispatch('receiveReportsError'), + }); + + if (!Visibility.hidden()) { + eTagPoll.makeRequest(); + } + + Visibility.change(() => { + if (!Visibility.hidden()) { + dispatch('restartPolling'); + } else { + dispatch('stopPolling'); + } + }); +}; + +export const receiveReportsSuccess = ({ commit }, response) => + commit(types.RECEIVE_REPORTS_SUCCESS, response); + +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); + +export const createNewIssue = ({ state, dispatch }) => { + dispatch('requestCreateIssue'); + return axios.post(state.modal.endpoint) + .then(() => dispatch('receiveCreateIssueSuccess')) + .catch(() => dispatch('receiveCreateIssueError')); +}; + +export const requestCreateIssue = ({ commit }) => commit(types.REQUEST_CREATE_ISSUE); +export const receiveCreateIssueSuccess = ({ commit }) => commit(types.RECEIVE_CREATE_ISSUE_SUCCESS); +export const receiveCreateIssueError = ({ commit }) => { + flash(s__('Report|There was an error creating the issue. Please try again.')); + commit(types.RECEIVE_CREATE_ISSUE_ERROR); +}; + +// prevent babel-plugin-rewire from generating an invalid default during karma tests +export default () => {}; |