From 2320fdec952eb5ef1924b14566b680c3dd50fd5e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=20K=C3=A4sler?= <tom.kaesler@mni.thm.de>
Date: Wed, 20 Mar 2019 21:17:40 +0100
Subject: [PATCH] Fix comment patching

listen to CommentPatched events from WS backend
---
 .../comment-list/comment-list.component.ts    | 21 ++++++++++++++-
 src/app/models/messages/patch-comment.ts      | 10 ++++---
 .../websockets/ws-comment-service.service.ts  | 27 ++++++++++---------
 3 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/src/app/components/shared/comment-list/comment-list.component.ts b/src/app/components/shared/comment-list/comment-list.component.ts
index 79d57dec0..532819bae 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -59,7 +59,26 @@ export class CommentListComponent implements OnInit {
       c.creationTimestamp = payload.timestamp;
       this.comments = this.comments.concat(c);
     } else if (msg.type === 'CommentPatched') {
-      console.log(msg);
+      const c = this.comments.find((comment: Comment) => comment.id === payload.id);
+      if (c) {
+        const index = this.comments.indexOf(c);
+        console.log(index);
+        const newList = this.comments.slice(0);
+        const changes = payload.changes;
+        // ToDo: there must be a better way to update the model
+        for (const change of changes) {
+          console.log(change);
+          if (change.read) {
+            c.read = change.read;
+          } else if (change.favorite) {
+            c.favorite = change.favorite;
+          } else if (change.correct) {
+            c.correct = change.correct;
+          }
+        }
+        newList[index] = c;
+        this.comments = newList;
+      }
     }
   }
 }
diff --git a/src/app/models/messages/patch-comment.ts b/src/app/models/messages/patch-comment.ts
index 2c76e2ab1..faec58f4e 100644
--- a/src/app/models/messages/patch-comment.ts
+++ b/src/app/models/messages/patch-comment.ts
@@ -1,14 +1,16 @@
+import { TSMap } from 'typescript-map';
+
 export class PatchComment {
   type: string;
   payload: {
-      commentId;
-      changes: Map<string, any>;
+      id: string;
+      changes: TSMap<string, any>;
   };
 
-  constructor(commentId: string, changes: Map<string, any>) {
+  constructor(id: string, changes: TSMap<string, any>) {
       this.type = 'PatchComment';
       this.payload = {
-        commentId: commentId,
+        id: id,
         changes: changes
       };
   }
diff --git a/src/app/services/websockets/ws-comment-service.service.ts b/src/app/services/websockets/ws-comment-service.service.ts
index 1e35adfdf..5e8516afe 100644
--- a/src/app/services/websockets/ws-comment-service.service.ts
+++ b/src/app/services/websockets/ws-comment-service.service.ts
@@ -3,6 +3,7 @@ import { Comment } from '../../models/comment';
 import { RxStompService } from '@stomp/ng2-stompjs';
 import { CreateComment } from '../../models/messages/create-comment';
 import { PatchComment } from '../../models/messages/patch-comment';
+import { TSMap } from 'typescript-map';
 
 
 @Injectable({
@@ -15,7 +16,7 @@ export class WsCommentServiceService {
   add(comment: Comment): void {
     const message = new CreateComment(comment.roomId, comment.userId, comment.body);
     this.rxStompService.publish({
-      destination: `/queue/comment.command`,
+      destination: `/queue/comment.command.create`,
       body: JSON.stringify(message),
       headers: {
         'content-type': 'application/json'
@@ -24,33 +25,35 @@ export class WsCommentServiceService {
   }
 
   toggleRead(comment: Comment): Comment {
+    console.log(comment);
     comment.read = !comment.read;
-    const payload = new Map<string, any>();
-    payload.set('mark', comment.read);
-    this.patchComment(comment, payload);
+    const changes = new TSMap<string, any>();
+    changes.set('read', comment.read);
+    this.patchComment(comment, changes);
     return comment;
   }
 
   toggleFavorite(comment: Comment): Comment {
     comment.favorite = !comment.favorite;
-    const payload = new Map<string, any>();
-    payload.set('correct', comment.read);
-    this.patchComment(comment, payload);
+    const changes = new TSMap<string, any>();
+    changes.set('favorite', comment.favorite);
+    this.patchComment(comment, changes);
     return comment;
   }
 
   toggleCorrect(comment: Comment): Comment {
     comment.correct = !comment.correct;
-    const payload = new Map<string, any>();
-    payload.set('correct', comment.read);
-    this.patchComment(comment, payload);
+    const changes = new TSMap<string, any>();
+    changes.set('correct', comment.correct);
+    this.patchComment(comment, changes);
     return comment;
   }
 
-  private patchComment(comment: Comment, changes: Map<string, any>): void {
+  private patchComment(comment: Comment, changes: TSMap<string, any>): void {
     const message = new PatchComment(comment.id, changes);
+    console.log(message);
       this.rxStompService.publish({
-        destination: `/queue/comment.command`,
+        destination: `/queue/comment.command.patch`,
         body: JSON.stringify(message),
         headers: {
           'content-type': 'application/json'
-- 
GitLab