sacg-cronogramas/src/app/services/alert.service.ts

128 lines
3.4 KiB
TypeScript

import { Injectable, ComponentRef, createComponent, EnvironmentInjector, ApplicationRef } from '@angular/core';
import { AlertDialogComponent, AlertDialogOptions } from '../components/alert-dialog/alert-dialog.component';
@Injectable({
providedIn: 'root'
})
export class AlertService {
private alertRef: ComponentRef<AlertDialogComponent> | null = null;
constructor(
private injector: EnvironmentInjector,
private appRef: ApplicationRef
) {}
/**
* Muestra un mensaje de alerta tipo Sweet Alert
* @param options Opciones de configuración del alert
* @returns Una promesa que se resuelve con true (confirmar) o false (cancelar)
*/
show(options: Partial<AlertDialogOptions> = {}): Promise<boolean> {
return new Promise((resolve) => {
// Si ya existe un alert, lo eliminamos
this.closeCurrentAlert();
// Creamos el componente dinámicamente
this.alertRef = createComponent(AlertDialogComponent, {
environmentInjector: this.injector,
});
// Agregamos a la aplicación y al DOM
document.body.appendChild(this.alertRef.location.nativeElement);
this.appRef.attachView(this.alertRef.hostView);
// Configuramos las opciones y mostramos
this.alertRef.instance.show(options);
// Manejamos la respuesta
const subscription = this.alertRef.instance.onConfirm.subscribe((result) => {
subscription.unsubscribe();
this.closeCurrentAlert();
resolve(result);
});
});
}
/**
* Muestra un mensaje de éxito
* @param message Mensaje a mostrar
* @param title Título del alert (opcional)
*/
success(message: string, title: string = '¡Operación exitosa!'): Promise<boolean> {
return this.show({
title,
message,
icon: 'success',
showCancelButton: false
});
}
/**
* Muestra un mensaje de error
* @param message Mensaje a mostrar
* @param title Título del alert (opcional)
*/
error(message: string, title: string = 'Error'): Promise<boolean> {
return this.show({
title,
message,
icon: 'error',
showCancelButton: false
});
}
/**
* Muestra un mensaje de advertencia
* @param message Mensaje a mostrar
* @param title Título del alert (opcional)
*/
warning(message: string, title: string = 'Advertencia'): Promise<boolean> {
return this.show({
title,
message,
icon: 'warning',
showCancelButton: false
});
}
/**
* Muestra un mensaje informativo
* @param message Mensaje a mostrar
* @param title Título del alert (opcional)
*/
info(message: string, title: string = 'Información'): Promise<boolean> {
return this.show({
title,
message,
icon: 'info',
showCancelButton: false
});
}
/**
* Muestra un diálogo de confirmación
* @param message Mensaje a mostrar
* @param title Título del alert (opcional)
*/
confirm(message: string, title: string = '¿Está seguro?'): Promise<boolean> {
return this.show({
title,
message,
icon: 'question',
showCancelButton: true,
confirmButtonText: 'Sí, confirmar',
cancelButtonText: 'Cancelar'
});
}
/**
* Cierra el alert actual si existe
*/
private closeCurrentAlert(): void {
if (this.alertRef) {
this.appRef.detachView(this.alertRef.hostView);
this.alertRef.location.nativeElement.remove();
this.alertRef = null;
}
}
}