All files / app/assets/javascripts/ref/stores actions.js

100% Statements 34/34
100% Branches 4/4
100% Functions 17/17
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        6x 59x   59x   6x 66x   6x 89x   89x 267x 228x     89x 89x 89x     6x 76x   76x   72x     4x     76x       6x 75x   75x   70x     5x     75x       6x   79x 23x   23x   19x     4x     23x     56x      
import Api from '~/api';
import { REF_TYPE_BRANCHES, REF_TYPE_TAGS, REF_TYPE_COMMITS } from '../constants';
import * as types from './mutation_types';
 
export const setEnabledRefTypes = ({ commit }, refTypes) =>
  commit(types.SET_ENABLED_REF_TYPES, refTypes);
 
export const setProjectId = ({ commit }, projectId) => commit(types.SET_PROJECT_ID, projectId);
 
export const setSelectedRef = ({ commit }, selectedRef) =>
  commit(types.SET_SELECTED_REF, selectedRef);
 
export const search = ({ state, dispatch, commit }, query) => {
  commit(types.SET_QUERY, query);
 
  const dispatchIfRefTypeEnabled = (refType, action) => {
    if (state.enabledRefTypes.includes(refType)) {
      dispatch(action);
    }
  };
  dispatchIfRefTypeEnabled(REF_TYPE_BRANCHES, 'searchBranches');
  dispatchIfRefTypeEnabled(REF_TYPE_TAGS, 'searchTags');
  dispatchIfRefTypeEnabled(REF_TYPE_COMMITS, 'searchCommits');
};
 
export const searchBranches = ({ commit, state }) => {
  commit(types.REQUEST_START);
 
  Api.branches(state.projectId, state.query)
    .then((response) => {
      commit(types.RECEIVE_BRANCHES_SUCCESS, response);
    })
    .catch((error) => {
      commit(types.RECEIVE_BRANCHES_ERROR, error);
    })
    .finally(() => {
      commit(types.REQUEST_FINISH);
    });
};
 
export const searchTags = ({ commit, state }) => {
  commit(types.REQUEST_START);
 
  Api.tags(state.projectId, state.query)
    .then((response) => {
      commit(types.RECEIVE_TAGS_SUCCESS, response);
    })
    .catch((error) => {
      commit(types.RECEIVE_TAGS_ERROR, error);
    })
    .finally(() => {
      commit(types.REQUEST_FINISH);
    });
};
 
export const searchCommits = ({ commit, state, getters }) => {
  // Only query the Commit API if the search query looks like a commit SHA
  if (getters.isQueryPossiblyASha) {
    commit(types.REQUEST_START);
 
    Api.commit(state.projectId, state.query)
      .then((response) => {
        commit(types.RECEIVE_COMMITS_SUCCESS, response);
      })
      .catch((error) => {
        commit(types.RECEIVE_COMMITS_ERROR, error);
      })
      .finally(() => {
        commit(types.REQUEST_FINISH);
      });
  } else {
    commit(types.RESET_COMMIT_MATCHES);
  }
};