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 {
plainDiffPath: state => state.diffs.plainDiffPath,
emailPatchPath: state => state.diffs.emailPatchPath,
}),
...mapState('diffs', ['showTreeList']),
...mapState('diffs', ['showTreeList', 'isLoading']),
...mapGetters('diffs', ['isParallelView']),
...mapGetters(['isNotesFetched', 'getNoteableData']),
targetBranch() {
......@@ -152,7 +152,7 @@ export default {
}
},
setDiscussions() {
if (this.isNotesFetched && !this.assignedDiscussions) {
if (this.isNotesFetched && !this.assignedDiscussions && !this.isLoading) {
requestIdleCallback(
() =>
this.assignDiscussionsToDiff().then(() => {
......
......@@ -37,29 +37,13 @@ export const fetchDiffFiles = ({ state, commit }) => {
// once for parallel and once for inline mode
export const assignDiscussionsToDiff = (
{ commit, state, rootState },
discussionsToAssign = rootState.notes.discussions,
discussions = rootState.notes.discussions,
) => {
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, {
diffFile,
discussions,
discussion,
diffPositionByLineCode,
});
});
......
......@@ -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;
discussions.forEach(discussion => {
const discussionLineCode = discussion.line_code;
const lineCheck = ({ lineCode }) =>
lineCode === discussionLineCode &&
isDiscussionApplicableToLine({
discussion,
diffPosition: diffPositionByLineCode[lineCode],
latestDiff,
});
if (diffFile.highlightedDiffLines) {
const line = diffFile.highlightedDiffLines.find(lineCheck);
if (line) {
Object.assign(line, {
discussions: line.discussions.concat(discussion),
const discussionLineCode = discussion.line_code;
const fileHash = discussion.diff_file.file_hash;
const lineCheck = ({ lineCode }) =>
lineCode === discussionLineCode &&
isDiscussionApplicableToLine({
discussion,
diffPosition: diffPositionByLineCode[lineCode],
latestDiff,
});
state.diffFiles = state.diffFiles.map(diffFile => {
if (diffFile.fileHash === fileHash) {
const file = { ...diffFile };
if (file.highlightedDiffLines) {
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) {
Object.assign(line, {
discussions: line.discussions.concat(discussion),
if (file.parallelDiffLines) {
file.parallelDiffLines = file.parallelDiffLines.map(line => {
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) {
Object.assign(diffFile, {
discussions: diffFile.discussions.concat(discussion),
});
if (!file.parallelDiffLines || !file.highlightedDiffLines) {
file.discussions = file.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