2022-10-11 01:57:18 +05:30
|
|
|
import { omitBy, isNil } from 'lodash';
|
|
|
|
import { objectToQuery } from '~/lib/utils/url_utility';
|
2021-11-18 22:05:49 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2022-10-11 01:57:18 +05:30
|
|
|
import { FETCH_TYPES } from '../constants';
|
2021-11-11 11:23:49 +05:30
|
|
|
import * as types from './mutation_types';
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
export const autocompleteQuery = ({ state, fetchType }) => {
|
|
|
|
const query = omitBy(
|
|
|
|
{
|
|
|
|
term: state.search,
|
|
|
|
project_id: state.searchContext?.project?.id,
|
|
|
|
project_ref: state.searchContext?.ref,
|
|
|
|
filter: fetchType,
|
|
|
|
},
|
|
|
|
isNil,
|
|
|
|
);
|
|
|
|
|
|
|
|
return `${state.autocompletePath}?${objectToQuery(query)}`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const doFetch = ({ commit, state, fetchType }) => {
|
2021-11-18 22:05:49 +05:30
|
|
|
return axios
|
2022-10-11 01:57:18 +05:30
|
|
|
.get(autocompleteQuery({ state, fetchType }))
|
2022-06-21 17:19:12 +05:30
|
|
|
.then(({ data }) => {
|
|
|
|
commit(types.RECEIVE_AUTOCOMPLETE_SUCCESS, data);
|
|
|
|
})
|
2021-11-18 22:05:49 +05:30
|
|
|
.catch(() => {
|
|
|
|
commit(types.RECEIVE_AUTOCOMPLETE_ERROR);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
export const fetchAutocompleteOptions = ({ commit, state }) => {
|
|
|
|
commit(types.REQUEST_AUTOCOMPLETE);
|
|
|
|
const promises = FETCH_TYPES.map((fetchType) => doFetch({ commit, state, fetchType }));
|
|
|
|
|
|
|
|
return Promise.all(promises);
|
|
|
|
};
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
export const clearAutocomplete = ({ commit }) => {
|
|
|
|
commit(types.CLEAR_AUTOCOMPLETE);
|
|
|
|
};
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
export const setSearch = ({ commit }, value) => {
|
|
|
|
commit(types.SET_SEARCH, value);
|
|
|
|
};
|