Skip to content
Snippets Groups Projects
comment.component.ts 2.89 KiB
Newer Older
import { Component, Input, OnInit } from '@angular/core';
Louis Peter's avatar
Louis Peter committed
import { Comment } from '../../../models/comment';
import { AuthenticationService } from '../../../services/http/authentication.service';
import { ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { CommentService } from '../../../services/http/comment.service';
import { NotificationService } from '../../../services/util/notification.service';
import { TranslateService } from '@ngx-translate/core';
import { LanguageService } from '../../../services/util/language.service';
import { WsCommentServiceService } from '../../../services/websockets/ws-comment-service.service';
import { PresentCommentComponent } from '../_dialogs/present-comment/present-comment.component';
import { MatDialog } from '@angular/material';

@Component({
  selector: 'app-comment',
  templateUrl: './comment.component.html',
  styleUrls: ['./comment.component.scss']
})
export class CommentComponent implements OnInit {
Louis Peter's avatar
Louis Peter committed
  @Input() comment: Comment;
Lukas Mauß's avatar
Lukas Mauß committed
  isCreator = false;
  isLoading = true;

  constructor(protected authenticationService: AuthenticationService,
              private route: ActivatedRoute,
              private location: Location,
              private commentService: CommentService,
              private notification: NotificationService,
              private translateService: TranslateService,
              public dialog: MatDialog,
              protected langService: LanguageService,
              private wsCommentService: WsCommentServiceService) {
    langService.langEmitter.subscribe(lang => translateService.use(lang)); }

  ngOnInit() {
Lukas Mauß's avatar
Lukas Mauß committed
    if (this.authenticationService.getRole() === 0) {
      this.isCreator = true;
    }
    this.translateService.use(localStorage.getItem('currentLang'));
  }
  setRead(comment: Comment): void {
    this.comment = this.wsCommentService.toggleRead(comment);
  setCorrect(comment: Comment): void {
    this.comment = this.wsCommentService.toggleCorrect(comment);
  setFavorite(comment: Comment): void {
    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.`);

  openPresentDialog(body: string): void {
    const dialogRef = this.dialog.open(PresentCommentComponent, {
      position: {
        left: '10px',
        right: '10px'
      },
      maxWidth: '100vw',
      maxHeight: '100vh',
      height: '100%',
      width: '100%'
    });
    dialogRef.componentInstance.body = body;
    dialogRef.afterClosed()
      .subscribe(result => {
        if (result === 'close') {
          return;
        }
      });
  }