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

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

429 lines
14 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { isEqual } from 'lodash';
2022-06-21 17:19:12 +05:30
import { isInMRPage } from '~/lib/utils/common_utils';
2021-03-11 19:13:27 +05:30
import * as constants from '../constants';
import * as types from './mutation_types';
import * as utils from './utils';
2018-03-17 18:26:18 +05:30
export default {
2018-12-05 23:21:45 +05:30
[types.ADD_NEW_NOTE](state, data) {
const note = data.discussion ? data.discussion.notes[0] : data;
2022-08-27 11:52:29 +05:30
const { discussion_id: discussionId, type } = note;
2021-03-08 18:12:59 +05:30
const [exists] = state.discussions.filter((n) => n.id === note.discussion_id);
2018-11-08 19:23:39 +05:30
const isDiscussion = type === constants.DISCUSSION_NOTE || type === constants.DIFF_NOTE;
2018-03-17 18:26:18 +05:30
if (!exists) {
2021-01-29 00:20:46 +05:30
let discussion = data.discussion || note.base_discussion;
if (!discussion) {
discussion = {
expanded: true,
2022-08-27 11:52:29 +05:30
id: discussionId,
2021-01-29 00:20:46 +05:30
individual_note: !isDiscussion,
2022-08-27 11:52:29 +05:30
reply_id: discussionId,
2021-01-29 00:20:46 +05:30
};
if (isDiscussion && isInMRPage()) {
discussion.resolvable = note.resolvable;
discussion.resolved = false;
discussion.active = true;
discussion.resolve_path = note.resolve_path;
discussion.resolve_with_issue_path = note.resolve_with_issue_path;
discussion.diff_discussion = false;
}
2018-03-27 19:54:05 +05:30
}
2021-03-11 19:13:27 +05:30
// note.base_discussion = undefined; // No point keeping a reference to this
delete note.base_discussion;
2021-01-29 00:20:46 +05:30
discussion.notes = [note];
state.discussions.push(discussion);
2018-03-17 18:26:18 +05:30
}
},
[types.ADD_NEW_REPLY_TO_DISCUSSION](state, note) {
2019-12-21 20:55:43 +05:30
const discussion = utils.findNoteObjectById(state.discussions, note.discussion_id);
const existingNote = discussion && utils.findNoteObjectById(discussion.notes, note.id);
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
if (discussion && !existingNote) {
discussion.notes.push(note);
2018-03-17 18:26:18 +05:30
}
},
[types.DELETE_NOTE](state, note) {
2018-11-08 19:23:39 +05:30
const noteObj = utils.findNoteObjectById(state.discussions, note.discussion_id);
2018-03-17 18:26:18 +05:30
if (noteObj.individual_note) {
2018-11-08 19:23:39 +05:30
state.discussions.splice(state.discussions.indexOf(noteObj), 1);
2018-03-17 18:26:18 +05:30
} else {
const comment = utils.findNoteObjectById(noteObj.notes, note.id);
noteObj.notes.splice(noteObj.notes.indexOf(comment), 1);
if (!noteObj.notes.length) {
2018-11-08 19:23:39 +05:30
state.discussions.splice(state.discussions.indexOf(noteObj), 1);
2018-03-17 18:26:18 +05:30
}
}
2018-11-08 19:23:39 +05:30
},
[types.EXPAND_DISCUSSION](state, { discussionId }) {
const discussion = utils.findNoteObjectById(state.discussions, discussionId);
2018-11-20 20:47:30 +05:30
Object.assign(discussion, { expanded: true });
2018-11-08 19:23:39 +05:30
},
2018-03-27 19:54:05 +05:30
2018-11-08 19:23:39 +05:30
[types.COLLAPSE_DISCUSSION](state, { discussionId }) {
const discussion = utils.findNoteObjectById(state.discussions, discussionId);
2018-11-20 20:47:30 +05:30
Object.assign(discussion, { expanded: false });
2018-03-17 18:26:18 +05:30
},
[types.REMOVE_PLACEHOLDER_NOTES](state) {
2018-11-08 19:23:39 +05:30
const { discussions } = state;
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
for (let i = discussions.length - 1; i >= 0; i -= 1) {
const note = discussions[i];
2018-03-17 18:26:18 +05:30
const children = note.notes;
2018-05-09 12:01:36 +05:30
if (children.length && !note.individual_note) {
// remove placeholder from discussions
2018-03-17 18:26:18 +05:30
for (let j = children.length - 1; j >= 0; j -= 1) {
if (children[j].isPlaceholderNote) {
children.splice(j, 1);
}
}
2018-05-09 12:01:36 +05:30
} else if (note.isPlaceholderNote) {
// remove placeholders from state root
2018-11-08 19:23:39 +05:30
discussions.splice(i, 1);
2018-03-17 18:26:18 +05:30
}
}
},
[types.SET_NOTES_DATA](state, data) {
Object.assign(state, { notesData: data });
},
[types.SET_NOTEABLE_DATA](state, data) {
Object.assign(state, { noteableData: data });
},
2020-07-28 23:09:34 +05:30
[types.SET_ISSUE_CONFIDENTIAL](state, data) {
state.noteableData.confidential = data;
},
2020-10-24 23:57:45 +05:30
[types.SET_ISSUABLE_LOCK](state, locked) {
state.noteableData.discussion_locked = locked;
},
2018-03-17 18:26:18 +05:30
[types.SET_USER_DATA](state, data) {
Object.assign(state, { userData: data });
},
2018-11-20 20:47:30 +05:30
2022-01-26 12:08:38 +05:30
[types.CLEAR_DISCUSSIONS](state) {
state.discussions = [];
},
2021-12-11 22:18:48 +05:30
[types.ADD_OR_UPDATE_DISCUSSIONS](state, discussionsData) {
discussionsData.forEach((d) => {
2019-02-15 15:39:39 +05:30
const discussion = { ...d };
const diffData = {};
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
if (discussion.diff_file) {
2019-02-15 15:39:39 +05:30
diffData.file_hash = discussion.diff_file.file_hash;
2019-03-02 22:35:43 +05:30
diffData.truncated_diff_lines = utils.prepareDiffLines(
discussion.truncated_diff_lines || [],
);
2018-11-20 20:47:30 +05:30
}
2018-03-17 18:26:18 +05:30
// To support legacy notes, should be very rare case.
2018-11-08 19:23:39 +05:30
if (discussion.individual_note && discussion.notes.length > 1) {
2021-03-08 18:12:59 +05:30
discussion.notes.forEach((n) => {
2021-12-11 22:18:48 +05:30
const newDiscussion = {
2018-11-08 19:23:39 +05:30
...discussion,
2019-02-15 15:39:39 +05:30
...diffData,
2018-03-27 19:54:05 +05:30
notes: [n], // override notes array to only have one item to mimick individual_note
2021-12-11 22:18:48 +05:30
};
const oldDiscussion = state.discussions.find(
(existingDiscussion) =>
existingDiscussion.id === discussion.id && existingDiscussion.notes[0].id === n.id,
);
if (oldDiscussion) {
state.discussions.splice(state.discussions.indexOf(oldDiscussion), 1, newDiscussion);
} else {
state.discussions.push(newDiscussion);
}
2018-03-17 18:26:18 +05:30
});
} else {
2021-12-11 22:18:48 +05:30
const oldDiscussion = utils.findNoteObjectById(state.discussions, discussion.id);
2018-03-27 19:54:05 +05:30
2021-12-11 22:18:48 +05:30
if (oldDiscussion) {
state.discussions.splice(state.discussions.indexOf(oldDiscussion), 1, {
...discussion,
...diffData,
expanded: oldDiscussion.expanded,
});
} else {
state.discussions.push({ ...discussion, ...diffData });
}
2018-03-17 18:26:18 +05:30
}
2021-12-11 22:18:48 +05:30
});
2018-03-17 18:26:18 +05:30
},
2021-12-11 22:18:48 +05:30
2018-03-17 18:26:18 +05:30
[types.SET_LAST_FETCHED_AT](state, fetchedAt) {
Object.assign(state, { lastFetchedAt: fetchedAt });
},
[types.SET_TARGET_NOTE_HASH](state, hash) {
Object.assign(state, { targetNoteHash: hash });
},
[types.SHOW_PLACEHOLDER_NOTE](state, data) {
2018-11-08 19:23:39 +05:30
let notesArr = state.discussions;
const existingDiscussion = utils.findNoteObjectById(notesArr, data.replyId);
if (existingDiscussion) {
notesArr = existingDiscussion.notes;
2018-03-17 18:26:18 +05:30
}
notesArr.push({
individual_note: true,
isPlaceholderNote: true,
2018-11-08 19:23:39 +05:30
placeholderType: data.isSystemNote ? constants.SYSTEM_NOTE : constants.NOTE,
2018-03-17 18:26:18 +05:30
notes: [
{
body: data.noteBody,
},
],
});
},
[types.TOGGLE_AWARD](state, data) {
const { awardName, note } = data;
const { id, name, username } = state.userData;
2018-05-09 12:01:36 +05:30
const hasEmojiAwardedByCurrentUser = note.award_emoji.filter(
2021-03-08 18:12:59 +05:30
(emoji) => `${emoji.name}` === `${data.awardName}` && emoji.user.id === id,
2018-05-09 12:01:36 +05:30
);
2018-03-17 18:26:18 +05:30
if (hasEmojiAwardedByCurrentUser.length) {
// If current user has awarded this emoji, remove it.
2018-11-08 19:23:39 +05:30
note.award_emoji.splice(note.award_emoji.indexOf(hasEmojiAwardedByCurrentUser[0]), 1);
2018-03-17 18:26:18 +05:30
} else {
note.award_emoji.push({
name: awardName,
user: { id, name, username },
});
}
},
2019-02-15 15:39:39 +05:30
[types.TOGGLE_DISCUSSION](state, { discussionId, forceExpanded = null }) {
2018-11-08 19:23:39 +05:30
const discussion = utils.findNoteObjectById(state.discussions, discussionId);
2019-02-15 15:39:39 +05:30
Object.assign(discussion, {
expanded: forceExpanded === null ? !discussion.expanded : forceExpanded,
});
2018-03-17 18:26:18 +05:30
},
2020-04-22 19:07:51 +05:30
[types.SET_EXPAND_DISCUSSIONS](state, { discussionIds, expanded }) {
if (discussionIds?.length) {
2021-03-08 18:12:59 +05:30
discussionIds.forEach((discussionId) => {
2020-04-22 19:07:51 +05:30
const discussion = utils.findNoteObjectById(state.discussions, discussionId);
Object.assign(discussion, { expanded });
});
}
},
2021-02-22 17:27:13 +05:30
[types.SET_RESOLVING_DISCUSSION](state, isResolving) {
state.isResolvingDiscussion = isResolving;
},
2018-03-17 18:26:18 +05:30
[types.UPDATE_NOTE](state, note) {
2018-11-08 19:23:39 +05:30
const noteObj = utils.findNoteObjectById(state.discussions, note.discussion_id);
2018-03-17 18:26:18 +05:30
2021-03-11 19:13:27 +05:30
// Disable eslint here so we can delete the property that we no longer need
// in the note object
// eslint-disable-next-line no-param-reassign
delete note.base_discussion;
2018-03-17 18:26:18 +05:30
if (noteObj.individual_note) {
2019-07-07 11:18:12 +05:30
if (note.type === constants.DISCUSSION_NOTE) {
noteObj.individual_note = false;
}
2018-03-17 18:26:18 +05:30
noteObj.notes.splice(0, 1, note);
} else {
const comment = utils.findNoteObjectById(noteObj.notes, note.id);
2021-03-11 19:13:27 +05:30
if (!isEqual(comment, note)) {
noteObj.notes.splice(noteObj.notes.indexOf(comment), 1, note);
}
2018-03-17 18:26:18 +05:30
}
2018-03-27 19:54:05 +05:30
},
2019-02-15 15:39:39 +05:30
[types.APPLY_SUGGESTION](state, { noteId, discussionId, suggestionId }) {
const noteObj = utils.findNoteObjectById(state.discussions, discussionId);
const comment = utils.findNoteObjectById(noteObj.notes, noteId);
2021-03-08 18:12:59 +05:30
comment.suggestions = comment.suggestions.map((suggestion) => ({
2019-02-15 15:39:39 +05:30
...suggestion,
applied: suggestion.applied || suggestion.id === suggestionId,
appliable: false,
}));
},
2020-06-23 00:09:42 +05:30
[types.SET_APPLYING_BATCH_STATE](state, isApplyingBatch) {
2021-03-08 18:12:59 +05:30
state.batchSuggestionsInfo.forEach((suggestionInfo) => {
2020-06-23 00:09:42 +05:30
const { discussionId, noteId, suggestionId } = suggestionInfo;
const noteObj = utils.findNoteObjectById(state.discussions, discussionId);
const comment = utils.findNoteObjectById(noteObj.notes, noteId);
2021-03-08 18:12:59 +05:30
comment.suggestions = comment.suggestions.map((suggestion) => ({
2020-06-23 00:09:42 +05:30
...suggestion,
is_applying_batch: suggestion.id === suggestionId && isApplyingBatch,
}));
});
},
[types.ADD_SUGGESTION_TO_BATCH](state, { noteId, discussionId, suggestionId }) {
state.batchSuggestionsInfo.push({
suggestionId,
noteId,
discussionId,
});
},
[types.REMOVE_SUGGESTION_FROM_BATCH](state, id) {
const index = state.batchSuggestionsInfo.findIndex(({ suggestionId }) => suggestionId === id);
if (index !== -1) {
state.batchSuggestionsInfo.splice(index, 1);
}
},
[types.CLEAR_SUGGESTION_BATCH](state) {
state.batchSuggestionsInfo.splice(0, state.batchSuggestionsInfo.length);
},
2018-03-27 19:54:05 +05:30
[types.UPDATE_DISCUSSION](state, noteData) {
const note = noteData;
2021-03-08 18:12:59 +05:30
const selectedDiscussion = state.discussions.find((disc) => disc.id === note.id);
2018-03-27 19:54:05 +05:30
note.expanded = true; // override expand flag to prevent collapse
2018-11-20 20:47:30 +05:30
if (note.diff_file) {
2019-02-15 15:39:39 +05:30
Object.assign(note, {
file_hash: note.diff_file.file_hash,
});
2018-11-20 20:47:30 +05:30
}
Object.assign(selectedDiscussion, { ...note });
2018-03-27 19:54:05 +05:30
},
2020-10-24 23:57:45 +05:30
[types.UPDATE_DISCUSSION_POSITION](state, { discussionId, position }) {
2021-03-08 18:12:59 +05:30
const selectedDiscussion = state.discussions.find((disc) => disc.id === discussionId);
2020-10-24 23:57:45 +05:30
if (selectedDiscussion) Object.assign(selectedDiscussion.position, { ...position });
},
2018-03-27 19:54:05 +05:30
[types.CLOSE_ISSUE](state) {
Object.assign(state.noteableData, { state: constants.CLOSED });
},
[types.REOPEN_ISSUE](state) {
Object.assign(state.noteableData, { state: constants.REOPENED });
},
[types.TOGGLE_STATE_BUTTON_LOADING](state, value) {
Object.assign(state, { isToggleStateButtonLoading: value });
2018-03-17 18:26:18 +05:30
},
2018-11-08 19:23:39 +05:30
[types.SET_NOTES_FETCHED_STATE](state, value) {
Object.assign(state, { isNotesFetched: value });
},
2018-12-13 13:39:08 +05:30
[types.SET_NOTES_LOADING_STATE](state, value) {
state.isLoading = value;
},
2021-03-11 19:13:27 +05:30
[types.SET_NOTES_FETCHING_STATE](state, value) {
state.isFetching = value;
},
2018-11-08 19:23:39 +05:30
[types.SET_DISCUSSION_DIFF_LINES](state, { discussionId, diffLines }) {
const discussion = utils.findNoteObjectById(state.discussions, discussionId);
2019-03-02 22:35:43 +05:30
discussion.truncated_diff_lines = utils.prepareDiffLines(diffLines);
2018-11-08 19:23:39 +05:30
},
2018-12-13 13:39:08 +05:30
2021-01-03 14:25:43 +05:30
[types.SET_DISCUSSIONS_SORT](state, { direction, persist }) {
state.discussionSortOrder = direction;
state.persistSortOrder = persist;
},
[types.SET_TIMELINE_VIEW](state, value) {
state.isTimelineEnabled = value;
2020-04-22 19:07:51 +05:30
},
2020-07-28 23:09:34 +05:30
[types.SET_SELECTED_COMMENT_POSITION](state, position) {
state.selectedCommentPosition = position;
},
[types.SET_SELECTED_COMMENT_POSITION_HOVER](state, position) {
state.selectedCommentPositionHover = position;
},
2018-12-13 13:39:08 +05:30
[types.DISABLE_COMMENTS](state, value) {
state.commentsDisabled = value;
},
2019-02-15 15:39:39 +05:30
[types.UPDATE_RESOLVABLE_DISCUSSIONS_COUNTS](state) {
state.resolvableDiscussionsCount = state.discussions.filter(
2021-03-08 18:12:59 +05:30
(discussion) => !discussion.individual_note && discussion.resolvable,
2019-02-15 15:39:39 +05:30
).length;
state.unresolvedDiscussionsCount = state.discussions.filter(
2021-03-08 18:12:59 +05:30
(discussion) =>
2019-02-15 15:39:39 +05:30
!discussion.individual_note &&
discussion.resolvable &&
2021-03-08 18:12:59 +05:30
discussion.notes.some((note) => note.resolvable && !note.resolved),
2019-02-15 15:39:39 +05:30
).length;
},
2019-03-02 22:35:43 +05:30
[types.CONVERT_TO_DISCUSSION](state, discussionId) {
2019-07-07 11:18:12 +05:30
const convertedDisscussionIds = [...state.convertedDisscussionIds, discussionId];
Object.assign(state, { convertedDisscussionIds });
},
[types.REMOVE_CONVERTED_DISCUSSION](state, discussionId) {
const convertedDisscussionIds = [...state.convertedDisscussionIds];
convertedDisscussionIds.splice(convertedDisscussionIds.indexOf(discussionId), 1);
Object.assign(state, { convertedDisscussionIds });
2019-03-02 22:35:43 +05:30
},
2020-03-13 15:44:24 +05:30
[types.SET_CURRENT_DISCUSSION_ID](state, discussionId) {
state.currentDiscussionId = discussionId;
},
[types.REQUEST_DESCRIPTION_VERSION](state) {
state.isLoadingDescriptionVersion = true;
},
2020-04-08 14:13:33 +05:30
[types.RECEIVE_DESCRIPTION_VERSION](state, { descriptionVersion, versionId }) {
const descriptionVersions = { ...state.descriptionVersions, [versionId]: descriptionVersion };
Object.assign(state, { descriptionVersions, isLoadingDescriptionVersion: false });
2020-03-13 15:44:24 +05:30
},
[types.RECEIVE_DESCRIPTION_VERSION_ERROR](state) {
state.isLoadingDescriptionVersion = false;
},
[types.REQUEST_DELETE_DESCRIPTION_VERSION](state) {
state.isLoadingDescriptionVersion = true;
},
[types.RECEIVE_DELETE_DESCRIPTION_VERSION](state, descriptionVersion) {
state.isLoadingDescriptionVersion = false;
2020-04-08 14:13:33 +05:30
Object.assign(state.descriptionVersions, descriptionVersion);
2020-03-13 15:44:24 +05:30
},
[types.RECEIVE_DELETE_DESCRIPTION_VERSION_ERROR](state) {
state.isLoadingDescriptionVersion = false;
},
2020-06-23 00:09:42 +05:30
[types.UPDATE_ASSIGNEES](state, assignees) {
state.noteableData.assignees = assignees;
},
2020-11-24 15:15:51 +05:30
[types.SET_FETCHING_DISCUSSIONS](state, value) {
state.currentlyFetchingDiscussions = value;
},
2022-07-23 23:45:48 +05:30
[types.SET_DONE_FETCHING_BATCH_DISCUSSIONS](state, value) {
state.doneFetchingBatchDiscussions = value;
},
2018-03-17 18:26:18 +05:30
};