bstlboard-front/src/app/ui/entry/entry.ui.ts

216 lines
5.4 KiB
TypeScript

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 { FlagService } from '../../services/flag.service';
import { BookmarksService } from '../../services/bookmarks.service';
import { ModerationService } from '../../services/moderarion.service';
import { ConfirmDialog } from '../../ui/confirm/confirm.component';
import { EntriesService } from 'src/app/services/entries.service';
import { ActivatedRoute, Router } from '@angular/router';
@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() linkedTag: boolean = true;
@Input() text: boolean = false;
@Input() change: Function;
@Input() actions: boolean = true;
constructor(
private authService: AuthService,
private entriesService: EntriesService,
private voteService: VoteService,
private flagService: FlagService,
private moderationService: ModerationService,
private bookmarksService: BookmarksService,
private dialog: MatDialog,
private router: Router,
private route: ActivatedRoute) { }
ngOnInit(): void {
this.authService.auth.subscribe({
next: (auth: any) => {
if (auth && auth.authorities) {
for (let role of auth.authorities) {
if (role.authority == 'ROLE_ADMIN' || role.authority == 'ROLE_MOD') {
this.moderator = true;
}
}
}
}
})
}
applyTag(tag) {
if (this.linkedTag) {
this.router.routeReuseStrategy.shouldReuseRoute = () => false;
this.router.navigate(
[],
{
relativeTo: this.route,
queryParams: { 'tags': tag },
queryParamsHandling: 'merge'
});
}
}
voteUp() {
this.voteService.voteEntryUp(this.entry.id).subscribe({
next: () => {
this.change && this.change();
}
});
}
voteDown() {
this.voteService.voteEntryDown(this.entry.id).subscribe({
next: () => {
this.change && this.change();
}
});
}
addBookmark() {
this.bookmarksService.addEntry(this.entry.id).subscribe({
next: () => {
this.entry.metadata.bookmark = false;
this.entry.metadata.removeBookmark = true;
}
});
}
removeBookmark() {
this.bookmarksService.removeEntry(this.entry.id).subscribe({
next: () => {
this.entry.metadata.bookmark = true;
this.entry.metadata.removeBookmark = false;
}
});
}
unvote() {
this.voteService.unvoteEntry(this.entry.id).subscribe({
next: () => {
this.change && this.change();
}
});
}
flag() {
const dialogRef = this.dialog.open(ConfirmDialog, {
data: {
'label': 'entry.confirmFlag',
'args': [this.entry.title, this.entry.author]
}
})
dialogRef.afterClosed().subscribe({
next: (result) => {
if (result) {
this.flagService.flagEntry(this.entry.id).subscribe({
next: () => {
this.entry.metadata.flag = false;
this.entry.metadata.unflag = true;
}
});
}
}
});
}
unflag() {
const dialogRef = this.dialog.open(ConfirmDialog, {
data: {
'label': 'entry.confirmUnflag',
'args': [this.entry.title, this.entry.author]
}
})
dialogRef.afterClosed().subscribe({
next: (result) => {
if (result) {
this.flagService.unflagEntry(this.entry.id).subscribe({
next: () => {
this.entry.metadata.unflag = false;
this.entry.metadata.flag = true;
}
});
}
}
});
}
deleteEntry() {
const dialogRef = this.dialog.open(ConfirmDialog, {
data: {
'label': 'entry.confirmDelete',
'args': [this.entry.title, this.entry.author]
}
})
dialogRef.afterClosed().subscribe({
next: (result) => {
if (result) {
this.entriesService.delete(this.entry.id).subscribe({
next: () => {
this.entry = null;
this.change && this.change();
}
})
}
}
});
}
modDeleteEntry() {
const dialogRef = this.dialog.open(ConfirmDialog, {
data: {
'label': 'moderation.entry.confirmDelete',
'args': [this.entry.title, this.entry.author]
}
})
dialogRef.afterClosed().subscribe({
next: (result) => {
if (result) {
this.moderationService.deleteEntry(this.entry.id).subscribe({
next: () => {
this.entry = null;
this.change && this.change();
}
})
}
}
});
}
modUnflagEntry() {
const dialogRef = this.dialog.open(ConfirmDialog, {
data: {
'label': 'moderation.entry.confirmUnflag',
'args': [this.entry.title, this.entry.author]
}
})
dialogRef.afterClosed().subscribe({
next: (result) => {
if (result) {
this.moderationService.unflagEntry(this.entry.id).subscribe({
next: () => {
this.change && this.change();
}
})
}
}
});
}
}