From ee62a2fd5ac44d67fd60e75989f03ea653aa2c54 Mon Sep 17 00:00:00 2001 From: Phil Hughes Date: Mon, 1 Aug 2016 11:31:35 +0100 Subject: Moved update headline HTML into discussion model --- .../diff_notes/components/resolve_btn.js.es6 | 7 ++- .../diff_notes/models/discussion.js.es6 | 14 +++++ .../javascripts/diff_notes/services/resolve.js.es6 | 66 +++++++++------------- 3 files changed, 45 insertions(+), 42 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/diff_notes/components/resolve_btn.js.es6 b/app/assets/javascripts/diff_notes/components/resolve_btn.js.es6 index b2542e6664d..e1611928049 100644 --- a/app/assets/javascripts/diff_notes/components/resolve_btn.js.es6 +++ b/app/assets/javascripts/diff_notes/components/resolve_btn.js.es6 @@ -25,6 +25,9 @@ } }, computed: { + discussion: function () { + return this.discussions[this.discussionId]; + }, note: function () { return CommentsStore.get(this.discussionId, this.noteId); }, @@ -63,14 +66,14 @@ } promise.then((response) => { - const data = response.json(); this.loading = false; if (response.status === 200) { + const data = response.json(); const resolved_by = data ? data.resolved_by : null; CommentsStore.update(this.discussionId, this.noteId, !this.isResolved, resolved_by); - ResolveService.updateDiscussionHeadline(this.discussionId, data); + this.discussion.updateHeadline(data); } else { new Flash('An error occurred when trying to resolve a comment. Please try again.', 'alert'); } diff --git a/app/assets/javascripts/diff_notes/models/discussion.js.es6 b/app/assets/javascripts/diff_notes/models/discussion.js.es6 index 8f0d77a62f5..615e8d12e5e 100644 --- a/app/assets/javascripts/diff_notes/models/discussion.js.es6 +++ b/app/assets/javascripts/diff_notes/models/discussion.js.es6 @@ -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(); + } + } } diff --git a/app/assets/javascripts/diff_notes/services/resolve.js.es6 b/app/assets/javascripts/diff_notes/services/resolve.js.es6 index fe07ab39d5e..b9fe6caf7e8 100644 --- a/app/assets/javascripts/diff_notes/services/resolve.js.es6 +++ b/app/assets/javascripts/diff_notes/services/resolve.js.es6 @@ -28,13 +28,34 @@ } toggleResolveForDiscussion(namespace, mergeRequestId, discussionId) { - const isResolved = CommentsStore.state[discussionId].isResolved(); + const discussion = CommentsStore.state[discussionId], + isResolved = discussion.isResolved(); + let promise; if (isResolved) { - return this.unResolveAll(namespace, mergeRequestId, discussionId); + promise = this.unResolveAll(namespace, mergeRequestId, discussionId); } 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) { @@ -51,18 +72,7 @@ return this.discussionResource.save({ mergeRequestId, 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) { @@ -76,31 +86,7 @@ return this.discussionResource.delete({ mergeRequestId, 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(); - } + }, {}); } } -- cgit v1.2.1