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

113 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-12-30 11:58:25 +01:00
import { Component, OnInit, OnDestroy, Input, Output, EventEmitter } from '@angular/core';
import { Router } from '@angular/router';
2021-12-24 15:21:22 +01:00
import { ApiClientService } from 'src/app/_services/api-client.service';
2021-12-27 19:58:38 +01:00
import { AuthService } from '../../_services/auth.service';
2022-01-11 13:33:39 +01:00
import { ToastrService } from 'ngx-toastr';
import Swal from 'sweetalert2';
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}>();
2022-02-14 11:40:47 +01:00
@Output() userImpersonate: EventEmitter<number> = new EventEmitter<number>();
2021-12-24 15:21:22 +01:00
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
constructor(
2022-01-11 13:33:39 +01:00
private api: ApiClientService,
public auth: AuthService,
2022-01-11 13:33:39 +01:00
private router: Router,
private toastr: ToastrService
) { }
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() {
2022-01-11 13:33:39 +01:00
this.api.get(this.sourceType || "list").then((data: any) => {
2021-12-22 23:06:58 +01:00
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);
2022-02-14 16:49:55 +01:00
this.auth.authChanged.subscribe({
next: () => this.loadTableData()
});
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) {
2022-02-10 09:46:40 +01:00
if(this.auth.profile.hasRole('SUPER_EDITOR')) {
2021-12-27 19:58:38 +01:00
this.changeAvailability.emit({user, newState});
}
}
2022-02-14 11:40:47 +01:00
onUserImpersonate(user: number) {
if(this.auth.profile.hasRole('SUPER_ADMIN')) {
this.auth.impersonate(user).then((user_id) => {
this.loadTableData();
this.userImpersonate.emit(user_id);
});
}
}
openPlaceDetails(lat: number, lng: number) {
this.router.navigate(['/place-details', lat, lng]);
}
2022-01-11 13:33:39 +01:00
2022-02-24 23:41:46 +01:00
editService(id: number) {
this.router.navigate(['/services', id]);
}
2022-01-11 13:33:39 +01:00
deleteService(id: number) {
console.log(id);
Swal.fire({
title: 'Sei del tutto sicuro di voler rimuovere l\'intervento?',
text: "Gli interventi eliminati non si possono recuperare.",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Si, rimuovilo',
cancelButtonText: 'Annulla'
}).then((result) => {
if (result.isConfirmed) {
this.api.delete(`services/${id}`).then((response) => {
this.toastr.success('Intervento rimosso con successo.');
this.loadTableData();
}).catch((e) => {
this.toastr.error('Errore durante la rimozione dell\'intervento.');
});
}
})
}
2021-12-22 23:06:58 +01:00
}