This commit is contained in:
Constantin Simonis 2024-12-18 13:53:14 +01:00
parent 8b871d2e34
commit 49716901e5
Signed by: csimonis
GPG Key ID: 758DD9C506603183
2 changed files with 13 additions and 11 deletions

View File

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

View File

@ -15,4 +15,8 @@ export default class EmployeeApiService {
public deleteById(id: number): Observable<Employee> {
return this.http.delete(`${EmployeeApiService.BASE_URL}/employees/${id}`)
}
public getAll(): Observable<Employee[]> {
return this.http.get<Employee[]>(`${EmployeeApiService.BASE_URL}/employees`)
}
}