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)
.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 }) => { discussions.filter(discussion => discussion.diff_discussion).forEach(discussion => {
commit(types.SET_LINE_DISCUSSIONS_FOR_FILE, { commit(types.SET_LINE_DISCUSSIONS_FOR_FILE, {
diffFile, discussion,
discussions,
diffPositionByLineCode, diffPositionByLineCode,
}); });
}); });
......
...@@ -90,11 +90,11 @@ export default { ...@@ -90,11 +90,11 @@ 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({
...@@ -103,36 +103,53 @@ export default { ...@@ -103,36 +103,53 @@ export default {
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) { if (file.highlightedDiffLines) {
Object.assign(line, { file.highlightedDiffLines = file.highlightedDiffLines.map(line => {
if (lineCheck(line)) {
return {
...line,
discussions: line.discussions.concat(discussion), discussions: line.discussions.concat(discussion),
}); };
} }
return line;
});
} }
if (diffFile.parallelDiffLines) { if (file.parallelDiffLines) {
const { left, right } = diffFile.parallelDiffLines.find( file.parallelDiffLines = file.parallelDiffLines.map(line => {
parallelLine => const left = line.left && lineCheck(line.left);
(parallelLine.left && lineCheck(parallelLine.left)) || const right = line.right && lineCheck(line.right);
(parallelLine.right && lineCheck(parallelLine.right)),
);
const line = left && left.lineCode === discussionLineCode ? left : right;
if (line) { if (left || right) {
Object.assign(line, { return {
discussions: line.discussions.concat(discussion), left: {
...line.left,
discussions: left ? line.left.discussions.concat(discussion) : [],
},
right: {
...line.right,
discussions: right ? line.right.discussions.concat(discussion) : [],
},
};
}
return line;
}); });
} }
if (!file.parallelDiffLines || !file.highlightedDiffLines) {
file.discussions = file.discussions.concat(discussion);
} }
if (!diffFile.parallelDiffLines || !diffFile.highlightedDiffLines) { return file;
Object.assign(diffFile, {
discussions: diffFile.discussions.concat(discussion),
});
} }
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