Skip to content
Snippets Groups Projects
comment-list.component.ts 2.09 KiB
Newer Older
import { Component, 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';
  selector: 'app-comment-list',
  templateUrl: './comment-list.component.html',
  styleUrls: ['./comment-list.component.scss']
export class CommentListComponent implements OnInit {
  comments: Comment[];
  isLoading = true;
Lukas Mauß's avatar
Lukas Mauß committed
  roomId: string;
  hideCommentsList: boolean;
  filteredComments: Comment[];
  constructor(private commentService: CommentService,
    private translateService: TranslateService,
    protected langService: LanguageService) {
    private rxStompService: RxStompService) {
    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 {
    this.filteredComments = this.comments.filter(c => c.body.toLowerCase().includes(term));

  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') {
      console.log(msg);
    }