137 lines
4.8 KiB
TypeScript
137 lines
4.8 KiB
TypeScript
import { Component, OnInit } from '@angular/core';
|
|
import { CommonModule } from '@angular/common';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
|
import { MatDialog, MatDialogModule } from '@angular/material/dialog'; // Importa MatDialog
|
|
import { StructureService, Structure } from '../../services/structure.service';
|
|
import { StructureDialogComponent } from '../structure-dialog/structure-dialog.component';
|
|
import { ConfirmDialogComponent, ConfirmDialogData } from '../confirm-dialog/confirm-dialog.component'; // Importa il dialog di conferma
|
|
import { Observable } from 'rxjs';
|
|
|
|
@Component({
|
|
selector: 'app-structures',
|
|
standalone: true,
|
|
imports: [
|
|
CommonModule,
|
|
MatTableModule,
|
|
MatButtonModule,
|
|
MatIconModule,
|
|
MatProgressSpinnerModule,
|
|
MatDialogModule // Aggiungi MatDialogModule agli imports
|
|
],
|
|
templateUrl: './structures.component.html',
|
|
styleUrl: './structures.component.scss'
|
|
})
|
|
export class StructuresComponent implements OnInit {
|
|
|
|
structures$: Observable<Structure[]> | undefined; // Observable per i dati della tabella
|
|
displayedColumns: string[] = ['id', 'name', 'city', 'email', 'actions']; // Colonne da visualizzare
|
|
isLoading = false; // Flag per indicatore di caricamento
|
|
error: string | null = null; // Per messaggi di errore
|
|
|
|
constructor(
|
|
private structureService: StructureService,
|
|
private dialog: MatDialog // Inietta MatDialog
|
|
) { }
|
|
|
|
ngOnInit(): void {
|
|
this.loadStructures();
|
|
}
|
|
|
|
loadStructures(): void {
|
|
this.isLoading = true;
|
|
this.error = null;
|
|
this.structures$ = this.structureService.getStructures();
|
|
|
|
// Gestione semplice del caricamento e degli errori (potrebbe essere migliorata con catchError, finalize)
|
|
this.structures$.subscribe({
|
|
next: () => this.isLoading = false,
|
|
error: (err) => {
|
|
console.error('Error loading structures:', err);
|
|
this.error = 'Errore durante il caricamento delle strutture.';
|
|
this.isLoading = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
addStructure(): void {
|
|
const dialogRef = this.dialog.open(StructureDialogComponent, {
|
|
width: '400px', // Imposta la larghezza del dialog
|
|
data: {} // Passa un oggetto vuoto perché è un'aggiunta
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe(result => {
|
|
// 'result' contiene i dati del form se l'utente ha salvato
|
|
if (result) {
|
|
this.isLoading = true; // Mostra spinner durante il salvataggio
|
|
this.structureService.addStructure(result).subscribe({
|
|
next: () => {
|
|
this.loadStructures(); // Ricarica la lista dopo l'aggiunta
|
|
},
|
|
error: (err) => {
|
|
console.error('Error adding structure:', err);
|
|
this.error = 'Errore durante l\'aggiunta della struttura.';
|
|
this.isLoading = false;
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
editStructure(structure: Structure): void {
|
|
const dialogRef = this.dialog.open(StructureDialogComponent, {
|
|
width: '400px',
|
|
data: { structure: structure } // Passa la struttura esistente al dialog
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe(result => {
|
|
// 'result' contiene i dati aggiornati del form se l'utente ha salvato
|
|
if (result) {
|
|
this.isLoading = true;
|
|
this.structureService.updateStructure(structure.id, result).subscribe({
|
|
next: () => {
|
|
this.loadStructures(); // Ricarica la lista dopo la modifica
|
|
},
|
|
error: (err) => {
|
|
console.error(`Error updating structure ${structure.id}:`, err);
|
|
this.error = 'Errore durante la modifica della struttura.';
|
|
this.isLoading = false;
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
deleteStructure(structure: Structure): void {
|
|
const dialogData: ConfirmDialogData = {
|
|
title: 'Conferma Eliminazione',
|
|
message: `Sei sicuro di voler eliminare la struttura "${structure.name}" (ID: ${structure.id})? L'azione non è reversibile.`,
|
|
confirmButtonText: 'Elimina',
|
|
cancelButtonText: 'Annulla'
|
|
};
|
|
|
|
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
|
|
width: '400px',
|
|
data: dialogData
|
|
});
|
|
|
|
dialogRef.afterClosed().subscribe(confirmed => {
|
|
if (confirmed) {
|
|
this.isLoading = true; // Mostra spinner durante l'eliminazione
|
|
this.structureService.deleteStructure(structure.id).subscribe({
|
|
next: () => {
|
|
this.loadStructures(); // Ricarica la lista dopo l'eliminazione
|
|
},
|
|
error: (err) => {
|
|
console.error(`Error deleting structure ${structure.id}:`, err);
|
|
this.error = 'Errore durante l\'eliminazione della struttura.';
|
|
this.isLoading = false;
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
}
|