bstlboard-front/src/app/pages/view/view.page.ts

76 lines
2.1 KiB
TypeScript

import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { Subscription } from 'rxjs';
import { ViewService } from 'src/app/services/view.service';
import { EntriesService } from '../../services/entries.service';
import { PageEntries } from '../entries/entries.page';
@Component({
selector: 'page-view',
templateUrl: './view.page.html',
styleUrls: ['./view.page.scss']
})
export class PageView implements OnInit, OnDestroy {
boundFetch: Function;
name: string;
username: string;
paramsSub: Subscription;
@ViewChild('entries') entries: PageEntries;
constructor(
private entriesService: EntriesService,
private viewService: ViewService,
private router: Router,
private route: ActivatedRoute) {
}
ngOnInit(): void {
this.paramsSub = this.route.params.subscribe({
next: (params: Params) => {
this.name = params['name'];
this.username = params['username'];
if (!this.name) {
this.viewService.getViews().then((data: any) => {
const views = data.content;
if (!views || views.length == 0) {
this.router.navigate(['/v'], { queryParams: { name: this.name }, queryParamsHandling: 'merge' });
} else {
this.router.navigateByUrl('/v/' + views[0].name);
}
})
} else {
this.viewService.getView(this.name, this.username).subscribe({
next: (data) => {
if (this.entries) {
this.entries.refresh();
}
},
error: (error) => {
this.router.navigate(['/v'], { queryParams: { name: this.name }, queryParamsHandling: 'merge' });
}
})
}
}
});
this.boundFetch = this.fetch.bind(this);
}
ngOnDestroy(): void {
this.paramsSub.unsubscribe();
}
fetch(page: number, size: number, asc: boolean, filter: any) {
if (this.username) {
filter = filter || {};
filter.username = this.username;
}
return this.entriesService.getByView(this.name, page, size, asc, filter);
}
}