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

88 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-12-04 21:36:11 +01:00
import { Component } from '@angular/core';
2021-12-24 15:21:22 +01:00
import { AuthService } from './_services/auth.service';
import { LocationBackService } from 'src/app/_services/locationBack.service';
import { GuardLoaderIconService } from 'src/app/_services/guard-loader-icon.service';
2022-01-03 20:17:17 +01:00
import { versions } from 'src/environments/versions';
2022-01-10 23:59:44 +01:00
import { Router, RouteConfigLoadStart, RouteConfigLoadEnd } from '@angular/router';
2022-03-11 22:04:47 +01:00
import { ApiClientService } from './_services/api-client.service';
import { ModalAlertComponent } from 'src/app/_components/modal-alert/modal-alert.component';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AuthorizeGuard } from './_guards/authorize.guard';
2021-12-04 21:36:11 +01:00
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
public menuButtonClicked = false;
2022-01-03 20:17:17 +01:00
public revision_datetime_string;
public versions = versions;
2022-01-10 23:59:44 +01:00
public loadingRoute = false;
2022-03-11 22:04:47 +01:00
private loadAlertsInterval: NodeJS.Timer | undefined = undefined;
public alerts = [];
private alertsEtag = "";
2021-12-24 15:21:22 +01:00
constructor(
public auth: AuthService,
private locationBackService: LocationBackService,
public guardLoaderIconService: GuardLoaderIconService,
2022-03-11 22:04:47 +01:00
private router: Router,
private api: ApiClientService,
private modalService: BsModalService,
public guard: AuthorizeGuard
) {
2022-01-03 20:17:17 +01:00
this.revision_datetime_string = new Date(versions.revision_timestamp).toLocaleString(undefined, { day: '2-digit', month: '2-digit', year: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric' });
this.locationBackService.initialize();
2022-01-03 20:17:17 +01:00
}
2022-01-10 23:59:44 +01:00
2022-03-11 22:04:47 +01:00
loadAlerts() {
2023-10-25 18:10:29 +02:00
if(this.auth.profile.id) {
this.api.get("alerts", {}, this.alertsEtag).then((response) => {
if(this.api.isLastSame) return;
2023-10-25 18:21:08 +02:00
this.alerts = response !== null ? response : [];
this.alertsEtag = this.api.lastEtag;
2023-12-31 15:26:48 +01:00
}).catch((err) => {
console.log(err);
2022-03-11 22:04:47 +01:00
});
}
}
2022-01-10 23:59:44 +01:00
ngOnInit () {
this.router.events.subscribe((event) => {
if (event instanceof RouteConfigLoadStart) {
this.loadingRoute = true;
} else if (event instanceof RouteConfigLoadEnd) {
this.loadingRoute = false;
}
});
2022-03-11 22:04:47 +01:00
2024-01-07 18:43:52 +01:00
if(this.auth.profile.can("alerts-read")) {
this.loadAlertsInterval = setInterval(() => {
console.log("Refreshing alerts...");
this.loadAlerts();
}, 30000);
2022-03-11 22:04:47 +01:00
this.loadAlerts();
2024-01-07 18:43:52 +01:00
this.api.alertsChanged.subscribe(() => {
this.loadAlerts();
});
}
2022-03-11 22:04:47 +01:00
}
openAlert(id: number) {
2024-01-07 18:43:52 +01:00
if(this.auth.profile.can("alerts-read")) {
this.modalService.show(ModalAlertComponent, {
initialState: {
id: id
}
});
}
}
logout(event: Event) {
event.preventDefault();
this.auth.logout();
2022-01-10 23:59:44 +01:00
}
2021-12-04 21:36:11 +01:00
}