From 5e8606562df861e22d80e888457300f862aeb89b Mon Sep 17 00:00:00 2001
From: Phil Hughes <me@iamphill.com>
Date: Tue, 26 Jul 2016 14:30:57 +0100
Subject: [PATCH] Added ability to toggle resolving by commenting

---
 .../components/resolve_all_btn.js.es6         | 10 +---------
 .../diff_notes/services/resolve.js.es6        | 19 +++++++++++++++++++
 .../diff_notes/stores/comments.js.es6         | 13 +++++++++++--
 app/assets/javascripts/notes.js               | 12 ++++++++----
 .../merge_requests/_discussion.html.haml      |  2 +-
 app/views/projects/notes/_form.html.haml      |  1 +
 6 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/app/assets/javascripts/diff_notes/components/resolve_all_btn.js.es6 b/app/assets/javascripts/diff_notes/components/resolve_all_btn.js.es6
index 4c56386ad66..102f213cae5 100644
--- a/app/assets/javascripts/diff_notes/components/resolve_all_btn.js.es6
+++ b/app/assets/javascripts/diff_notes/components/resolve_all_btn.js.es6
@@ -40,15 +40,7 @@
     },
     methods: {
       resolve: function () {
-        let promise;
-
-        if (this.allResolved) {
-          promise = ResolveService
-            .unResolveAll(this.namespace, this.mergeRequestId, this.discussionId);
-        } else {
-          promise = ResolveService
-            .resolveAll(this.namespace, this.mergeRequestId, this.discussionId);
-        }
+        ResolveService.toggleResolveForDiscussion(this.namespace, this.mergeRequestId, this.discussionId);
       }
     }
   });
diff --git a/app/assets/javascripts/diff_notes/services/resolve.js.es6 b/app/assets/javascripts/diff_notes/services/resolve.js.es6
index 052f3c5f721..c5cfbfe2d91 100644
--- a/app/assets/javascripts/diff_notes/services/resolve.js.es6
+++ b/app/assets/javascripts/diff_notes/services/resolve.js.es6
@@ -23,6 +23,25 @@
       return this.noteResource.delete({ noteId }, {});
     }
 
+    toggleResolveForDiscussion(namespace, mergeRequestId, discussionId) {
+      const noteIds = CommentsStore.notesForDiscussion(discussionId);
+      let isResolved = true;
+
+      for (const noteId of noteIds) {
+        const resolved = CommentsStore.state[discussionId][noteId];
+
+        if (!resolved) {
+          isResolved = false;
+        }
+      }
+
+      if (isResolved) {
+        return this.unResolveAll(namespace, mergeRequestId, discussionId);
+      } else {
+        return this.resolveAll(namespace, mergeRequestId, discussionId);
+      }
+    }
+
     resolveAll(namespace, mergeRequestId, discussionId) {
       this.setCSRF();
       Vue.http.options.root = `/${namespace}`;
diff --git a/app/assets/javascripts/diff_notes/stores/comments.js.es6 b/app/assets/javascripts/diff_notes/stores/comments.js.es6
index cd981023944..5b3aa0382bc 100644
--- a/app/assets/javascripts/diff_notes/stores/comments.js.es6
+++ b/app/assets/javascripts/diff_notes/stores/comments.js.es6
@@ -25,13 +25,22 @@
       }
     },
     updateCommentsForDiscussion: function (discussionId, resolve) {
-      const noteIds = CommentsStore.notesForDiscussion(discussionId, resolve);
+      const noteIds = CommentsStore.resolvedNotesForDiscussion(discussionId, resolve);
 
       for (const noteId of noteIds) {
         CommentsStore.update(discussionId, noteId, resolve);
       }
     },
