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

219 lines
6.7 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import Vue from 'vue';
2021-01-03 14:25:43 +05:30
import { pull, union } from 'lodash';
2020-11-24 15:15:51 +05:30
import { formatIssue, moveIssueListHelper } from '../boards_util';
2019-09-04 21:01:54 +05:30
import * as mutationTypes from './mutation_types';
2021-01-03 14:25:43 +05:30
import { s__ } from '~/locale';
2020-11-24 15:15:51 +05:30
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
2019-09-04 21:01:54 +05:30
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-01-03 14:25:43 +05:30
export const removeIssueFromList = ({ state, listId, issueId }) => {
2020-11-24 15:15:51 +05:30
Vue.set(state.issuesByListId, listId, pull(state.issuesByListId[listId], issueId));
2021-01-03 14:25:43 +05:30
const list = state.boardLists[listId];
Vue.set(state.boardLists, listId, { ...list, issuesSize: list.issuesSize - 1 });
2020-11-24 15:15:51 +05:30
};
2021-01-03 14:25:43 +05:30
export const addIssueToList = ({ state, listId, issueId, moveBeforeId, moveAfterId, atIndex }) => {
2020-11-24 15:15:51 +05:30
const listIssues = state.issuesByListId[listId];
let newIndex = atIndex || 0;
if (moveBeforeId) {
newIndex = listIssues.indexOf(moveBeforeId) + 1;
} else if (moveAfterId) {
newIndex = listIssues.indexOf(moveAfterId);
}
listIssues.splice(newIndex, 0, issueId);
Vue.set(state.issuesByListId, listId, listIssues);
2021-01-03 14:25:43 +05:30
const list = state.boardLists[listId];
Vue.set(state.boardLists, listId, { ...list, issuesSize: list.issuesSize + 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) {
const { boardType, disabled, showPromotion, ...endpoints } = data;
2020-06-23 00:09:42 +05:30
state.endpoints = endpoints;
2020-10-24 23:57:45 +05:30
state.boardType = boardType;
2020-11-24 15:15:51 +05:30
state.disabled = disabled;
state.showPromotion = showPromotion;
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-01-03 14:25:43 +05:30
[mutationTypes.RECEIVE_BOARD_LISTS_FAILURE]: state => {
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;
},
[mutationTypes.CREATE_LIST_FAILURE]: state => {
2021-01-03 14:25:43 +05:30
state.error = s__('Boards|An error occurred while creating the list. Please try again.');
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
},
[mutationTypes.REQUEST_REMOVE_LIST]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_REMOVE_LIST_SUCCESS]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_REMOVE_LIST_ERROR]: () => {
notImplemented();
},
2021-01-03 14:25:43 +05:30
[mutationTypes.REQUEST_ISSUES_FOR_LIST]: (state, { listId, fetchNext }) => {
Vue.set(state.listsFlags, listId, { [fetchNext ? 'isLoadingMore' : 'isLoading']: true });
},
[mutationTypes.RECEIVE_ISSUES_FOR_LIST_SUCCESS]: (
state,
{ listIssues, listPageInfo, listId },
) => {
2020-11-24 15:15:51 +05:30
const { listData, issues } = listIssues;
Vue.set(state, 'issues', { ...state.issues, ...issues });
2021-01-03 14:25:43 +05:30
Vue.set(
state.issuesByListId,
listId,
union(state.issuesByListId[listId] || [], listData[listId]),
);
Vue.set(state.pageInfoByListId, listId, listPageInfo[listId]);
Vue.set(state.listsFlags, listId, { isLoading: false, isLoadingMore: false });
2020-11-24 15:15:51 +05:30
},
[mutationTypes.RECEIVE_ISSUES_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-01-03 14:25:43 +05:30
[mutationTypes.RESET_ISSUES]: state => {
Object.keys(state.issuesByListId).forEach(listId => {
Vue.set(state.issuesByListId, listId, []);
});
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 }) => {
if (!state.issues[issueId]) {
/* eslint-disable-next-line @gitlab/require-i18n-strings */
throw new Error('No issue found.');
}
Vue.set(state.issues[issueId], prop, value);
},
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
const issue = moveIssueListHelper(originalIssue, fromList, toList);
Vue.set(state.issues, issue.id, issue);
2021-01-03 14:25:43 +05:30
removeIssueFromList({ state, listId: fromListId, issueId: issue.id });
2020-11-24 15:15:51 +05:30
addIssueToList({ state, listId: toListId, issueId: 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);
Vue.set(state.issues, 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.');
2020-11-24 15:15:51 +05:30
Vue.set(state.issues, originalIssue.id, originalIssue);
2021-01-03 14:25:43 +05:30
removeIssueFromList({ state, listId: toListId, issueId: originalIssue.id });
2020-11-24 15:15:51 +05:30
addIssueToList({
state,
listId: fromListId,
issueId: originalIssue.id,
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();
},
2020-11-24 15:15:51 +05:30
[mutationTypes.ADD_ISSUE_TO_LIST]: (state, { list, issue, position }) => {
const listIssues = state.issuesByListId[list.id];
listIssues.splice(position, 0, issue.id);
Vue.set(state.issuesByListId, list.id, listIssues);
Vue.set(state.issues, issue.id, issue);
},
[mutationTypes.ADD_ISSUE_TO_LIST_FAILURE]: (state, { list, issue }) => {
2021-01-03 14:25:43 +05:30
state.error = s__('Boards|An error occurred while creating the issue. Please try again.');
removeIssueFromList({ state, listId: list.id, issueId: 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();
},
};