import { Component, OnInit, Input } from '@angular/core'; import { MatDialog } from '@angular/material/dialog'; import { AuthService } from '../../services/auth.service'; import { VoteService } from '../../services/vote.service'; import { BookmarksService } from '../../services/bookmarks.service'; import { ModerationService } from '../../services/moderarion.service'; import { ConfirmDialog } from '../../ui/confirm/confirm.component'; @Component({ selector: 'ui-entry', templateUrl: './entry.ui.html', styleUrls: [ './entry.ui.scss' ] }) export class UiEntry implements OnInit { moderator: boolean = false; @Input() entry: any; @Input() index: number; @Input() change: Function; constructor(private authService: AuthService, private voteService: VoteService, private moderationService: ModerationService, private bookmarksService: BookmarksService, public dialog: MatDialog) { } ngOnInit(): void { this.authService.auth.subscribe((auth: any) => { if (auth && auth.authorities) { for (let role of auth.authorities) { if (role.authority == 'ROLE_ADMIN' || role.authority == 'ROLE_MOD') { this.moderator = true; } } } }) } voteUp() { this.voteService.voteEntryUp(this.entry.id).subscribe((result) => { this.change && this.change(); }); } voteDown() { this.voteService.voteEntryDown(this.entry.id).subscribe((result) => { this.change && this.change(); }); } addBookmark() { this.bookmarksService.addEntry(this.entry.id).subscribe((result) => { this.entry.metadata.bookmarked = true; }); } removeBookmark() { this.bookmarksService.removeEntry(this.entry.id).subscribe((result) => { this.entry.metadata.bookmarked = false; }); } unvote() { this.voteService.unvoteEntry(this.entry.id).subscribe((result) => { this.change && this.change(); }); } deleteEntry(entry: any) { const dialogRef = this.dialog.open(ConfirmDialog, { data: { 'label': 'moderation.entry.confirmDelete', 'args': [ entry.title, entry.author ] } }) dialogRef.afterClosed().subscribe(result => { if (result) { this.moderationService.deleteEntry(entry.id).subscribe((result: any) => { this.entry = null; this.change && this.change(); }) } }); } }