diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-08 21:08:14 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-03-08 21:08:14 +0300 |
commit | 9885b7e33ece32ac3bfbf077bb2dc53c459dbc0e (patch) | |
tree | bcbabc83286e8852df225b2117b28d4dc8678a7f /app/assets/javascripts/search | |
parent | 684838d4bea13af1dac9c2f32b99985bf0f9f8e2 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/search')
-rw-r--r-- | app/assets/javascripts/search/store/actions.js | 5 | ||||
-rw-r--r-- | app/assets/javascripts/search/store/utils.js | 29 |
2 files changed, 31 insertions, 3 deletions
diff --git a/app/assets/javascripts/search/store/actions.js b/app/assets/javascripts/search/store/actions.js index 3af2b7892be..cd89234a231 100644 --- a/app/assets/javascripts/search/store/actions.js +++ b/app/assets/javascripts/search/store/actions.js @@ -13,6 +13,7 @@ import { mergeById, isSidebarDirty, getAggregationsUrl, + prepareSearchAggregations, } from './utils'; export const fetchGroups = ({ commit }, search) => { @@ -135,12 +136,12 @@ export const fetchSidebarCount = ({ commit, state }) => { return Promise.all(promises); }; -export const fetchLanguageAggregation = ({ commit }) => { +export const fetchLanguageAggregation = ({ commit, state }) => { commit(types.REQUEST_AGGREGATIONS); return axios .get(getAggregationsUrl()) .then(({ data }) => { - commit(types.RECEIVE_AGGREGATIONS_SUCCESS, data); + commit(types.RECEIVE_AGGREGATIONS_SUCCESS, prepareSearchAggregations(state, data)); }) .catch((e) => { logError(e); diff --git a/app/assets/javascripts/search/store/utils.js b/app/assets/javascripts/search/store/utils.js index 1e6619ca6d5..8e484e69646 100644 --- a/app/assets/javascripts/search/store/utils.js +++ b/app/assets/javascripts/search/store/utils.js @@ -1,7 +1,8 @@ -import { isEqual } from 'lodash'; +import { isEqual, orderBy } from 'lodash'; import AccessorUtilities from '~/lib/utils/accessor'; import { formatNumber } from '~/locale'; import { joinPaths } from '~/lib/utils/url_utility'; +import { languageFilterData } from '~/search/sidebar/constants/language_filter_data'; import { MAX_FREQUENT_ITEMS, MAX_FREQUENCY, @@ -9,6 +10,8 @@ import { NUMBER_FORMATING_OPTIONS, } from './constants'; +const LANGUAGE_AGGREGATION_NAME = languageFilterData.filterParam; + function extractKeys(object, keyList) { return Object.fromEntries(keyList.map((key) => [key, object[key]])); } @@ -117,3 +120,27 @@ export const getAggregationsUrl = () => { currentUrl.pathname = joinPaths('/search', 'aggregations'); return currentUrl.toString(); }; + +const sortLanguages = (state, entries) => { + const queriedLanguages = state.query?.[LANGUAGE_AGGREGATION_NAME] || []; + + if (!Array.isArray(queriedLanguages) || !queriedLanguages.length) { + return entries; + } + + const queriedLanguagesSet = new Set(queriedLanguages); + + return orderBy(entries, [({ key }) => queriedLanguagesSet.has(key), 'count'], ['desc', 'desc']); +}; + +export const prepareSearchAggregations = (state, aggregationData) => + aggregationData.map((item) => { + if (item?.name === LANGUAGE_AGGREGATION_NAME) { + return { + ...item, + buckets: sortLanguages(state, item.buckets), + }; + } + + return item; + }); |