From 4328cc7029ad9040b200f85e23382cb5a703fdda Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lukas=20Mau=C3=9F?= <lukas.mauss@mni.thm.de>
Date: Thu, 21 Mar 2019 15:41:27 +0100
Subject: [PATCH] Add voting functions to components

---
 .../comment-list/comment-list.component.ts    | 44 +++++++++++--------
 .../shared/comment/comment.component.ts       |  8 ++++
 2 files changed, 34 insertions(+), 18 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 a5dd22ad4..801eacc3e 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -19,9 +19,9 @@ export class CommentListComponent implements OnInit {
   filteredComments: Comment[];
 
   constructor(private commentService: CommentService,
-    private translateService: TranslateService,
-    protected langService: LanguageService,
-    private rxStompService: RxStompService) {
+              private translateService: TranslateService,
+              protected langService: LanguageService,
+              private rxStompService: RxStompService) {
     langService.langEmitter.subscribe(lang => translateService.use(lang));
   }
 
@@ -51,28 +51,36 @@ export class CommentListComponent implements OnInit {
   parseIncomingMessage(message: Message) {
     const msg = JSON.parse(message.body);
     const payload = msg.payload;
-    if (msg.type === 'CommentCreated') {
-      const c = new Comment();
-      c.roomId = this.roomId;
-      c.body = payload.body;
-      c.id = payload.id;
-      c.creationTimestamp = payload.timestamp;
-      this.comments = this.comments.concat(c);
-    } else if (msg.type === 'CommentPatched') {
+    switch (msg.type) {
+      case 'CommentCreated':
+        const c = new Comment();
+        c.roomId = this.roomId;
+        c.body = payload.body;
+        c.id = payload.id;
+        c.creationTimestamp = payload.timestamp;
+        this.comments = this.comments.concat(c);
+        break;
+      case 'CommentPatched':
         for (let i = 0; i < this.comments.length; i++) {
           if (payload.id === this.comments[i].id) {
             for (const [key, value] of Object.entries(payload.changes)) {
               switch (key) {
-                case 'read':       this.comments[i].read = <boolean>value;
-                                   break;
-                case 'correct' :   this.comments[i].correct = <boolean>value;
-                                   break;
-                case 'favorite' :  this.comments[i].favorite = <boolean>value;
-                                   break;
+                case 'read':
+                  this.comments[i].read = <boolean>value;
+                  break;
+                case 'correct' :
+                  this.comments[i].correct = <boolean>value;
+                  break;
+                case 'favorite' :
+                  this.comments[i].favorite = <boolean>value;
+                  break;
+                case 'score' :
+                  this.comments[i].score = <number>value;
+                  break;
               }
             }
           }
-      }
+        }
     }
   }
 }
diff --git a/src/app/components/shared/comment/comment.component.ts b/src/app/components/shared/comment/comment.component.ts
index b6290b6d0..a689be35d 100644
--- a/src/app/components/shared/comment/comment.component.ts
+++ b/src/app/components/shared/comment/comment.component.ts
@@ -51,6 +51,14 @@ export class CommentComponent implements OnInit {
     this.comment = this.wsCommentService.toggleFavorite(comment);
   }
 
+  voteUp(comment: Comment): void {
+    this.wsCommentService.voteUp(comment);
+  }
+
+  voteDown(comment: Comment): void {
+    this.wsCommentService.voteDown(comment);
+  }
+
   delete(comment: Comment): void {
     this.commentService.deleteComment(comment.id).subscribe(room => {
       this.notification.show(`Comment '${comment.body}' successfully deleted.`);
-- 
GitLab