refactor
This commit is contained in:
parent
e474385692
commit
27b5867912
@ -1,4 +1,4 @@
|
|||||||
import {Component} from '@angular/core';
|
import {Component, inject, OnInit} from '@angular/core';
|
||||||
import {CommonModule} from '@angular/common';
|
import {CommonModule} from '@angular/common';
|
||||||
import {catchError, Observable, of, retry} from 'rxjs';
|
import {catchError, Observable, of, retry} from 'rxjs';
|
||||||
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
|
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
|
||||||
@ -16,6 +16,7 @@ import {MatTableModule} from '@angular/material/table';
|
|||||||
import {MatSortModule} from '@angular/material/sort';
|
import {MatSortModule} from '@angular/material/sort';
|
||||||
import {MatDialog} from "@angular/material/dialog";
|
import {MatDialog} from "@angular/material/dialog";
|
||||||
import {DeleteEmployeeComponent} from "../delete-employee/delete-employee.component";
|
import {DeleteEmployeeComponent} from "../delete-employee/delete-employee.component";
|
||||||
|
import EmployeeApiService from "../services/employee-api.service";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-employee-list',
|
selector: 'app-employee-list',
|
||||||
@ -39,24 +40,21 @@ import {DeleteEmployeeComponent} from "../delete-employee/delete-employee.compon
|
|||||||
},
|
},
|
||||||
styleUrl: './employee-list.component.css'
|
styleUrl: './employee-list.component.css'
|
||||||
})
|
})
|
||||||
export class EmployeeListComponent {
|
export class EmployeeListComponent implements OnInit{
|
||||||
private static readonly EMPLOYEES_ENDPOINT = 'http://localhost:8089/employees';
|
private readonly apiService: EmployeeApiService = inject(EmployeeApiService);
|
||||||
|
private readonly snackBar: MatSnackBar = inject(MatSnackBar);
|
||||||
|
private readonly deleteDialogue: MatDialog = inject(MatDialog);
|
||||||
|
|
||||||
private static readonly MAX_RETRIES = 3;
|
private static readonly MAX_RETRIES = 3;
|
||||||
public employees$: Observable<Employee[]>;
|
public employees$: Observable<Employee[]>;
|
||||||
public readonly displayedColumns: string[] = ['name', 'actions'];
|
public readonly displayedColumns: string[] = ['name', 'actions'];
|
||||||
|
|
||||||
constructor(
|
public ngOnInit(): void {
|
||||||
private readonly httpClient: HttpClient,
|
|
||||||
private readonly snackBar: MatSnackBar,
|
|
||||||
private readonly deleteDialogue: MatDialog
|
|
||||||
) {
|
|
||||||
this.employees$ = this.fetchEmployees();
|
this.employees$ = this.fetchEmployees();
|
||||||
}
|
}
|
||||||
|
|
||||||
private fetchEmployees(): Observable<Employee[]> {
|
private fetchEmployees(): Observable<Employee[]> {
|
||||||
return this.httpClient.get<Employee[]>(
|
return this.apiService.getAll().pipe(
|
||||||
EmployeeListComponent.EMPLOYEES_ENDPOINT,
|
|
||||||
).pipe(
|
|
||||||
retry(EmployeeListComponent.MAX_RETRIES),
|
retry(EmployeeListComponent.MAX_RETRIES),
|
||||||
catchError((error: HttpErrorResponse) => {
|
catchError((error: HttpErrorResponse) => {
|
||||||
console.error('Error fetching employees:', error);
|
console.error('Error fetching employees:', error);
|
||||||
|
@ -15,4 +15,8 @@ export default class EmployeeApiService {
|
|||||||
public deleteById(id: number): Observable<Employee> {
|
public deleteById(id: number): Observable<Employee> {
|
||||||
return this.http.delete(`${EmployeeApiService.BASE_URL}/employees/${id}`)
|
return this.http.delete(`${EmployeeApiService.BASE_URL}/employees/${id}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getAll(): Observable<Employee[]> {
|
||||||
|
return this.http.get<Employee[]>(`${EmployeeApiService.BASE_URL}/employees`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user