summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/projects/commit/store/actions.js
blob: cfff93eac5aac2fa4a8d565f69591cf744ba34b3 (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
import { createAlert } from '~/flash';
import axios from '~/lib/utils/axios_utils';
import { PROJECT_BRANCHES_ERROR } from '../constants';
import * as types from './mutation_types';

export const clearModal = ({ commit }) => {
  commit(types.CLEAR_MODAL);
};

export const requestBranches = ({ commit }) => {
  commit(types.REQUEST_BRANCHES);
};

export const setBranchesEndpoint = ({ commit }, endpoint) => {
  commit(types.SET_BRANCHES_ENDPOINT, endpoint);
};

export const fetchBranches = ({ commit, dispatch, state }, query) => {
  dispatch('requestBranches');

  return axios
    .get(state.branchesEndpoint, {
      params: { search: query },
    })
    .then(({ data = [] }) => {
      commit(types.RECEIVE_BRANCHES_SUCCESS, data.Branches?.length ? data.Branches : data);
    })
    .catch(() => {
      createAlert({ message: PROJECT_BRANCHES_ERROR });
    });
};

export const setBranch = ({ commit, dispatch }, branch) => {
  commit(types.SET_BRANCH, branch);
  dispatch('setSelectedBranch', branch);
};

export const setSelectedBranch = ({ commit }, branch) => {
  commit(types.SET_SELECTED_BRANCH, branch);
};

export const setSelectedProject = ({ commit, dispatch, state }, id) => {
  let { branchesEndpoint } = state;

  if (state.projects?.length) {
    branchesEndpoint = state.projects.find((p) => p.id === id).refsUrl;
  }

  commit(types.SET_SELECTED_PROJECT, id);
  dispatch('setBranchesEndpoint', branchesEndpoint);
  dispatch('fetchBranches');
};