Commit b2ef44d5 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'ph/235741/fixSuggestionsUpdatingIncorrectly' into 'master'

Fixes suggestions being overwritten incorrectly

See merge request gitlab-org/gitlab!51988
parents 767650e8 0f6e11cb
import { isEqual } from 'lodash';
import * as utils from './utils'; import * as utils from './utils';
import * as types from './mutation_types'; import * as types from './mutation_types';
import * as constants from '../constants'; import * as constants from '../constants';
...@@ -31,7 +32,8 @@ export default { ...@@ -31,7 +32,8 @@ export default {
} }
} }
note.base_discussion = undefined; // No point keeping a reference to this // note.base_discussion = undefined; // No point keeping a reference to this
delete note.base_discussion;
discussion.notes = [note]; discussion.notes = [note];
state.discussions.push(discussion); state.discussions.push(discussion);
...@@ -220,6 +222,11 @@ export default { ...@@ -220,6 +222,11 @@ export default {
[types.UPDATE_NOTE](state, note) { [types.UPDATE_NOTE](state, note) {
const noteObj = utils.findNoteObjectById(state.discussions, note.discussion_id); const noteObj = utils.findNoteObjectById(state.discussions, note.discussion_id);
// 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;
if (noteObj.individual_note) { if (noteObj.individual_note) {
if (note.type === constants.DISCUSSION_NOTE) { if (note.type === constants.DISCUSSION_NOTE) {
noteObj.individual_note = false; noteObj.individual_note = false;
...@@ -228,8 +235,11 @@ export default { ...@@ -228,8 +235,11 @@ export default {
noteObj.notes.splice(0, 1, note); noteObj.notes.splice(0, 1, note);
} else { } else {
const comment = utils.findNoteObjectById(noteObj.notes, note.id); const comment = utils.findNoteObjectById(noteObj.notes, note.id);
if (!isEqual(comment, note)) {
noteObj.notes.splice(noteObj.notes.indexOf(comment), 1, note); noteObj.notes.splice(noteObj.notes.indexOf(comment), 1, note);
} }
}
}, },
[types.APPLY_SUGGESTION](state, { noteId, discussionId, suggestionId }) { [types.APPLY_SUGGESTION](state, { noteId, discussionId, suggestionId }) {
......
...@@ -64,6 +64,11 @@ export default { ...@@ -64,6 +64,11 @@ export default {
mounted() { mounted() {
this.renderSuggestions(); this.renderSuggestions();
}, },
beforeDestroy() {
if (this.suggestionsWatch) {
this.suggestionsWatch();
}
},
methods: { methods: {
renderSuggestions() { renderSuggestions() {
// swaps out suggestion(s) markdown with rich diff components // swaps out suggestion(s) markdown with rich diff components
...@@ -108,6 +113,13 @@ export default { ...@@ -108,6 +113,13 @@ export default {
}, },
}); });
// We're using `$watch` as `suggestionsCount` updates do not
// propagate to this component for some unknown reason while
// using a traditional prop watcher.
this.suggestionsWatch = this.$watch('suggestionsCount', () => {
suggestionDiff.suggestionsCount = this.suggestionsCount;
});
suggestionDiff.$on('apply', ({ suggestionId, callback, message }) => { suggestionDiff.$on('apply', ({ suggestionId, callback, message }) => {
this.$emit('apply', { suggestionId, callback, flashContainer: this.$el, message }); this.$emit('apply', { suggestionId, callback, flashContainer: this.$el, message });
}); });
......
---
title: Fixed notes polling incorrectly overwriting suggestions in the DOM
merge_request: 51988
author:
type: fixed
...@@ -400,6 +400,19 @@ describe('Notes Store mutations', () => { ...@@ -400,6 +400,19 @@ describe('Notes Store mutations', () => {
expect(state.discussions[0].notes[0].note).toEqual('Foo'); expect(state.discussions[0].notes[0].note).toEqual('Foo');
}); });
it('does not update existing note if it matches', () => {
const state = {
discussions: [{ ...individualNote, individual_note: false }],
};
jest.spyOn(state.discussions[0].notes, 'splice');
const updated = individualNote.notes[0];
mutations.UPDATE_NOTE(state, updated);
expect(state.discussions[0].notes.splice).not.toHaveBeenCalled();
});
it('transforms an individual note to discussion', () => { it('transforms an individual note to discussion', () => {
const state = { const state = {
discussions: [individualNote], discussions: [individualNote],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment