From cc2ba87673129692ee649e86965ce43005b40ab8 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 10 Aug 2017 16:15:24 +0200
Subject: [PATCH] remove comment functionality

---
 frontend-js/src/main/js/ServerConnector.js    | 18 +++++++++++++++++
 .../main/js/gui/admin/CommentsAdminPanel.js   | 20 +++++++++++++++++--
 .../src/test/js/ServerConnector-test.js       |  6 ++++++
 .../comments/4290/DELETE_token=MOCK_TOKEN_ID& |  1 +
 4 files changed, 43 insertions(+), 2 deletions(-)
 create mode 100644 frontend-js/testFiles/apiCalls/projects/sample/comments/4290/DELETE_token=MOCK_TOKEN_ID&

diff --git a/frontend-js/src/main/js/ServerConnector.js b/frontend-js/src/main/js/ServerConnector.js
index b04e1ce807..9e6cc6126b 100644
--- a/frontend-js/src/main/js/ServerConnector.js
+++ b/frontend-js/src/main/js/ServerConnector.js
@@ -414,6 +414,12 @@ ServerConnector.deleteOverlayUrl = function(queryParams) {
   });
 };
 
+ServerConnector.deleteCommentUrl = function(queryParams) {
+  return this.getApiUrl({
+    url : this.getProjectUrl(queryParams) + "comments/" + queryParams.commentId + "/",
+  });
+};
+
 ServerConnector.getOverlaysUrl = function(queryParams, filterParams) {
   return this.getApiUrl({
     url : this.getProjectUrl(queryParams) + "overlays/",
@@ -1302,6 +1308,18 @@ ServerConnector.removeOverlay = function(params) {
   });
 };
 
+ServerConnector.removeComment = function(params) {
+  var self = this;
+  var queryParams = {
+    commentId : params.commentId
+  };
+  var filterParams = {};
+  return self.getProjectId(params.projectId).then(function(result) {
+    queryParams.projectId = result;
+    return self.sendDeleteRequest(self.deleteCommentUrl(queryParams), filterParams);
+  });
+};
+
 ServerConnector.getSuggestedQueryList = function(projectId) {
   var self = this;
   return self.getProjectId(projectId).then(function(result) {
diff --git a/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js b/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js
index 98d5ba3fc3..c4b80659b1 100644
--- a/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js
+++ b/frontend-js/src/main/js/gui/admin/CommentsAdminPanel.js
@@ -61,7 +61,7 @@ CommentsAdminPanel.prototype._createGui = function() {
       title : 'Removed',
     }, {
       title : 'Pinned',
-    }, ]
+    }, ],
   });
 
   var projectsCell = Functions.createElement({
@@ -140,6 +140,15 @@ CommentsAdminPanel.prototype.onProjectChange = function() {
       data.push(self.commentToTableRow(comments[i]));
     }
     dataTable.clear().rows.add(data).draw();
+    $("[name='commentsTable']", self.getElement()).on("click", "[name='removeComment']", function() {
+      var button = this;
+      return ServerConnector.removeComment({
+        commentId : $(button).attr("data")
+      }).then(function() {
+        $(button).after("<span>YES</span>");
+        button.style.display = "none";
+      });
+    });
   });
 };
 
@@ -165,12 +174,19 @@ CommentsAdminPanel.prototype.commentToTableRow = function(comment) {
     title = comment.getTitle();
   }
 
+  var remove = null;
+  if (comment.isRemoved()) {
+    remove = "YES";
+  } else {
+    remove = "<button name='removeComment' data='" + comment.getId() + "'>REMOVE</button>";
+  }
+
   var row = [ comment.getId(), // 
   title, //
   comment.getAuthor(), // 
   comment.getEmail(), // 
   comment.getContent(), // 
-  toYesNo(comment.isRemoved()), //
+  remove, //
   toYesNo(comment.isPinned()) ];
   return row;
 }
diff --git a/frontend-js/src/test/js/ServerConnector-test.js b/frontend-js/src/test/js/ServerConnector-test.js
index 730bca8fa4..baefb69ed8 100644
--- a/frontend-js/src/test/js/ServerConnector-test.js
+++ b/frontend-js/src/test/js/ServerConnector-test.js
@@ -160,6 +160,12 @@ describe('ServerConnector', function() {
     });
   });
 
+  it('removeComment', function() {
+    return ServerConnector.removeComment({
+      commentId : 4290,
+    });
+  });
+
   it('updateOverlay', function() {
     return ServerConnector.updateOverlay({
       overlayId : 17296,
diff --git a/frontend-js/testFiles/apiCalls/projects/sample/comments/4290/DELETE_token=MOCK_TOKEN_ID& b/frontend-js/testFiles/apiCalls/projects/sample/comments/4290/DELETE_token=MOCK_TOKEN_ID&
new file mode 100644
index 0000000000..9e26dfeeb6
--- /dev/null
+++ b/frontend-js/testFiles/apiCalls/projects/sample/comments/4290/DELETE_token=MOCK_TOKEN_ID&
@@ -0,0 +1 @@
+{}
\ No newline at end of file
-- 
GitLab