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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-11-08 19:23:39 +05:30
import AccessorUtilities from '~/lib/utils/accessor';
2021-09-30 23:02:18 +05:30
import { isLoggedIn } from '~/lib/utils/common_utils';
2021-03-08 18:12:59 +05:30
import { getGroups, getProjects } from '~/rest_api';
2021-03-11 19:13:27 +05:30
import { getTopFrequentItems } from '../utils';
import * as types from './mutation_types';
2018-11-08 19:23:39 +05:30
export const setNamespace = ({ commit }, namespace) => {
commit(types.SET_NAMESPACE, namespace);
};
export const setStorageKey = ({ commit }, key) => {
commit(types.SET_STORAGE_KEY, key);
};
2023-03-17 16:20:25 +05:30
export const toggleItemsListEditablity = ({ commit }) => {
commit(types.TOGGLE_ITEMS_LIST_EDITABILITY);
};
2018-11-08 19:23:39 +05:30
export const requestFrequentItems = ({ commit }) => {
commit(types.REQUEST_FREQUENT_ITEMS);
};
export const receiveFrequentItemsSuccess = ({ commit }, data) => {
commit(types.RECEIVE_FREQUENT_ITEMS_SUCCESS, data);
};
export const receiveFrequentItemsError = ({ commit }) => {
commit(types.RECEIVE_FREQUENT_ITEMS_ERROR);
};
export const fetchFrequentItems = ({ state, dispatch }) => {
dispatch('requestFrequentItems');
2021-11-11 11:23:49 +05:30
if (AccessorUtilities.canUseLocalStorage()) {
2018-11-08 19:23:39 +05:30
const storedFrequentItems = JSON.parse(localStorage.getItem(state.storageKey));
dispatch(
'receiveFrequentItemsSuccess',
!storedFrequentItems ? [] : getTopFrequentItems(storedFrequentItems),
);
} else {
dispatch('receiveFrequentItemsError');
}
};
export const requestSearchedItems = ({ commit }) => {
commit(types.REQUEST_SEARCHED_ITEMS);
};
export const receiveSearchedItemsSuccess = ({ commit }, data) => {
commit(types.RECEIVE_SEARCHED_ITEMS_SUCCESS, data);
};
export const receiveSearchedItemsError = ({ commit }) => {
commit(types.RECEIVE_SEARCHED_ITEMS_ERROR);
};
export const fetchSearchedItems = ({ state, dispatch }, searchQuery) => {
dispatch('requestSearchedItems');
const params = {
simple: true,
per_page: 20,
2021-09-30 23:02:18 +05:30
membership: isLoggedIn(),
2018-11-08 19:23:39 +05:30
};
2021-03-08 18:12:59 +05:30
let searchFunction;
2018-11-08 19:23:39 +05:30
if (state.namespace === 'projects') {
2021-03-08 18:12:59 +05:30
searchFunction = getProjects;
2018-11-08 19:23:39 +05:30
params.order_by = 'last_activity_at';
2021-03-08 18:12:59 +05:30
} else {
searchFunction = getGroups;
2018-11-08 19:23:39 +05:30
}
2021-03-08 18:12:59 +05:30
return searchFunction(searchQuery, params)
.then((results) => {
2018-11-08 19:23:39 +05:30
dispatch('receiveSearchedItemsSuccess', results);
})
.catch(() => {
dispatch('receiveSearchedItemsError');
});
};
export const setSearchQuery = ({ commit, dispatch }, query) => {
commit(types.SET_SEARCH_QUERY, query);
if (query) {
dispatch('fetchSearchedItems', query);
} else {
dispatch('fetchFrequentItems');
}
};
2023-03-17 16:20:25 +05:30
export const removeFrequentItemSuccess = ({ commit }, itemId) => {
commit(types.RECEIVE_REMOVE_FREQUENT_ITEM_SUCCESS, itemId);
};
export const removeFrequentItemError = ({ commit }) => {
commit(types.RECEIVE_REMOVE_FREQUENT_ITEM_ERROR);
};
export const removeFrequentItem = ({ state, dispatch }, itemId) => {
if (AccessorUtilities.canUseLocalStorage()) {
try {
const storedRawItems = JSON.parse(localStorage.getItem(state.storageKey));
localStorage.setItem(
state.storageKey,
JSON.stringify(storedRawItems.filter((item) => item.id !== itemId)),
);
dispatch('removeFrequentItemSuccess', itemId);
} catch {
dispatch('removeFrequentItemError');
}
} else {
dispatch('removeFrequentItemError');
}
};