debian-mirror-gitlab/app/assets/javascripts/boards/stores/mutations.js

313 lines
10 KiB
JavaScript
Raw Normal View History

2021-01-03 14:25:43 +05:30
import { pull, union } from 'lodash';
2021-03-11 19:13:27 +05:30
import Vue from 'vue';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { s__ } from '~/locale';
2021-04-17 20:07:23 +05:30
import { formatIssue, moveItemListHelper } from '../boards_util';
import { issuableTypes } from '../constants';
2019-09-04 21:01:54 +05:30
import * as mutationTypes from './mutation_types';
const notImplemented = () => {
2020-04-22 19:07:51 +05:30
/* eslint-disable-next-line @gitlab/require-i18n-strings */
2019-09-04 21:01:54 +05:30
throw new Error('Not implemented!');
};
2021-04-17 20:07:23 +05:30
const updateListItemsCount = ({ state, listId, value }) => {
2021-01-03 14:25:43 +05:30
const list = state.boardLists[listId];
2021-04-17 20:07:23 +05:30
if (state.issuableType === issuableTypes.epic) {
Vue.set(state.boardLists, listId, { ...list, epicsCount: list.epicsCount + value });
} else {
Vue.set(state.boardLists, listId, { ...list, issuesCount: list.issuesCount + value });
}
};
export const removeItemFromList = ({ state, listId, itemId }) => {
Vue.set(state.boardItemsByListId, listId, pull(state.boardItemsByListId[listId], itemId));
updateListItemsCount({ state, listId, value: -1 });
2020-11-24 15:15:51 +05:30
};
2021-04-17 20:07:23 +05:30
export const addItemToList = ({ state, listId, itemId, moveBeforeId, moveAfterId, atIndex }) => {
const listIssues = state.boardItemsByListId[listId];
2020-11-24 15:15:51 +05:30
let newIndex = atIndex || 0;
if (moveBeforeId) {
newIndex = listIssues.indexOf(moveBeforeId) + 1;
} else if (moveAfterId) {
newIndex = listIssues.indexOf(moveAfterId);
}
2021-04-17 20:07:23 +05:30
listIssues.splice(newIndex, 0, itemId);
Vue.set(state.boardItemsByListId, listId, listIssues);
updateListItemsCount({ state, listId, value: 1 });
2020-11-24 15:15:51 +05:30
};
2019-09-04 21:01:54 +05:30
export default {
2020-11-24 15:15:51 +05:30
[mutationTypes.SET_INITIAL_BOARD_DATA](state, data) {
2021-04-17 20:07:23 +05:30
const { boardType, disabled, boardId, fullPath, boardConfig, issuableType } = data;
2021-03-08 18:12:59 +05:30
state.boardId = boardId;
state.fullPath = fullPath;
2020-10-24 23:57:45 +05:30
state.boardType = boardType;
2020-11-24 15:15:51 +05:30
state.disabled = disabled;
2021-02-22 17:27:13 +05:30
state.boardConfig = boardConfig;
2021-04-17 20:07:23 +05:30
state.issuableType = issuableType;
2020-10-24 23:57:45 +05:30
},
2020-11-24 15:15:51 +05:30
[mutationTypes.RECEIVE_BOARD_LISTS_SUCCESS]: (state, lists) => {
state.boardLists = lists;
},
2021-03-08 18:12:59 +05:30
[mutationTypes.RECEIVE_BOARD_LISTS_FAILURE]: (state) => {
2021-01-03 14:25:43 +05:30
state.error = s__(
'Boards|An error occurred while fetching the board lists. Please reload the page.',
);
},
2020-11-24 15:15:51 +05:30
[mutationTypes.SET_ACTIVE_ID](state, { id, sidebarType }) {
2020-10-24 23:57:45 +05:30
state.activeId = id;
2020-11-24 15:15:51 +05:30
state.sidebarType = sidebarType;
},
[mutationTypes.SET_FILTERS](state, filterParams) {
state.filterParams = filterParams;
},
2021-04-17 20:07:23 +05:30
[mutationTypes.CREATE_LIST_FAILURE]: (
state,
error = s__('Boards|An error occurred while creating the list. Please try again.'),
) => {
state.error = error;
},
[mutationTypes.RECEIVE_LABELS_REQUEST]: (state) => {
state.labelsLoading = true;
2019-09-04 21:01:54 +05:30
},
2021-03-11 19:13:27 +05:30
[mutationTypes.RECEIVE_LABELS_SUCCESS]: (state, labels) => {
state.labels = labels;
2021-04-17 20:07:23 +05:30
state.labelsLoading = false;
},
[mutationTypes.RECEIVE_LABELS_FAILURE]: (state) => {
state.error = s__('Boards|An error occurred while fetching labels. Please reload the page.');
state.labelsLoading = false;
2021-01-29 00:20:46 +05:30
},
2021-03-08 18:12:59 +05:30
[mutationTypes.GENERATE_DEFAULT_LISTS_FAILURE]: (state) => {
2021-01-29 00:20:46 +05:30
state.error = s__('Boards|An error occurred while generating lists. Please reload the page.');
},
2019-09-04 21:01:54 +05:30
[mutationTypes.REQUEST_ADD_LIST]: () => {
notImplemented();
},
2021-01-03 14:25:43 +05:30
[mutationTypes.RECEIVE_ADD_LIST_SUCCESS]: (state, list) => {
Vue.set(state.boardLists, list.id, list);
2019-09-04 21:01:54 +05:30
},
[mutationTypes.RECEIVE_ADD_LIST_ERROR]: () => {
notImplemented();
},
2020-11-24 15:15:51 +05:30
[mutationTypes.MOVE_LIST]: (state, { movedList, listAtNewIndex }) => {
const { boardLists } = state;
2021-01-03 14:25:43 +05:30
Vue.set(boardLists, movedList.id, movedList);
Vue.set(boardLists, listAtNewIndex.id, listAtNewIndex);
2019-09-04 21:01:54 +05:30
},
2020-11-24 15:15:51 +05:30
[mutationTypes.UPDATE_LIST_FAILURE]: (state, backupList) => {
2021-01-03 14:25:43 +05:30
state.error = s__('Boards|An error occurred while updating the list. Please try again.');
2020-11-24 15:15:51 +05:30
Vue.set(state, 'boardLists', backupList);
2019-09-04 21:01:54 +05:30
},
2021-04-17 20:07:23 +05:30
[mutationTypes.TOGGLE_LIST_COLLAPSED]: (state, { listId, collapsed }) => {
Vue.set(state.boardLists[listId], 'collapsed', collapsed);
},
2021-01-29 00:20:46 +05:30
[mutationTypes.REMOVE_LIST]: (state, listId) => {
Vue.delete(state.boardLists, listId);
2019-09-04 21:01:54 +05:30
},
2021-01-29 00:20:46 +05:30
[mutationTypes.REMOVE_LIST_FAILURE](state, listsBackup) {
state.error = s__('Boards|An error occurred while removing the list. Please try again.');
state.boardLists = listsBackup;
2019-09-04 21:01:54 +05:30
},
2021-04-17 20:07:23 +05:30
[mutationTypes.REQUEST_ITEMS_FOR_LIST]: (state, { listId, fetchNext }) => {
2021-01-03 14:25:43 +05:30
Vue.set(state.listsFlags, listId, { [fetchNext ? 'isLoadingMore' : 'isLoading']: true });
},
2021-04-17 20:07:23 +05:30
[mutationTypes.RECEIVE_ITEMS_FOR_LIST_SUCCESS]: (state, { listItems, listPageInfo, listId }) => {
const { listData, boardItems } = listItems;
Vue.set(state, 'boardItems', { ...state.boardItems, ...boardItems });
2021-01-03 14:25:43 +05:30
Vue.set(
2021-04-17 20:07:23 +05:30
state.boardItemsByListId,
2021-01-03 14:25:43 +05:30
listId,
2021-04-17 20:07:23 +05:30
union(state.boardItemsByListId[listId] || [], listData[listId]),
2021-01-03 14:25:43 +05:30
);
Vue.set(state.pageInfoByListId, listId, listPageInfo[listId]);
Vue.set(state.listsFlags, listId, { isLoading: false, isLoadingMore: false });
2020-11-24 15:15:51 +05:30
},
2021-04-17 20:07:23 +05:30
[mutationTypes.RECEIVE_ITEMS_FOR_LIST_FAILURE]: (state, listId) => {
2021-01-03 14:25:43 +05:30
state.error = s__(
'Boards|An error occurred while fetching the board issues. Please reload the page.',
);
Vue.set(state.listsFlags, listId, { isLoading: false, isLoadingMore: false });
2020-10-24 23:57:45 +05:30
},
2021-03-08 18:12:59 +05:30
[mutationTypes.RESET_ISSUES]: (state) => {
2021-04-17 20:07:23 +05:30
Object.keys(state.boardItemsByListId).forEach((listId) => {
Vue.set(state.boardItemsByListId, listId, []);
2021-01-03 14:25:43 +05:30
});
2020-10-24 23:57:45 +05:30
},
2020-11-24 15:15:51 +05:30
[mutationTypes.UPDATE_ISSUE_BY_ID]: (state, { issueId, prop, value }) => {
2021-04-17 20:07:23 +05:30
if (!state.boardItems[issueId]) {
2020-11-24 15:15:51 +05:30
/* eslint-disable-next-line @gitlab/require-i18n-strings */
throw new Error('No issue found.');
}
2021-04-17 20:07:23 +05:30
Vue.set(state.boardItems[issueId], prop, value);
2020-11-24 15:15:51 +05:30
},
2021-02-22 17:27:13 +05:30
[mutationTypes.SET_ASSIGNEE_LOADING](state, isLoading) {
state.isSettingAssignees = isLoading;
},
2019-09-04 21:01:54 +05:30
[mutationTypes.REQUEST_ADD_ISSUE]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_ADD_ISSUE_SUCCESS]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_ADD_ISSUE_ERROR]: () => {
notImplemented();
},
2020-11-24 15:15:51 +05:30
[mutationTypes.MOVE_ISSUE]: (
state,
{ originalIssue, fromListId, toListId, moveBeforeId, moveAfterId },
) => {
2021-01-03 14:25:43 +05:30
const fromList = state.boardLists[fromListId];
const toList = state.boardLists[toListId];
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
const issue = moveItemListHelper(originalIssue, fromList, toList);
Vue.set(state.boardItems, issue.id, issue);
2020-11-24 15:15:51 +05:30
2021-04-17 20:07:23 +05:30
removeItemFromList({ state, listId: fromListId, itemId: issue.id });
addItemToList({ state, listId: toListId, itemId: issue.id, moveBeforeId, moveAfterId });
2019-09-04 21:01:54 +05:30
},
2020-11-24 15:15:51 +05:30
[mutationTypes.MOVE_ISSUE_SUCCESS]: (state, { issue }) => {
const issueId = getIdFromGraphQLId(issue.id);
2021-04-17 20:07:23 +05:30
Vue.set(state.boardItems, issueId, formatIssue({ ...issue, id: issueId }));
2019-09-04 21:01:54 +05:30
},
2020-11-24 15:15:51 +05:30
[mutationTypes.MOVE_ISSUE_FAILURE]: (
state,
{ originalIssue, fromListId, toListId, originalIndex },
) => {
2021-01-03 14:25:43 +05:30
state.error = s__('Boards|An error occurred while moving the issue. Please try again.');
2021-04-17 20:07:23 +05:30
Vue.set(state.boardItems, originalIssue.id, originalIssue);
removeItemFromList({ state, listId: toListId, itemId: originalIssue.id });
addItemToList({
2020-11-24 15:15:51 +05:30
state,
listId: fromListId,
2021-04-17 20:07:23 +05:30
itemId: originalIssue.id,
2020-11-24 15:15:51 +05:30
atIndex: originalIndex,
});
2019-09-04 21:01:54 +05:30
},
[mutationTypes.REQUEST_UPDATE_ISSUE]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_UPDATE_ISSUE_SUCCESS]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_UPDATE_ISSUE_ERROR]: () => {
notImplemented();
},
2021-03-08 18:12:59 +05:30
[mutationTypes.CREATE_ISSUE_FAILURE]: (state) => {
2021-01-29 00:20:46 +05:30
state.error = s__('Boards|An error occurred while creating the issue. Please try again.');
},
2020-11-24 15:15:51 +05:30
[mutationTypes.ADD_ISSUE_TO_LIST]: (state, { list, issue, position }) => {
2021-04-17 20:07:23 +05:30
addItemToList({
2021-01-29 00:20:46 +05:30
state,
listId: list.id,
2021-04-17 20:07:23 +05:30
itemId: issue.id,
2021-01-29 00:20:46 +05:30
atIndex: position,
});
2021-04-17 20:07:23 +05:30
Vue.set(state.boardItems, issue.id, issue);
2020-11-24 15:15:51 +05:30
},
2021-01-29 00:20:46 +05:30
[mutationTypes.ADD_ISSUE_TO_LIST_FAILURE]: (state, { list, issueId }) => {
2021-01-03 14:25:43 +05:30
state.error = s__('Boards|An error occurred while creating the issue. Please try again.');
2021-04-17 20:07:23 +05:30
removeItemFromList({ state, listId: list.id, itemId: issueId });
2021-01-29 00:20:46 +05:30
},
[mutationTypes.REMOVE_ISSUE_FROM_LIST]: (state, { list, issue }) => {
2021-04-17 20:07:23 +05:30
removeItemFromList({ state, listId: list.id, itemId: issue.id });
Vue.delete(state.boardItems, issue.id);
2020-11-24 15:15:51 +05:30
},
2019-09-04 21:01:54 +05:30
[mutationTypes.SET_CURRENT_PAGE]: () => {
notImplemented();
},
[mutationTypes.TOGGLE_EMPTY_STATE]: () => {
notImplemented();
},
2021-03-08 18:12:59 +05:30
[mutationTypes.REQUEST_GROUP_PROJECTS]: (state, fetchNext) => {
Vue.set(state, 'groupProjectsFlags', {
[fetchNext ? 'isLoadingMore' : 'isLoading']: true,
pageInfo: state.groupProjectsFlags.pageInfo,
});
},
[mutationTypes.RECEIVE_GROUP_PROJECTS_SUCCESS]: (state, { projects, pageInfo, fetchNext }) => {
Vue.set(state, 'groupProjects', fetchNext ? [...state.groupProjects, ...projects] : projects);
Vue.set(state, 'groupProjectsFlags', { isLoading: false, isLoadingMore: false, pageInfo });
},
[mutationTypes.RECEIVE_GROUP_PROJECTS_FAILURE]: (state) => {
state.error = s__('Boards|An error occurred while fetching group projects. Please try again.');
Vue.set(state, 'groupProjectsFlags', { isLoading: false, isLoadingMore: false });
},
[mutationTypes.SET_SELECTED_PROJECT]: (state, project) => {
state.selectedProject = project;
},
2021-03-11 19:13:27 +05:30
[mutationTypes.ADD_BOARD_ITEM_TO_SELECTION]: (state, boardItem) => {
state.selectedBoardItems = [...state.selectedBoardItems, boardItem];
},
[mutationTypes.REMOVE_BOARD_ITEM_FROM_SELECTION]: (state, boardItem) => {
Vue.set(
state,
'selectedBoardItems',
state.selectedBoardItems.filter((obj) => obj !== boardItem),
);
},
[mutationTypes.SET_ADD_COLUMN_FORM_VISIBLE]: (state, visible) => {
2021-04-17 20:07:23 +05:30
Vue.set(state.addColumnForm, 'visible', visible);
2021-03-11 19:13:27 +05:30
},
[mutationTypes.ADD_LIST_TO_HIGHLIGHTED_LISTS]: (state, listId) => {
state.highlightedLists.push(listId);
},
[mutationTypes.REMOVE_LIST_FROM_HIGHLIGHTED_LISTS]: (state, listId) => {
state.highlightedLists = state.highlightedLists.filter((id) => id !== listId);
},
2021-04-17 20:07:23 +05:30
[mutationTypes.RESET_BOARD_ITEM_SELECTION]: (state) => {
state.selectedBoardItems = [];
},
2019-09-04 21:01:54 +05:30
};