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

123 lines
3.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';
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';
2022-03-01 00:07:51 +01:00
import { TranslateService } from '@ngx-translate/core';
2022-01-11 13:33:39 +01:00
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,
2022-03-01 00:07:51 +01:00
private toastr: ToastrService,
private translate: TranslateService
) { }
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-03-12 20:58:49 +01:00
if(!this.sourceType) this.sourceType = "list";
this.api.get(this.sourceType).then((data: any) => {
2021-12-22 23:06:58 +01:00
console.log(data);
2022-03-12 20:58:49 +01:00
this.data = data.filter((row: any) => typeof row.hidden !== 'undefined' ? !row.hidden : true);
if(this.sourceType === 'list') {
this.api.availableUsers = this.data.filter((row: any) => row.available).length;
}
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);
2022-03-01 00:07:51 +01:00
this.translate.get(['table.yes_remove', 'table.cancel', 'table.remove_service_confirm', 'table.remove_service_text']).subscribe((res: { [key: string]: string; }) => {
console.log(res);
Swal.fire({
title: res['table.remove_service_confirm'],
text: res['table.remove_service_confirm_text'],
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: res['table.yes_remove'],
cancelButtonText: res['table.cancel']
}).then((result) => {
if (result.isConfirmed) {
this.api.delete(`services/${id}`).then((response) => {
this.translate.get('table.service_deleted_successfully').subscribe((res: string) => {
2022-03-01 00:07:51 +01:00
this.toastr.success(res);
});
this.loadTableData();
}).catch((e) => {
this.translate.get('table.service_deleted_error').subscribe((res: string) => {
2022-03-01 00:07:51 +01:00
this.toastr.error(res);
});
});
}
});
});
2022-01-11 13:33:39 +01:00
}
2021-12-22 23:06:58 +01:00
}