2021-01-29 00:20:46 +05:30
|
|
|
import Api from '~/api';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { createAlert } from '~/alert';
|
2023-01-13 00:05:48 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-01-29 00:20:46 +05:30
|
|
|
import { visitUrl, setUrlParams } from '~/lib/utils/url_utility';
|
2023-01-13 00:05:48 +05:30
|
|
|
import { logError } from '~/lib/logger';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { __ } from '~/locale';
|
2023-06-20 00:43:36 +05:30
|
|
|
import { languageFilterData } from '~/search/sidebar/components/language_filter/data';
|
2021-12-11 22:18:48 +05:30
|
|
|
import { GROUPS_LOCAL_STORAGE_KEY, PROJECTS_LOCAL_STORAGE_KEY, SIDEBAR_PARAMS } from './constants';
|
2021-01-29 00:20:46 +05:30
|
|
|
import * as types from './mutation_types';
|
2023-04-23 21:23:45 +05:30
|
|
|
import {
|
|
|
|
loadDataFromLS,
|
|
|
|
setFrequentItemToLS,
|
|
|
|
mergeById,
|
|
|
|
isSidebarDirty,
|
|
|
|
getAggregationsUrl,
|
2023-05-27 22:25:52 +05:30
|
|
|
prepareSearchAggregations,
|
2023-04-23 21:23:45 +05:30
|
|
|
} from './utils';
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
export const fetchGroups = ({ commit }, search) => {
|
|
|
|
commit(types.REQUEST_GROUPS);
|
2021-09-30 23:02:18 +05:30
|
|
|
Api.groups(search, { order_by: 'similarity' })
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((data) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
commit(types.RECEIVE_GROUPS_SUCCESS, data);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2022-11-25 23:54:43 +05:30
|
|
|
createAlert({ message: __('There was a problem fetching groups.') });
|
2021-01-29 00:20:46 +05:30
|
|
|
commit(types.RECEIVE_GROUPS_ERROR);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
export const fetchProjects = ({ commit, state }, search) => {
|
2021-02-22 17:27:13 +05:30
|
|
|
commit(types.REQUEST_PROJECTS);
|
|
|
|
const groupId = state.query?.group_id;
|
2022-06-21 17:19:12 +05:30
|
|
|
|
|
|
|
const handleCatch = () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
createAlert({ message: __('There was an error fetching projects') });
|
2022-06-21 17:19:12 +05:30
|
|
|
commit(types.RECEIVE_PROJECTS_ERROR);
|
|
|
|
};
|
|
|
|
const handleSuccess = ({ data }) => {
|
|
|
|
commit(types.RECEIVE_PROJECTS_SUCCESS, data);
|
2021-02-22 17:27:13 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
if (groupId) {
|
2022-07-23 23:45:48 +05:30
|
|
|
Api.groupProjects(groupId, search, {
|
|
|
|
order_by: 'similarity',
|
|
|
|
with_shared: false,
|
|
|
|
include_subgroups: true,
|
|
|
|
})
|
2022-06-21 17:19:12 +05:30
|
|
|
.then(handleSuccess)
|
|
|
|
.catch(handleCatch);
|
2021-02-22 17:27:13 +05:30
|
|
|
} else {
|
|
|
|
// The .catch() is due to the API method not handling a rejection properly
|
2022-06-21 17:19:12 +05:30
|
|
|
Api.projects(search, { order_by: 'similarity' }).then(handleSuccess).catch(handleCatch);
|
2021-02-22 17:27:13 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
export const preloadStoredFrequentItems = ({ commit }) => {
|
|
|
|
const storedGroups = loadDataFromLS(GROUPS_LOCAL_STORAGE_KEY);
|
|
|
|
commit(types.LOAD_FREQUENT_ITEMS, { key: GROUPS_LOCAL_STORAGE_KEY, data: storedGroups });
|
2021-09-30 23:02:18 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
const storedProjects = loadDataFromLS(PROJECTS_LOCAL_STORAGE_KEY);
|
|
|
|
commit(types.LOAD_FREQUENT_ITEMS, { key: PROJECTS_LOCAL_STORAGE_KEY, data: storedProjects });
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loadFrequentGroups = async ({ commit, state }) => {
|
|
|
|
const storedData = state.frequentItems[GROUPS_LOCAL_STORAGE_KEY];
|
|
|
|
const promises = storedData.map((d) => Api.group(d.id));
|
2021-09-30 23:02:18 +05:30
|
|
|
try {
|
2021-10-27 15:23:28 +05:30
|
|
|
const inflatedData = mergeById(await Promise.all(promises), storedData);
|
2021-09-30 23:02:18 +05:30
|
|
|
commit(types.LOAD_FREQUENT_ITEMS, { key: GROUPS_LOCAL_STORAGE_KEY, data: inflatedData });
|
|
|
|
} catch {
|
2022-11-25 23:54:43 +05:30
|
|
|
createAlert({ message: __('There was a problem fetching recent groups.') });
|
2021-09-30 23:02:18 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
export const loadFrequentProjects = async ({ commit, state }) => {
|
|
|
|
const storedData = state.frequentItems[PROJECTS_LOCAL_STORAGE_KEY];
|
|
|
|
const promises = storedData.map((d) => Api.project(d.id).then((res) => res.data));
|
2021-09-30 23:02:18 +05:30
|
|
|
try {
|
2021-10-27 15:23:28 +05:30
|
|
|
const inflatedData = mergeById(await Promise.all(promises), storedData);
|
2021-09-30 23:02:18 +05:30
|
|
|
commit(types.LOAD_FREQUENT_ITEMS, { key: PROJECTS_LOCAL_STORAGE_KEY, data: inflatedData });
|
|
|
|
} catch {
|
2022-11-25 23:54:43 +05:30
|
|
|
createAlert({ message: __('There was a problem fetching recent projects.') });
|
2021-09-30 23:02:18 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
export const setFrequentGroup = ({ state, commit }, item) => {
|
|
|
|
const frequentItems = setFrequentItemToLS(GROUPS_LOCAL_STORAGE_KEY, state.frequentItems, item);
|
|
|
|
commit(types.LOAD_FREQUENT_ITEMS, { key: GROUPS_LOCAL_STORAGE_KEY, data: frequentItems });
|
2021-09-30 23:02:18 +05:30
|
|
|
};
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
export const setFrequentProject = ({ state, commit }, item) => {
|
|
|
|
const frequentItems = setFrequentItemToLS(PROJECTS_LOCAL_STORAGE_KEY, state.frequentItems, item);
|
|
|
|
commit(types.LOAD_FREQUENT_ITEMS, { key: PROJECTS_LOCAL_STORAGE_KEY, data: frequentItems });
|
2021-09-30 23:02:18 +05:30
|
|
|
};
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
export const setQuery = ({ state, commit }, { key, value }) => {
|
2021-01-29 00:20:46 +05:30
|
|
|
commit(types.SET_QUERY, { key, value });
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
if (SIDEBAR_PARAMS.includes(key)) {
|
|
|
|
commit(types.SET_SIDEBAR_DIRTY, isSidebarDirty(state.query, state.urlQuery));
|
|
|
|
}
|
2021-01-29 00:20:46 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export const applyQuery = ({ state }) => {
|
2023-04-23 21:23:45 +05:30
|
|
|
visitUrl(setUrlParams({ ...state.query, page: null }, window.location.href, false, true));
|
2021-01-29 00:20:46 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export const resetQuery = ({ state }) => {
|
2023-05-27 22:25:52 +05:30
|
|
|
visitUrl(
|
|
|
|
setUrlParams({ ...state.query, page: null, state: null, confidential: null }, undefined, true),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resetLanguageQueryWithRedirect = ({ state }) => {
|
|
|
|
visitUrl(setUrlParams({ ...state.query, language: null }, undefined, true));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const resetLanguageQuery = ({ commit }) => {
|
|
|
|
commit(types.SET_QUERY, { key: languageFilterData?.filterParam, value: [] });
|
2021-01-29 00:20:46 +05:30
|
|
|
};
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
export const fetchSidebarCount = ({ commit, state }) => {
|
2023-05-27 22:25:52 +05:30
|
|
|
const promises = Object.values(state.navigation).map((navItem) => {
|
2023-01-13 00:05:48 +05:30
|
|
|
// active nav item has count already so we skip it
|
2023-05-27 22:25:52 +05:30
|
|
|
if (!navItem.active) {
|
2023-01-13 00:05:48 +05:30
|
|
|
return axios
|
2023-05-27 22:25:52 +05:30
|
|
|
.get(navItem.count_link)
|
2023-01-13 00:05:48 +05:30
|
|
|
.then(({ data: { count } }) => {
|
2023-05-27 22:25:52 +05:30
|
|
|
commit(types.RECEIVE_NAVIGATION_COUNT, { key: navItem.scope, count });
|
2023-01-13 00:05:48 +05:30
|
|
|
})
|
|
|
|
.catch((e) => logError(e));
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
return Promise.all(promises);
|
|
|
|
};
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
export const fetchLanguageAggregation = ({ commit, state }) => {
|
2023-04-23 21:23:45 +05:30
|
|
|
commit(types.REQUEST_AGGREGATIONS);
|
|
|
|
return axios
|
|
|
|
.get(getAggregationsUrl())
|
2023-06-20 00:43:36 +05:30
|
|
|
.then(({ data }) => {
|
2023-05-27 22:25:52 +05:30
|
|
|
commit(types.RECEIVE_AGGREGATIONS_SUCCESS, prepareSearchAggregations(state, data));
|
2023-04-23 21:23:45 +05:30
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
logError(e);
|
|
|
|
commit(types.RECEIVE_AGGREGATIONS_ERROR);
|
|
|
|
});
|
|
|
|
};
|