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

208 lines
6.5 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import Vue from 'vue';
import { sortBy, pull } from 'lodash';
import { formatIssue, moveIssueListHelper } from '../boards_util';
2019-09-04 21:01:54 +05:30
import * as mutationTypes from './mutation_types';
2020-11-24 15:15:51 +05:30
import { __ } from '~/locale';
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!');
};
2020-11-24 15:15:51 +05:30
const removeIssueFromList = (state, listId, issueId) => {
Vue.set(state.issuesByListId, listId, pull(state.issuesByListId[listId], issueId));
};
const addIssueToList = ({ state, listId, issueId, moveBeforeId, moveAfterId, atIndex }) => {
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);
};
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;
},
[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 => {
state.error = __('An error occurred while creating the list. Please try again.');
2019-09-04 21:01:54 +05:30
},
[mutationTypes.REQUEST_ADD_LIST]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_ADD_LIST_SUCCESS]: () => {
notImplemented();
},
[mutationTypes.RECEIVE_ADD_LIST_ERROR]: () => {
notImplemented();
},
2020-11-24 15:15:51 +05:30
[mutationTypes.MOVE_LIST]: (state, { movedList, listAtNewIndex }) => {
const { boardLists } = state;
const movedListIndex = state.boardLists.findIndex(l => l.id === movedList.id);
Vue.set(boardLists, movedListIndex, movedList);
Vue.set(boardLists, movedListIndex.position + 1, listAtNewIndex);
Vue.set(state, 'boardLists', sortBy(boardLists, 'position'));
2019-09-04 21:01:54 +05:30
},
2020-11-24 15:15:51 +05:30
[mutationTypes.UPDATE_LIST_FAILURE]: (state, backupList) => {
state.error = __('An error occurred while updating the list. Please try again.');
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();
},
2020-11-24 15:15:51 +05:30
[mutationTypes.RECEIVE_ISSUES_FOR_LIST_SUCCESS]: (state, { listIssues, listId }) => {
const { listData, issues } = listIssues;
Vue.set(state, 'issues', { ...state.issues, ...issues });
Vue.set(state.issuesByListId, listId, listData[listId]);
const listIndex = state.boardLists.findIndex(l => l.id === listId);
Vue.set(state.boardLists[listIndex], 'loading', false);
},
[mutationTypes.RECEIVE_ISSUES_FOR_LIST_FAILURE]: (state, listId) => {
state.error = __('An error occurred while fetching the board issues. Please reload the page.');
const listIndex = state.boardLists.findIndex(l => l.id === listId);
Vue.set(state.boardLists[listIndex], 'loading', false);
},
2020-10-24 23:57:45 +05:30
[mutationTypes.REQUEST_ISSUES_FOR_ALL_LISTS]: state => {
state.isLoadingIssues = true;
},
2020-11-24 15:15:51 +05:30
[mutationTypes.RECEIVE_ISSUES_FOR_ALL_LISTS_SUCCESS]: (state, { listData, issues }) => {
state.issuesByListId = listData;
state.issues = issues;
2020-10-24 23:57:45 +05:30
state.isLoadingIssues = false;
},
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);
},
2020-10-24 23:57:45 +05:30
[mutationTypes.RECEIVE_ISSUES_FOR_ALL_LISTS_FAILURE]: state => {
2020-11-24 15:15:51 +05:30
state.error = __('An error occurred while fetching the board issues. Please reload the page.');
2020-10-24 23:57:45 +05:30
state.isLoadingIssues = false;
},
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 },
) => {
const fromList = state.boardLists.find(l => l.id === fromListId);
const toList = state.boardLists.find(l => l.id === toListId);
const issue = moveIssueListHelper(originalIssue, fromList, toList);
Vue.set(state.issues, issue.id, issue);
removeIssueFromList(state, fromListId, issue.id);
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 },
) => {
state.error = __('An error occurred while moving the issue. Please try again.');
Vue.set(state.issues, originalIssue.id, originalIssue);
removeIssueFromList(state, toListId, originalIssue.id);
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 }) => {
state.error = __('An error occurred while creating the issue. Please try again.');
removeIssueFromList(state, list.id, issue.id);
},
2019-09-04 21:01:54 +05:30
[mutationTypes.SET_CURRENT_PAGE]: () => {
notImplemented();
},
[mutationTypes.TOGGLE_EMPTY_STATE]: () => {
notImplemented();
},
};