From 54ef51b1a6545cf70b924ce86bd7f5101ed2493e Mon Sep 17 00:00:00 2001
From: Tim Dahmen <tim.dahmen@mni.thm.de>
Date: Wed, 21 Oct 2020 16:42:52 +0200
Subject: [PATCH] Add support for tags with the new backend

---
 .../creator/_dialogs/tags/tags.component.html |  4 +--
 .../creator/_dialogs/tags/tags.component.ts   | 27 ++------------
 .../room-creator-page.component.ts            | 36 +++++++------------
 .../create-comment.component.ts               |  4 +--
 .../comment-list/comment-list.component.ts    |  7 ++--
 .../question-wall/question-wall.component.ts  |  4 +--
 src/app/models/room.ts                        |  7 ++--
 src/app/services/http/comment.service.ts      |  2 +-
 8 files changed, 29 insertions(+), 62 deletions(-)

diff --git a/src/app/components/creator/_dialogs/tags/tags.component.html b/src/app/components/creator/_dialogs/tags/tags.component.html
index a439d548a..bbf60db99 100644
--- a/src/app/components/creator/_dialogs/tags/tags.component.html
+++ b/src/app/components/creator/_dialogs/tags/tags.component.html
@@ -13,7 +13,7 @@
                aria-labelledby="tag-new"
                [formControl]="tagFormControl"
                name="taginput"
-               maxlength="30"/>
+               maxlength="20"/>
         <mat-placeholder class="placeholder">{{ 'room-page.tag-new' | translate }}</mat-placeholder>
         <mat-hint align="end"><span aria-hidden="true">{{tag.value.length}} / 30</span></mat-hint>
         <mat-error *ngIf="!tagFormControl.valid">
@@ -29,7 +29,7 @@
       </button>
       <span class="fill-remaining-space"></span>
     </div>
-    <div *ngIf="tagsEnabled === true && tags !== undefined && tags.length > 0">
+    <div *ngIf="tags.length > 0">
       <div
         fxLayout="row"
         *ngFor="let tag of tags"
diff --git a/src/app/components/creator/_dialogs/tags/tags.component.ts b/src/app/components/creator/_dialogs/tags/tags.component.ts
index dd5e91a4d..5c39e4559 100644
--- a/src/app/components/creator/_dialogs/tags/tags.component.ts
+++ b/src/app/components/creator/_dialogs/tags/tags.component.ts
@@ -12,12 +12,9 @@ import { FormControl, Validators } from '@angular/forms';
   templateUrl: './tags.component.html',
   styleUrls: ['./tags.component.scss']
 })
