Commit 0fba7cca authored by Phil Hughes's avatar Phil Hughes

Update the state, not a param

Also fixed a bug where discussions wouldn't be assigned to a line
when switching from discussion tab to changes tab
parent 80a689fa
...@@ -63,7 +63,7 @@ export default { ...@@ -63,7 +63,7 @@ export default {
plainDiffPath: state => state.diffs.plainDiffPath, plainDiffPath: state => state.diffs.plainDiffPath,
emailPatchPath: state => state.diffs.emailPatchPath, emailPatchPath: state => state.diffs.emailPatchPath,
}), }),
...mapState('diffs', ['showTreeList']), ...mapState('diffs', ['showTreeList', 'isLoading']),
...mapGetters('diffs', ['isParallelView']), ...mapGetters('diffs', ['isParallelView']),
...mapGetters(['isNotesFetched', 'getNoteableData']), ...mapGetters(['isNotesFetched', 'getNoteableData']),
targetBranch() { targetBranch() {
...@@ -152,7 +152,7 @@ export default { ...@@ -152,7 +152,7 @@ export default {
} }
}, },
setDiscussions() { setDiscussions() {
if (this.isNotesFetched && !this.assignedDiscussions) { if (this.isNotesFetched && !this.assignedDiscussions && !this.isLoading) {
requestIdleCallback( requestIdleCallback(
() => () =>
this.assignDiscussionsToDiff().then(() => { this.assignDiscussionsToDiff().then(() => {
......
...@@ -37,29 +37,13 @@ export const fetchDiffFiles = ({ state, commit }) => { ...@@ -37,29 +37,13 @@ export const fetchDiffFiles = ({ state, commit }) => {
// once for parallel and once for inline mode // once for parallel and once for inline mode
export const assignDiscussionsToDiff = ( export const assignDiscussionsToDiff = (
{ commit, state, rootState }, { commit, state, rootState },
discussionsToAssign = rootState.notes.discussions, discussions = rootState.notes.discussions,
) => { ) => {
const diffPositionByLineCode = getDiffPositionByLineCode(state.diffFiles); const diffPositionByLineCode = getDiffPositionByLineCode(state.diffFiles);
const discussionsByDiffFile = discussionsToAssign
.filter(discussion => discussion.diff_discussion) discussions.filter(discussion => discussion.diff_discussion).forEach(discussion => {
.reduce((acc, discussion) => {
const discussions = (
acc[discussion.diff_file.file_hash] || { discussions: [] }
).discussions.concat(discussion);
return {
...acc,
[discussion.diff_file.file_hash]: {
diffFile: state.diffFiles.find(file => file.fileHash === discussion.diff_file.file_hash),
discussions,
},
};
}, {});
Object.values(discussionsByDiffFile).forEach(({ discussions, diffFile }) => {
commit(types.SET_LINE_DISCUSSIONS_FOR_FILE, { commit(types.SET_LINE_DISCUSSIONS_FOR_FILE, {
diffFile, discussion,
discussions,
diffPositionByLineCode, diffPositionByLineCode,
}); });
}); });
......
...@@ -90,49 +90,66 @@ export default { ...@@ -90,49 +90,66 @@ export default {
})); }));
}, },
[types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { diffFile, discussions, diffPositionByLineCode }) { [types.SET_LINE_DISCUSSIONS_FOR_FILE](state, { discussion, diffPositionByLineCode }) {
const { latestDiff } = state; const { latestDiff } = state;
discussions.forEach(discussion => { const discussionLineCode = discussion.line_code;
const discussionLineCode = discussion.line_code; const fileHash = discussion.diff_file.file_hash;
const lineCheck = ({ lineCode }) => const lineCheck = ({ lineCode }) =>
lineCode === discussionLineCode && lineCode === discussionLineCode &&
isDiscussionApplicableToLine({ isDiscussionApplicableToLine({
discussion, discussion,
diffPosition: diffPositionByLineCode[lineCode], diffPosition: diffPositionByLineCode[lineCode],
latestDiff, latestDiff,
}); });
if (diffFile.highlightedDiffLines) { state.diffFiles = state.diffFiles.map(diffFile => {
const line = diffFile.highlightedDiffLines.find(lineCheck); if (diffFile.fileHash === fileHash) {
const file = { ...diffFile };
if (line) {
Object.assign(line, { if (file.highlightedDiffLines) {
discussions: line.discussions.concat(discussion), file.highlightedDiffLines = file.highlightedDiffLines.map(line => {
if (lineCheck(line)) {
return {
...line,
discussions: line.discussions.concat(discussion),
};
}
return line;
}); });
} }
}
if (diffFile.parallelDiffLines) {
const { left, right } = diffFile.parallelDiffLines.find(
parallelLine =>
(parallelLine.left && lineCheck(parallelLine.left)) ||
(parallelLine.right && lineCheck(parallelLine.right)),
);
const line = left && left.lineCode === discussionLineCode ? left : right;
if (line) { if (file.parallelDiffLines) {
Object.assign(line, { file.parallelDiffLines = file.parallelDiffLines.map(line => {
discussions: line.discussions.concat(discussion), const left = line.left && lineCheck(line.left);
const right = line.right && lineCheck(line.right);
if (left || right) {
return {
left: {
...line.left,
discussions: left ? line.left.discussions.concat(discussion) : [],
},
right: {
...line.right,
discussions: right ? line.right.discussions.concat(discussion) : [],
},
};
}
return line;
}); });
} }
}
if (!diffFile.parallelDiffLines || !diffFile.highlightedDiffLines) { if (!file.parallelDiffLines || !file.highlightedDiffLines) {
Object.assign(diffFile, { file.discussions = file.discussions.concat(discussion);
discussions: diffFile.discussions.concat(discussion), }
});
return file;
} }
return diffFile;
}); });
}, },
......
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