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

95 lines
2.6 KiB
TypeScript

import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import { CommentService } from '../../services/comment.service';
import { SettingsService } from '../../services/settings.service';
@Component({
selector: 'ui-comments',
templateUrl: './comments.ui.html',
styleUrls: ['./comments.ui.scss']
})
export class UiComments implements OnInit, OnDestroy {
settings: any;
comments: any;
@Input() target: number;
@Input() parent: number;
@Input() ignore: string[] = [];
@Input() subcomments: boolean = false;
@Input() parentLink: boolean = false;
boundRefresh: Function;
settingsSubscription: Subscription;
constructor(private settingsService: SettingsService, private commentService: CommentService) { }
ngOnInit(): void {
this.boundRefresh = this.refresh.bind(this);
this.settingsSubscription = this.settingsService.settings.subscribe({
next: (settings) => {
this.settings = settings;
this.refresh();
}
})
}
ngOnDestroy(): void {
this.settingsSubscription.unsubscribe;
}
refresh(): void {
if (this.parent) {
this.commentService.getNewByParent(this.target, this.parent, 0, this.settings.pageSize, this.ignore).subscribe({
next: (data) => {
this.comments = data;
}
})
} else {
this.commentService.getNew(this.target, 0, this.settings.pageSize, this.ignore).subscribe({
next: (data) => {
this.comments = data;
}
})
}
}
addComment(comment: any): void {
if (!this.comments) {
this.comments = { "content": [] };
}
if (!this.comments.content) {
this.comments.content = [];
}
this.comments.content.push(comment);
this.comments.totalElements = (this.comments.totalElements || 0) + 1;
}
showMore() {
const oldContent: any[] = this.comments.content;
if (this.parent) {
this.commentService.getNewByParent(this.target, this.parent, this.comments.number + 1, this.comments.size, this.ignore).subscribe({
next: (data) => {
this.comments = data;
for (let comment of this.comments.content) {
oldContent.push(comment);
}
this.comments.content = oldContent;
}
})
} else {
this.commentService.getNew(this.target, this.comments.number + 1, this.comments.size, this.ignore).subscribe({
next: (data) => {
this.comments = data;
for (let comment of this.comments.content) {
oldContent.push(comment);
}
this.comments.content = oldContent;
}
})
}
}
}