summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/filtered_search_bar/store/modules/filters/actions.js
blob: 443cb28cf10ea7a35416df2acfdb920f02640a92 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { deprecatedCreateFlash as createFlash } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import Api from '~/api';
import * as types from './mutation_types';

export const setEndpoints = ({ commit }, params) => {
  const { milestonesEndpoint, labelsEndpoint, groupEndpoint, projectEndpoint } = params;
  commit(types.SET_MILESTONES_ENDPOINT, milestonesEndpoint);
  commit(types.SET_LABELS_ENDPOINT, labelsEndpoint);
  commit(types.SET_GROUP_ENDPOINT, groupEndpoint);
  commit(types.SET_PROJECT_ENDPOINT, projectEndpoint);
};

export function fetchBranches({ commit, state }, search = '') {
  const { projectEndpoint } = state;
  commit(types.REQUEST_BRANCHES);

  return Api.branches(projectEndpoint, search)
    .then(response => {
      commit(types.RECEIVE_BRANCHES_SUCCESS, response.data);
      return response;
    })
    .catch(({ response }) => {
      const { status } = response;
      commit(types.RECEIVE_BRANCHES_ERROR, status);
      createFlash(__('Failed to load branches. Please try again.'));
    });
}

export const fetchMilestones = ({ commit, state }, search_title = '') => {
  commit(types.REQUEST_MILESTONES);
  const { milestonesEndpoint } = state;

  return axios
    .get(milestonesEndpoint, { params: { search_title } })
    .then(response => {
      commit(types.RECEIVE_MILESTONES_SUCCESS, response.data);
      return response;
    })
    .catch(({ response }) => {
      const { status } = response;
      commit(types.RECEIVE_MILESTONES_ERROR, status);
      createFlash(__('Failed to load milestones. Please try again.'));
    });
};

export const fetchLabels = ({ commit, state }, search = '') => {
  commit(types.REQUEST_LABELS);

  return axios
    .get(state.labelsEndpoint, { params: { search } })
    .then(response => {
      commit(types.RECEIVE_LABELS_SUCCESS, response.data);
      return response;
    })
    .catch(({ response }) => {
      const { status } = response;
      commit(types.RECEIVE_LABELS_ERROR, status);
      createFlash(__('Failed to load labels. Please try again.'));
    });
};

function fetchUser(options = {}) {
  const { commit, projectEndpoint, groupEndpoint, query, action, errorMessage } = options;
  commit(`REQUEST_${action}`);

  let fetchUserPromise;
  if (projectEndpoint) {
    fetchUserPromise = Api.projectUsers(projectEndpoint, query).then(data => ({ data }));
  } else {
    fetchUserPromise = Api.groupMembers(groupEndpoint, { query });
  }

  return fetchUserPromise
    .then(response => {
      commit(`RECEIVE_${action}_SUCCESS`, response.data);
      return response;
    })
    .catch(({ response }) => {
      const { status } = response;
      commit(`RECEIVE_${action}_ERROR`, status);
      createFlash(errorMessage);
    });
}

export const fetchAuthors = ({ commit, state }, query = '') => {
  const { projectEndpoint, groupEndpoint } = state;

  return fetchUser({
    commit,
    query,
    projectEndpoint,
    groupEndpoint,
    action: 'AUTHORS',
    errorMessage: __('Failed to load authors. Please try again.'),
  });
};

export const fetchAssignees = ({ commit, state }, query = '') => {
  const { projectEndpoint, groupEndpoint } = state;

  return fetchUser({
    commit,
    query,
    projectEndpoint,
    groupEndpoint,
    action: 'ASSIGNEES',
    errorMessage: __('Failed to load assignees. Please try again.'),
  });
};

export const setFilters = ({ commit, dispatch }, filters) => {
  commit(types.SET_SELECTED_FILTERS, filters);

  return dispatch('setFilters', filters, { root: true });
};

export const initialize = ({ commit }, initialFilters) => {
  commit(types.SET_SELECTED_FILTERS, initialFilters);
};