summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/issues/related_merge_requests/store/actions.js
blob: 4c81f1d9bc144a9aa681f8bd13e6d6f8bb1a9e78 (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
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { normalizeHeaders } from '~/lib/utils/common_utils';
import { __ } from '~/locale';
import * as types from './mutation_types';

const REQUEST_PAGE_COUNT = 100;

export const setInitialState = ({ commit }, props) => {
  commit(types.SET_INITIAL_STATE, props);
};

export const requestData = ({ commit }) => commit(types.REQUEST_DATA);

export const receiveDataSuccess = ({ commit }, data) => commit(types.RECEIVE_DATA_SUCCESS, data);

export const receiveDataError = ({ commit }) => commit(types.RECEIVE_DATA_ERROR);

export const fetchMergeRequests = ({ state, dispatch }) => {
  dispatch('requestData');

  return axios
    .get(`${state.apiEndpoint}?per_page=${REQUEST_PAGE_COUNT}`)
    .then((res) => {
      const { headers, data } = res;
      const total = Number(normalizeHeaders(headers)['X-TOTAL']) || 0;

      dispatch('receiveDataSuccess', { data, total });
    })
    .catch(() => {
      dispatch('receiveDataError');
      createAlert({
        message: __('Something went wrong while fetching related merge requests.'),
      });
    });
};