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 types from './mutation_types';
import * as constants from '../constants';
......@@ -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];
state.discussions.push(discussion);
......@@ -220,6 +222,11 @@ export default {
[types.UPDATE_NOTE](state, note) {
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 (note.type === constants.DISCUSSION_NOTE) {
noteObj.individual_note = false;
......@@ -228,7 +235,10 @@ export default {
noteObj.notes.splice(0, 1, note);
} else {
const comment = utils.findNoteObjectById(noteObj.notes, note.id);
noteObj.notes.splice(noteObj.notes.indexOf(comment), 1, note);
if (!isEqual(comment, note)) {
noteObj.notes.splice(noteObj.notes.indexOf(comment), 1, note);
}
}
},
......
......@@ -64,6 +64,11 @@ export default {
mounted() {
this.renderSuggestions();
},
beforeDestroy() {
if (this.suggestionsWatch) {
this.suggestionsWatch();
}
},
methods: {
renderSuggestions() {
// swaps out suggestion(s) markdown with rich diff components
......@@ -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 }) => {
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', () => {
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', () => {
const state = {
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