-export class TagsComponent implements OnInit {
-
-  extension: {};
+export class TagsComponent {
 
   tags: string[];
-  tagsEnabled: boolean;
 
   tagFormControl = new FormControl('', [Validators.minLength(3), Validators.maxLength(50)]);
   @ViewChild('tag') redel: ElementRef;
@@ -32,37 +29,19 @@ export class TagsComponent implements OnInit {
       langService.langEmitter.subscribe(lang => translationService.use(lang));
   }
 
-  ngOnInit() {
-    if (!this.extension) {
-      this.extension = {};
-      this.extension['enableTags'] = true;
-      this.tags = [];
-      this.tagsEnabled = true;
-    } else {
-      if (this.extension['tags']) {
-        this.tags = this.extension['tags'];
-      } else {
-        this.tags = [];
-      }
-      this.tagsEnabled = this.extension['enableTags'];
-    }
-  }
-
   addTag(tag: string) {
-    if (this.tagFormControl.valid) {
+    if (this.tagFormControl.valid && tag.length > 0) {
       this.tags.push(tag);
-      this.extension['tags'] = this.tags;
       this.redel.nativeElement.value = '';
     }
   }
 
   deleteTag(tag: string) {
     this.tags = this.tags.filter(o => o !== tag);
-    this.extension['tags'] = this.tags;
   }
 
   closeDialog(): void {
-    this.dialogRef.close(this.extension);
+    this.dialogRef.close(this.tags);
   }
 
 
diff --git a/src/app/components/creator/room-creator-page/room-creator-page.component.ts b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
index 2c0586a05..f47f4fab4 100644
--- a/src/app/components/creator/room-creator-page/room-creator-page.component.ts
+++ b/src/app/components/creator/room-creator-page/room-creator-page.component.ts
@@ -123,14 +123,7 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   }
 
   updateCommentSettings(settings: CommentSettingsDialog) {
-    // ToDo: FIX
-    /*const commentExtension: TSMap<string, any> = new TSMap();
-    commentExtension.set('enableThreshold', settings.enableThreshold);
-    commentExtension.set('commentThreshold', settings.threshold);
-    commentExtension.set('enableModeration', settings.enableModeration);
-    commentExtension.set('enableTags', settings.enableTags);
-    commentExtension.set('tags', settings.tags);
-    this.room.extensions['comments'] = commentExtension;
+    this.room.tags = settings.tags;
 
     if (this.moderationEnabled && !settings.enableModeration) {
       this.viewModuleCount = this.viewModuleCount - 1;
@@ -140,15 +133,13 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
 
     this.moderationEnabled = settings.enableModeration;
     localStorage.setItem('moderationEnabled', String(this.moderationEnabled));
-
-    this.updRoom.extensions['comments'] = commentExtension;*/
   }
 
   resetThreshold(): void {
-    // ToDo: FIX
-    /*if (this.room.extensions && this.room.extensions['comments']) {
-      delete this.room.extensions['comments'];
-    }*/
+    this.room.moderated = undefined;
+    this.room.threshold = undefined;
+    this.room.directSend = undefined;
+    this.room.tags = undefined;
   }
 
   saveChanges() {
@@ -224,25 +215,24 @@ export class RoomCreatorPageComponent extends RoomPageComponent implements OnIni
   }
 
   showTagsDialog(): void {
-    // ToDo: FIX
-    // this.updRoom = JSON.parse(JSON.stringify(this.room));
-    /*const dialogRef = this.dialog.open(TagsComponent, {
+    this.updRoom = JSON.parse(JSON.stringify(this.room));
+    const dialogRef = this.dialog.open(TagsComponent, {
       width: '400px'
     });
-    let tagExtension;
-    if (this.room.extensions !== undefined && this.room.extensions['tags'] !== undefined) {
-      tagExtension = JSON.parse(JSON.stringify(this.room.extensions['tags']));
+    let tags = [];
+    if (this.room.tags !== undefined) {
+      tags = this.room.tags;
     }
-    dialogRef.componentInstance.extension = tagExtension;
+    dialogRef.componentInstance.tags = tags;
     dialogRef.afterClosed()
       .subscribe(result => {
         if (!result || result === 'abort') {
           return;
         } else {
-          this.updRoom.extensions['tags'] = result;
+          this.updRoom.tags = result;
           this.saveChanges();
         }
-      });*/
+      });
   }
 
   copyShortId(): void {
diff --git a/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts b/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts
index 0c10780c9..5e14d05cb 100644
--- a/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts
+++ b/src/app/components/shared/_dialogs/create-comment/create-comment.component.ts
@@ -65,9 +65,7 @@ export class CreateCommentComponent implements OnInit {
       comment.body = body;
       comment.creatorId = this.user.id;
       comment.createdFromLecturer = this.user.role === 1;
-      if (this.selectedTag !== null) {
-        comment.tag = this.selectedTag;
-      }
+      comment.tag = this.selectedTag;
       this.dialogRef.close(comment);
     }
   }
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 07c995f4c..7ab27fe5f 100644
--- a/src/app/components/shared/comment-list/comment-list.component.ts
+++ b/src/app/components/shared/comment-list/comment-list.component.ts
@@ -295,10 +295,9 @@ export class CommentListComponent implements OnInit, OnDestroy {
     dialogRef.componentInstance.roomId = this.roomId;
     let tags;
     tags = [];
-    // ToDo: FIX
-    /*if (this.room.extensions && this.room.extensions['tags'] && this.room.extensions['tags'].tags) {
-      tags = this.room.extensions['tags'].tags;
-    }*/
+    if (this.room.tags) {
+      tags = this.room.tags;
+    }
     dialogRef.componentInstance.tags = tags;
     dialogRef.afterClosed()
       .subscribe(result => {
diff --git a/src/app/components/shared/questionwall/question-wall/question-wall.component.ts b/src/app/components/shared/questionwall/question-wall/question-wall.component.ts
index dc49394d7..3919c37bb 100644
--- a/src/app/components/shared/questionwall/question-wall/question-wall.component.ts
+++ b/src/app/components/shared/questionwall/question-wall/question-wall.component.ts
@@ -84,9 +84,7 @@ export class QuestionWallComponent implements OnInit, AfterViewInit, OnDestroy {
     });
     this.roomService.getRoom(this.roomId).subscribe(e => {
       this.room = e;
-      // ToDo: Fix
-      // this.tags = e.extensions['tags']['tags'];
-      this.tags = [];
+      this.tags = e.tags;
     });
     this.wsCommentService.getCommentStream(this.roomId).subscribe(e => {
       this.commentService.getComment(JSON.parse(e.body).payload.id).subscribe(comment => {
diff --git a/src/app/models/room.ts b/src/app/models/room.ts
index d345d0b64..f1af7606f 100644
--- a/src/app/models/room.ts
+++ b/src/app/models/room.ts
@@ -12,6 +12,7 @@ export class Room {
   moderated: boolean;
   directSend: boolean;
   threshold: number;
+  tags: string[];
 
   constructor(
     ownerId: string = '',
@@ -23,16 +24,18 @@ export class Room {
     moderated: boolean = true,
     directSend: boolean = true,
     threshold: number = null,
+    tags: string[] = []
   ) {
-    this.id = '',
+    this.id = '';
     this.ownerId = ownerId;
     this.shortId = shortId;
     this.abbreviation = abbreviation;
-    this.name = name,
+    this.name = name;
     this.description = description;
     this.closed = closed;
     this.moderated = moderated;
     this.directSend = directSend;
     this.threshold = threshold;
+    this.tags = tags;
   }
 }
diff --git a/src/app/services/http/comment.service.ts b/src/app/services/http/comment.service.ts
index a3ed74dcf..1d3f3a544 100644
--- a/src/app/services/http/comment.service.ts
+++ b/src/app/services/http/comment.service.ts
@@ -75,7 +75,7 @@ export class CommentService extends BaseHttpService {
     return this.http.post<Comment>(connectionUrl,
       {
         roomId: comment.roomId, body: comment.body,
-        read: comment.read, creationTimestamp: comment.timestamp
+        read: comment.read, creationTimestamp: comment.timestamp, tag: comment.tag
       }, httpOptions).pipe(
         tap(_ => ''),
         catchError(this.handleError<Comment>('addComment'))
-- 
GitLab