108 lines
3.1 KiB
TypeScript
108 lines
3.1 KiB
TypeScript
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
|
|
import { ConsoleService } from '../console.service';
|
|
import { ApiClientService } from '../../api/index';
|
|
import { NoticePage } from '../../api/models/noticepage.model';
|
|
import { Notice } from '../../api/models/notice.model';
|
|
import { Observable } from 'rxjs/Observable';
|
|
import { Subject } from 'rxjs/Subject';
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
|
|
|
@Component({
|
|
selector: 'app-notices',
|
|
templateUrl: './notices.component.html',
|
|
styleUrls: ['./notices.component.css'],
|
|
encapsulation: ViewEncapsulation.None
|
|
})
|
|
export class NoticesComponent implements OnInit {
|
|
|
|
public loading: boolean = false;
|
|
public notices: Subject<Notice[]> = new Subject<Notice[]>();
|
|
public error: Subject<string> = new Subject<string>();
|
|
|
|
private page: NoticePage;
|
|
private offset: number = 0;
|
|
private size: number = 6;
|
|
|
|
constructor(private consoleService: ConsoleService, private apiClientService: ApiClientService) { }
|
|
|
|
ngOnInit() {
|
|
this.reloadPage();
|
|
}
|
|
|
|
getNoticeTitle(notice: Notice): string {
|
|
var message = notice.messages.find(msg => msg.language === "en");
|
|
if (message == null) return "";
|
|
else return message.title;
|
|
}
|
|
|
|
getNoticeBody(notice: Notice): string {
|
|
var message = notice.messages.find(msg => msg.language === "en");
|
|
if (message == null) return "";
|
|
else return message.body;
|
|
}
|
|
|
|
getNoticeTime(time: number): string {
|
|
var d = new Date(time);
|
|
return d.toLocaleString();
|
|
}
|
|
|
|
|
|
hasReload(): boolean {
|
|
return (!this.loading);
|
|
}
|
|
|
|
hasNext(): boolean {
|
|
if (this.page == null) return false;
|
|
else return this.offset + this.size < this.page.total;
|
|
}
|
|
|
|
hasPrevious(): boolean {
|
|
if (this.page == null) return false;
|
|
else return this.offset > 0;
|
|
}
|
|
|
|
nextPage(): void {
|
|
this.consoleService.log("Next page requested.")
|
|
this.offset = this.offset + this.size;
|
|
this.loadPage(this.offset, this.size);
|
|
}
|
|
|
|
previousPage(): void {
|
|
this.consoleService.log("Previous page requested.")
|
|
this.offset = Math.max(0, this.offset - this.size);
|
|
this.loadPage(this.offset, this.size);
|
|
}
|
|
|
|
reloadPage(): void {
|
|
this.consoleService.log("Reload page requested.")
|
|
this.loadPage(this.offset, this.size);
|
|
}
|
|
|
|
loadPage(offset: number, size: number): void {
|
|
this.consoleService.log("Notice page with offset " + offset + " and size " + size + " requested.");
|
|
this.loading = true;
|
|
this.page = null;
|
|
this.error.next(null);
|
|
this.notices.next([]);
|
|
this.apiClientService.getNotices(offset, size).subscribe(data => {
|
|
this.consoleService.log("Notice page received.");
|
|
this.loading = false;
|
|
this.page = data.body;
|
|
this.notices.next(data.body.elements);
|
|
}, error => {
|
|
this.loading = false;
|
|
if (error instanceof HttpErrorResponse) {
|
|
let message = error.status + ": " + error.statusText;
|
|
this.consoleService.log("HttpError: " + message)
|
|
this.error.next(message);
|
|
} else {
|
|
this.consoleService.log("Unknown error.");
|
|
console.dir(error); // print it to the console
|
|
}
|
|
this.notices.next([]);
|
|
});
|
|
|
|
}
|
|
|
|
}
|