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

147 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-01-06 22:16:15 +01:00
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core';
2022-02-24 22:31:08 +01:00
import { TableComponent } from '../../_components/table/table.component';
import { ModalAvailabilityScheduleComponent } from '../../_components/modal-availability-schedule/modal-availability-schedule.component';
2022-03-10 00:11:15 +01:00
import { ModalAlertComponent } from 'src/app/_components/modal-alert/modal-alert.component';
2021-12-24 15:21:22 +01:00
import { ApiClientService } from 'src/app/_services/api-client.service';
import { ToastrService } from 'ngx-toastr';
2022-03-11 22:04:47 +01:00
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';
2022-03-01 00:07:51 +01:00
import { TranslateService } from '@ngx-translate/core';
import { AuthService } from 'src/app/_services/auth.service';
2021-12-04 21:36:11 +01:00
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.scss']
})
2022-03-11 22:04:47 +01:00
export class ListComponent implements OnInit, OnDestroy {
2021-12-27 20:50:10 +01:00
scheduleModalRef?: BsModalRef;
2022-03-10 00:11:15 +01:00
alertModalRef?: BsModalRef;
2021-12-27 20:50:10 +01:00
@ViewChild('table') table!: TableComponent;
2021-12-04 21:36:11 +01:00
2022-01-06 22:16:15 +01:00
public loadAvailabilityInterval: NodeJS.Timer | undefined = undefined;
public available: boolean | undefined = undefined;
public manual_mode: boolean | undefined = undefined;
2022-03-14 00:13:45 +01:00
public alertLoading = false;
constructor(
2022-03-12 20:58:49 +01:00
public api: ApiClientService,
2022-03-11 23:38:30 +01:00
public auth: AuthService,
private toastr: ToastrService,
2022-03-01 00:07:51 +01:00
private modalService: BsModalService,
private translate: TranslateService
2022-01-06 22:16:15 +01:00
) {
this.loadAvailability();
}
loadAvailability() {
this.api.get("availability").then((response) => {
this.available = response.available;
this.manual_mode = response.manual_mode;
});
}
2021-12-24 15:21:22 +01:00
changeAvailibility(available: 0|1, id?: number|undefined) {
2022-02-14 11:40:47 +01:00
if(typeof id === 'undefined') {
id = this.auth.profile.auth_user_id;
}
2021-12-24 15:21:22 +01:00
this.api.post("availability", {
id: id,
available: available
2021-12-26 21:32:54 +01:00
}).then((response) => {
let changed_user_msg = parseInt(response.updated_user) === parseInt(this.auth.profile.auth_user_id) ? "La tua disponibilità" : `La disponibilità di ${response.updated_user_name}`;
let msg = available === 1 ? `${changed_user_msg} è stata impostata con successo.` : `${changed_user_msg} è stata rimossa con successo.`;
this.toastr.success(msg);
2022-01-06 22:16:15 +01:00
this.loadAvailability();
2021-12-26 21:32:54 +01:00
this.table.loadTableData();
2021-12-24 15:21:22 +01:00
});
}
2021-12-04 21:36:11 +01:00
2022-01-06 22:16:15 +01:00
updateManualMode(manual_mode: 0|1) {
this.api.post("manual_mode", {
manual_mode: manual_mode
}).then((response) => {
2022-03-01 00:07:51 +01:00
this.translate.get('list.manual_mode_updated_successfully').subscribe((res: string) => {
this.toastr.success(res);
});
2022-01-06 22:16:15 +01:00
this.loadAvailability();
});
}
2021-12-28 00:32:15 +01:00
openScheduleModal() {
this.scheduleModalRef = this.modalService.show(ModalAvailabilityScheduleComponent, Object.assign({}, { class: 'modal-custom' }));
2021-12-27 20:50:10 +01:00
}
2022-03-10 00:11:15 +01:00
addAlertFull() {
2022-03-14 00:13:45 +01:00
this.alertLoading = true;
2022-03-11 23:38:30 +01:00
if(!this.auth.profile.hasRole('SUPER_EDITOR')) return;
2022-03-11 22:04:47 +01:00
this.api.post("alerts", {
type: "full"
2022-03-10 00:11:15 +01:00
}).then((response) => {
2022-03-14 00:13:45 +01:00
this.alertLoading = false;
2022-03-12 20:58:49 +01:00
if(response?.status === "error") {
this.toastr.error(response.message, undefined, {
timeOut: 5000
});
return;
}
2022-03-10 00:11:15 +01:00
this.alertModalRef = this.modalService.show(ModalAlertComponent, {
initialState: {
id: response.id
}
});
2022-03-11 22:04:47 +01:00
this.api.alertsChanged.next();
2022-03-10 00:11:15 +01:00
});
}
addAlertSupport() {
2022-03-14 00:13:45 +01:00
this.alertLoading = true;
2022-03-11 23:38:30 +01:00
if(!this.auth.profile.hasRole('SUPER_EDITOR')) return;
2022-03-11 22:04:47 +01:00
this.api.post("alerts", {
type: "support"
2022-03-10 00:11:15 +01:00
}).then((response) => {
2022-03-14 00:13:45 +01:00
this.alertLoading = false;
2022-03-12 20:58:49 +01:00
if(response?.status === "error") {
this.toastr.error(response.message, undefined, {
timeOut: 5000
});
return;
}
2022-03-10 00:11:15 +01:00
this.alertModalRef = this.modalService.show(ModalAlertComponent, {
initialState: {
id: response.id
}
});
2022-03-11 22:04:47 +01:00
this.api.alertsChanged.next();
2022-03-10 00:11:15 +01:00
});
}
2021-12-04 21:36:11 +01:00
ngOnInit(): void {
2022-01-06 22:16:15 +01:00
this.loadAvailabilityInterval = setInterval(() => {
console.log("Refreshing availability...");
this.loadAvailability();
}, 10000);
2022-02-14 16:49:55 +01:00
this.auth.authChanged.subscribe({
next: () => this.loadAvailability()
});
2022-01-06 22:16:15 +01:00
}
ngOnDestroy(): void {
if(typeof this.loadAvailabilityInterval !== 'undefined') {
clearInterval(this.loadAvailabilityInterval);
}
2021-12-04 21:36:11 +01:00
}
2022-01-05 00:51:59 +01:00
requestTelegramToken() {
this.api.post("telegram_login_token", {}).then((response) => {
console.log(response);
2022-05-27 18:30:17 +02:00
const a = document.createElement("a");
a.setAttribute('href', response.start_link);
a.setAttribute('target', '_blank');
a.click();
2022-01-05 00:51:59 +01:00
});
}
2021-12-04 21:36:11 +01:00
}