Commit ee62a2fd authored by Phil Hughes's avatar Phil Hughes

Moved update headline HTML into discussion model

parent d6e724d9
...@@ -25,6 +25,9 @@ ...@@ -25,6 +25,9 @@
} }
}, },
computed: { computed: {
discussion: function () {
return this.discussions[this.discussionId];
},
note: function () { note: function () {
return CommentsStore.get(this.discussionId, this.noteId); return CommentsStore.get(this.discussionId, this.noteId);
}, },
...@@ -63,14 +66,14 @@ ...@@ -63,14 +66,14 @@
} }
promise.then((response) => { promise.then((response) => {
const data = response.json();
this.loading = false; this.loading = false;
if (response.status === 200) { if (response.status === 200) {
const data = response.json();
const resolved_by = data ? data.resolved_by : null; const resolved_by = data ? data.resolved_by : null;
CommentsStore.update(this.discussionId, this.noteId, !this.isResolved, resolved_by); CommentsStore.update(this.discussionId, this.noteId, !this.isResolved, resolved_by);
ResolveService.updateDiscussionHeadline(this.discussionId, data); this.discussion.updateHeadline(data);
} else { } else {
new Flash('An error occurred when trying to resolve a comment. Please try again.', 'alert'); new Flash('An error occurred when trying to resolve a comment. Please try again.', 'alert');
} }
......
...@@ -53,4 +53,18 @@ class DiscussionModel { ...@@ -53,4 +53,18 @@ class DiscussionModel {
} }
} }
} }
updateHeadline (data) {
const $discussionHeadline = $(`.discussion[data-discussion-id="${this.id}"] .js-discussion-headline`);
if (data.discussion_headline_html) {
if ($discussionHeadline.length) {
$discussionHeadline.replaceWith(data.discussion_headline_html);
} else {
$(`.discussion[data-discussion-id="${this.id}"] .discussion-header`).append(data.discussion_headline_html);
}
} else {
$discussionHeadline.remove();
}
}
} }
...@@ -28,13 +28,34 @@ ...@@ -28,13 +28,34 @@
} }
toggleResolveForDiscussion(namespace, mergeRequestId, discussionId) { toggleResolveForDiscussion(namespace, mergeRequestId, discussionId) {
const isResolved = CommentsStore.state[discussionId].isResolved(); const discussion = CommentsStore.state[discussionId],
isResolved = discussion.isResolved();
let promise;
if (isResolved) { if (isResolved) {
return this.unResolveAll(namespace, mergeRequestId, discussionId); promise = this.unResolveAll(namespace, mergeRequestId, discussionId);
} else { } else {
return this.resolveAll(namespace, mergeRequestId, discussionId); promise = this.resolveAll(namespace, mergeRequestId, discussionId);
} }
promise.then((response) => {
discussion.loading = false;
if (response.status === 200) {
const data = response.json();
const resolved_by = data ? data.resolved_by : null;
if (isResolved) {
discussion.unResolveAllNotes();
} else {
discussion.resolveAllNotes(resolved_by);
}
discussion.updateHeadline(data);
} else {
new Flash('An error occurred when trying to resolve a discussion. Please try again.', 'alert');
}
})
} }
resolveAll(namespace, mergeRequestId, discussionId) { resolveAll(namespace, mergeRequestId, discussionId) {
...@@ -51,18 +72,7 @@ ...@@ -51,18 +72,7 @@
return this.discussionResource.save({ return this.discussionResource.save({
mergeRequestId, mergeRequestId,
discussionId discussionId
}, {}).then((response) => { }, {});
if (response.status === 200) {
const data = response.json();
const resolved_by = data ? data.resolved_by : null;
discussion.resolveAllNotes(resolved_by);
discussion.loading = false;
this.updateDiscussionHeadline(discussionId, data);
} else {
new Flash('An error occurred when trying to resolve a discussion. Please try again.', 'alert');
}
});
} }
unResolveAll(namespace, mergeRequestId, discussionId) { unResolveAll(namespace, mergeRequestId, discussionId) {
...@@ -76,31 +86,7 @@ ...@@ -76,31 +86,7 @@
return this.discussionResource.delete({ return this.discussionResource.delete({
mergeRequestId, mergeRequestId,
discussionId discussionId
}, {}).then((response) => { }, {});
if (response.status === 200) {
const data = response.json();
discussion.unResolveAllNotes();
discussion.loading = false;
this.updateDiscussionHeadline(discussionId, data);
} else {
new Flash('An error occurred when trying to unresolve a discussion. Please try again.', 'alert');
}
});
}
updateDiscussionHeadline(discussionId, data) {
const $discussionHeadline = $(`.discussion[data-discussion-id="${discussionId}"] .js-discussion-headline`);
if (data.discussion_headline_html) {
if ($discussionHeadline.length) {
$discussionHeadline.replaceWith(data.discussion_headline_html);
} else {
$(`.discussion[data-discussion-id="${discussionId}"] .discussion-header`).append(data.discussion_headline_html);
}
} else {
$discussionHeadline.remove();
}
} }
} }
......
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