Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error management #10

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 58 additions & 9 deletions src/app/modules/customers/list-customers/customers.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { Customer, CustomerAPIService } from 'src/services/customerService';
import { MatDialog } from '@angular/material/dialog';
import { DeleteCustomerComponent } from '../delete-customer/delete-customer.component';
import { AddCustomerComponent } from '../add-customer/add-customer.component';

import { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';
import { HttpErrorResponse } from '@angular/common/http';

@Component({
selector: 'app-customers',
Expand All @@ -23,20 +24,28 @@ export class CustomersComponent implements OnInit{
@ViewChild(MatPaginator, { static: true }) paginator!: MatPaginator;


constructor(private customerService:CustomerAPIService, public dialog: MatDialog) {
constructor(private customerService:CustomerAPIService, public dialog: MatDialog,
private snackBar: MatSnackBar,
) {
this.findAllCustomers();

}
ngOnInit(){}


findAllCustomers(){
this.customerService.getAllcustomers().subscribe((data)=>{
console.log(data);
this.dataSource = new MatTableDataSource<Customer>(data);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
}, 0);
this.customerService.getAllcustomers().subscribe({
next: (data) => {
console.log(data);
this.dataSource = new MatTableDataSource<Customer>(data);
setTimeout(() => {
this.dataSource.paginator = this.paginator;
}, 0);
},
error: (err: HttpErrorResponse) => {
console.error('Error sending request:', err);
this.handleError(err);
}
})
}

Expand Down Expand Up @@ -65,7 +74,47 @@ export class CustomersComponent implements OnInit{
console.info(`Filtering customers after delete customer with id ${customer.id}`);
this.dataSource.data = this.dataSource.data.filter( (customer) => customer.id !== customer.id);
}
});
} );

}

private handleError(error: HttpErrorResponse): void {
let errorMessage = 'Ha ocurrido un error en la aplicación';

if (error.status === 0) {
errorMessage = 'No hay conexión del front con el backend. Asegurate que no tienes restricciones de red en tu entorno de trabajo. '+ error.message;
} else if (error.status === 404) {
errorMessage = 'No se encontraron registros.';
} else if (error.error instanceof ErrorEvent) {
// Error del lado del cliente
errorMessage = `Error: ${error.error.message}`;
} else {
// Error del lado del servidor
errorMessage = `El servidor respondió con código ${error.status}. Mensaje: ${error.message}`;
}
this.showNotification(errorMessage, 'ERROR');
}


private showNotification(message: string, type: keyof typeof this.SNACKBAR_TYPES): void {
const config: MatSnackBarConfig = {
...this.snackBarConfig,
panelClass: [this.SNACKBAR_TYPES[type]]
};

this.snackBar.open(message, 'Cerrar', config);
}

private snackBarConfig: MatSnackBarConfig = {
duration: 5000,
horizontalPosition: 'center',
verticalPosition: 'top', // Cambiado a top para que coincida con los estilos centrados
};

private readonly SNACKBAR_TYPES = {
ERROR: 'error-snackbar',
SUCCESS: 'success-snackbar',
WARNING: 'warning-snackbar'
} as const;

}
5 changes: 3 additions & 2 deletions src/app/modules/material.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MatFormFieldModule } from '@angular/material/form-field';
import { MatSelectModule } from '@angular/material/select';
import {MatCardModule} from '@angular/material/card';
import { MatInputModule } from '@angular/material/input';
import { MatSnackBarModule } from '@angular/material/snack-bar';


@NgModule({
Expand All @@ -21,10 +22,10 @@ import { MatInputModule } from '@angular/material/input';
MatListModule, MatButtonModule, MatIconModule, CommonModule,MatFormFieldModule, MatIconModule,
MatSelectModule, MatCardModule, MatFormFieldModule,
MatInputModule ,
MatSlideToggleModule],
MatSlideToggleModule,MatSnackBarModule],
exports: [MatTableModule, MatPaginatorModule, MatToolbarModule, MatSidenavModule, MatFormFieldModule, MatIconModule,
MatListModule, MatButtonModule, MatIconModule, CommonModule,MatSelectModule, MatFormFieldModule,
MatInputModule ,
MatCardModule]
MatCardModule,MatSnackBarModule]
})
export class MaterialModule {}
2 changes: 1 addition & 1 deletion src/app/modules/sidenav/sidenav.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<button mat-icon-button class="example-icon" aria-label="Example icon-button with menu icon" (click)="saveit()">
<mat-icon>menu</mat-icon>
</button>
<span>Customer PWA</span>
<span>Tech Mart CRM</span>
</mat-toolbar-row>
<mat-toolbar-row>
<span class="menu-spacer"></span>
Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>CustomerPwa</title>
<title>Tech Mart RM</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
Expand Down
Loading