-    notesForDiscussion: function (discussionId, resolve) {
+    notesForDiscussion: function (discussionId) {
+      let ids = [];
+
+      for (const noteId in CommentsStore.state[discussionId]) {
+        ids.push(noteId);
+      }
+
+      return ids;
+    },
+    resolvedNotesForDiscussion: function (discussionId, resolve) {
       let ids = [];
 
       for (const noteId in CommentsStore.state[discussionId]) {
diff --git a/app/assets/javascripts/notes.js b/app/assets/javascripts/notes.js
index b5fc21e331b..b628e13eb5c 100644
--- a/app/assets/javascripts/notes.js
+++ b/app/assets/javascripts/notes.js
@@ -401,11 +401,14 @@
       this.removeDiscussionNoteForm($form);
 
       if ($form.attr('data-resolve-all') != null) {
-        var namespace = $form.attr('data-namespace'),
-            discussionId = $form.attr('data-discussion-id');
+        var namespacePath = $form.attr('data-namespace-path'),
+            projectPath = $form.attr('data-project-path')
+            discussionId = $form.attr('data-discussion-id'),
+            mergeRequestId = $('input[name="noteable_iid"]', $form).val(),
+            namespace = `${namespacePath}/${projectPath}`;
 
         if (ResolveService != null) {
-          ResolveService.resolveAll(namespace, discussionId, false)
+          ResolveService.toggleResolveForDiscussion(namespace, mergeRequestId, discussionId);
         }
       }
     };
@@ -771,7 +774,8 @@
         .closest('form')
         .attr('data-discussion-id', discussionId)
         .attr('data-resolve-all', 'true')
-        .attr('data-namespace', $this.attr('data-namespace'));
+        .attr('data-namespace-path', $this.attr('data-namespace-path'))
+        .attr('data-project-path', $this.attr('data-project-path'));
     };
 
     return Notes;
diff --git a/app/views/projects/merge_requests/_discussion.html.haml b/app/views/projects/merge_requests/_discussion.html.haml
index 768d4673680..35c787ca780 100644
--- a/app/views/projects/merge_requests/_discussion.html.haml
+++ b/app/views/projects/merge_requests/_discussion.html.haml
@@ -4,6 +4,6 @@
       = link_to 'Close merge request', merge_request_path(@merge_request, merge_request: {state_event: :close }), method: :put, class: "btn btn-nr btn-comment btn-close close-mr-link js-note-target-close", title: "Close merge request", data: {original_text: "Close merge request", alternative_text: "Comment & close merge request"}
     - if @merge_request.closed?
       = link_to 'Reopen merge request', merge_request_path(@merge_request, merge_request: {state_event: :reopen }), method: :put, class: "btn btn-nr btn-comment btn-reopen reopen-mr-link js-note-target-reopen", title: "Reopen merge request", data: {original_text: "Reopen merge request", alternative_text: "Comment & reopen merge request"}
-  = submit_tag 'Comment & resolve discussion', class: "btn btn-nr btn-create append-right-10 comment-btn js-comment-resolve-button", data: { namespace: "#{@merge_request.project.namespace.path}/#{@merge_request.project.path}" }
+  = submit_tag 'Comment & resolve discussion', class: "btn btn-nr btn-create append-right-10 comment-btn js-comment-resolve-button", data: { namespace_path: "#{@merge_request.project.namespace.path}", project_path: "#{@merge_request.project.path}" }
 
 #notes= render "projects/notes/notes_with_form"
diff --git a/app/views/projects/notes/_form.html.haml b/app/views/projects/notes/_form.html.haml
index 7c61ba750fe..a9f2cd873fe 100644
--- a/app/views/projects/notes/_form.html.haml
+++ b/app/views/projects/notes/_form.html.haml
@@ -5,6 +5,7 @@
   = f.hidden_field :commit_id
   = f.hidden_field :line_code
   = f.hidden_field :noteable_id
+  = hidden_field_tag :noteable_iid, @note.noteable.iid
   = f.hidden_field :noteable_type
   = f.hidden_field :type
   = f.hidden_field :position
-- 
2.30.9