debian-mirror-gitlab/app/assets/javascripts/search/store/actions.js

100 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-01-29 00:20:46 +05:30
import Api from '~/api';
import createFlash from '~/flash';
import { visitUrl, setUrlParams } from '~/lib/utils/url_utility';
2021-03-11 19:13:27 +05:30
import { __ } from '~/locale';
2021-09-30 23:02:18 +05:30
import { GROUPS_LOCAL_STORAGE_KEY, PROJECTS_LOCAL_STORAGE_KEY } from './constants';
2021-01-29 00:20:46 +05:30
import * as types from './mutation_types';
2021-09-30 23:02:18 +05:30
import { loadDataFromLS, setFrequentItemToLS, mergeById } 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(() => {
createFlash({ message: __('There was a problem fetching groups.') });
commit(types.RECEIVE_GROUPS_ERROR);
});
};
2021-02-22 17:27:13 +05:30
export const fetchProjects = ({ commit, state }, search) => {
commit(types.REQUEST_PROJECTS);
const groupId = state.query?.group_id;
2021-03-08 18:12:59 +05:30
const callback = (data) => {
2021-02-22 17:27:13 +05:30
if (data) {
commit(types.RECEIVE_PROJECTS_SUCCESS, data);
} else {
createFlash({ message: __('There was an error fetching projects') });
commit(types.RECEIVE_PROJECTS_ERROR);
}
};
if (groupId) {
2021-09-04 01:27:46 +05:30
// TODO (https://gitlab.com/gitlab-org/gitlab/-/issues/323331): For errors `createFlash` is called twice; in `callback` and in `Api.groupProjects`
2021-09-30 23:02:18 +05:30
Api.groupProjects(
groupId,
search,
{ order_by: 'similarity', with_shared: false, include_subgroups: true },
callback,
);
2021-02-22 17:27:13 +05:30
} else {
// The .catch() is due to the API method not handling a rejection properly
2021-11-11 11:23:49 +05:30
Api.projects(search, { order_by: 'similarity' }, callback).catch(() => {
2021-02-22 17:27:13 +05:30
callback();
});
}
};
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 {
createFlash({ message: __('There was a problem fetching recent groups.') });
}
};
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 {
createFlash({ message: __('There was a problem fetching recent projects.') });
}
};
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-01-29 00:20:46 +05:30
export const setQuery = ({ commit }, { key, value }) => {
commit(types.SET_QUERY, { key, value });
};
export const applyQuery = ({ state }) => {
visitUrl(setUrlParams({ ...state.query, page: null }));
};
export const resetQuery = ({ state }) => {
visitUrl(setUrlParams({ ...state.query, page: null, state: null, confidential: null }));
};