allerta-vvf/frontend/src/app/_components/table/table.component.ts

62 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-12-30 11:58:25 +01:00
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
2021-12-24 15:21:22 +01:00
import { TableType } from 'src/app/_models/TableType';
import { ApiClientService } from 'src/app/_services/api-client.service';
2021-12-27 19:58:38 +01:00
import { AuthService } from '../../_services/auth.service';
2021-12-22 23:06:58 +01:00
@Component({
selector: 'app-table',
templateUrl: './table.component.html',
styleUrls: ['./table.component.scss']
})
2021-12-30 11:58:25 +01:00
export class TableComponent implements OnInit, OnDestroy {
2021-12-22 23:06:58 +01:00
@Input() sourceType?: string;
2021-12-29 13:12:16 +01:00
@Input() refreshInterval?: number;
2021-12-22 23:06:58 +01:00
2021-12-24 15:21:22 +01:00
@Output() changeAvailability: EventEmitter<{user: number, newState: 0|1}> = new EventEmitter<{user: number, newState: 0|1}>();
2021-12-22 23:06:58 +01:00
public data: any = [];
2021-12-30 11:58:25 +01:00
public loadDataInterval: NodeJS.Timer | undefined = undefined;
2021-12-29 13:12:16 +01:00
2021-12-27 19:58:38 +01:00
constructor(public apiClient: ApiClientService, public auth: AuthService) {}
2021-12-22 23:06:58 +01:00
2021-12-27 14:43:01 +01:00
getTime() {
return Math.floor(Date.now() / 1000);
}
2021-12-24 15:21:22 +01:00
loadTableData() {
2021-12-22 23:06:58 +01:00
this.apiClient.get(this.sourceType || "list").then((data: any) => {
console.log(data);
2021-12-27 13:22:32 +01:00
this.data = data.filter((row: any) => {
if(typeof row.hidden !== 'undefined') return !row.hidden;
return true;
});
2021-12-22 23:06:58 +01:00
});
}
2021-12-24 15:21:22 +01:00
ngOnInit(): void {
console.log(this.sourceType);
this.loadTableData();
2021-12-29 13:12:16 +01:00
this.loadDataInterval = setInterval(() => {
2022-01-10 12:18:55 +01:00
if(typeof (window as any).skipTableReload !== 'undefined' && (window as any).skipTableReload) {
return;
}
2021-12-29 13:12:16 +01:00
console.log("Refreshing data...");
this.loadTableData();
}, this.refreshInterval || 10000);
2021-12-24 15:21:22 +01:00
}
2021-12-30 11:58:25 +01:00
ngOnDestroy(): void {
if(typeof this.loadDataInterval !== 'undefined') {
clearInterval(this.loadDataInterval);
}
}
2021-12-27 19:58:38 +01:00
onChangeAvailability(user: number, newState: 0|1) {
2021-12-29 15:38:16 +01:00
if(this.auth.profile.full_viewer) {
2021-12-27 19:58:38 +01:00
this.changeAvailability.emit({user, newState});
}
}
2021-12-22 23:06:58 +01:00
}