Skip to content
Snippets Groups Projects
comment-list.component.ts 3.78 KiB
Newer Older
import { Component, Input, OnInit } from '@angular/core';
import { Comment } from '../../../models/comment';
import { CommentService } from '../../../services/http/comment.service';
import { TranslateService } from '@ngx-translate/core';
import { LanguageService } from '../../../services/util/language.service';
import { RxStompService } from '@stomp/ng2-stompjs';
import { Message } from '@stomp/stompjs';
import { SubmitCommentComponent } from '../_dialogs/submit-comment/submit-comment.component';
import { MatDialog } from '@angular/material';
import { WsCommentServiceService } from '../../../services/websockets/ws-comment-service.service';
import { User } from '../../../models/user';
  selector: 'app-comment-list',
  templateUrl: './comment-list.component.html',
  styleUrls: ['./comment-list.component.scss']
export class CommentListComponent implements OnInit {
  @Input() user: User;
  @Input() roomId: string;
  comments: Comment[];
  isLoading = true;
  hideCommentsList: boolean;
  filteredComments: Comment[];
  constructor(private commentService: CommentService,
              private translateService: TranslateService,
              public dialog: MatDialog,
              protected langService: LanguageService,
              private rxStompService: RxStompService,
              private wsCommentService: WsCommentServiceService) {
    langService.langEmitter.subscribe(lang => translateService.use(lang));
    this.roomId = localStorage.getItem(`roomId`);
    this.comments = [];
    this.hideCommentsList = false;
    this.rxStompService.watch(`/topic/${this.roomId}.comment.stream`).subscribe((message: Message) => {
      this.parseIncomingMessage(message);
    });
Lukas Mauß's avatar
Lukas Mauß committed
    this.getComments();
    this.translateService.use(localStorage.getItem('currentLang'));
Lukas Mauß's avatar
Lukas Mauß committed
  getComments(): void {
    this.commentService.getComments(this.roomId)
      .subscribe(comments => {
        this.comments = comments;
        this.isLoading = false;
      });
  }

  searchComments(term: string): void {
Lukas Mauß's avatar
Lukas Mauß committed
    if (term) {
      this.hideCommentsList = true;
Lukas Mauß's avatar
Lukas Mauß committed
      this.filteredComments = this.comments.filter(c => c.body.toLowerCase().includes(term.toLowerCase()));
Lukas Mauß's avatar
Lukas Mauß committed
    } else {
      this.hideCommentsList = false;
    }

  parseIncomingMessage(message: Message) {
    const msg = JSON.parse(message.body);
    const payload = msg.payload;
    switch (msg.type) {
      case 'CommentCreated':
        const c = new Comment();
        c.roomId = this.roomId;
        c.body = payload.body;
        c.id = payload.id;
        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 'score' :
                  this.comments[i].score = <number>value;
                  break;

  openSubmitDialog(): void {
    const dialogRef = this.dialog.open(SubmitCommentComponent, {
      width: '400px'
    });
    dialogRef.componentInstance.user = this.user;
    dialogRef.componentInstance.roomId = this.roomId;
    dialogRef.afterClosed()
      .subscribe(result => {
        if (result) {
          this.send(result);
        } else {
          return;
        }
      });
  }

  send(comment: Comment): void {
    this.wsCommentService.add(comment);
  }