Files
WebApp/src/app/dashboard/export-results-dialog.ts
T
Angel Capetillo Zavala 329ee2a61d Feature: Exportar Excel
2026-09-25 11:30:40 -06:00

49 lines
1.7 KiB
TypeScript

import { Component, inject, InjectionToken, signal } from '@angular/core';
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import type { AnalysisResult } from './dashboard.mock';
import { exportExcel } from './exports/excel-export';
export type ExportFormat = 'pdf' | 'excel';
export const EXCEL_EXPORT = new InjectionToken<typeof exportExcel>('EXCEL_EXPORT', { factory: () => exportExcel });
@Component({
imports: [MatDialogModule, MatIconModule],
selector: 'app-export-results-dialog',
styleUrl: './export-results-dialog.scss',
templateUrl: './export-results-dialog.html',
})
export class ExportResultsDialog {
readonly selection = signal<ExportFormat | null>(null);
readonly generating = signal(false);
readonly error = signal(false);
private readonly result = inject<AnalysisResult>(MAT_DIALOG_DATA);
private readonly runExcelExport = inject(EXCEL_EXPORT);
constructor(private readonly dialogRef: MatDialogRef<ExportResultsDialog>) {}
select(format: ExportFormat): void {
if (this.generating()) return;
this.selection.set(format);
this.error.set(false);
}
close(): void { if (!this.generating()) this.dialogRef.close(); }
async submit(): Promise<void> {
if (this.generating() || this.selection() !== 'excel') return;
this.generating.set(true);
this.error.set(false);
this.dialogRef.disableClose = true;
try {
await this.runExcelExport(this.result);
this.dialogRef.close();
} catch {
this.error.set(true);
} finally {
this.generating.set(false);
this.dialogRef.disableClose = false;
}
}
}