From eaa06029bb27dbd6767f0412cfa378f7b3655cc5 Mon Sep 17 00:00:00 2001 From: ThompsonNye Date: Mon, 23 Jun 2025 16:53:11 +0200 Subject: [PATCH] Reset selected car if it is deleted --- .../src/app/modules/cars/cars/cars.component.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Vegasco-Web/src/app/modules/cars/cars/cars.component.ts b/src/Vegasco-Web/src/app/modules/cars/cars/cars.component.ts index c6d1da2..1024907 100644 --- a/src/Vegasco-Web/src/app/modules/cars/cars/cars.component.ts +++ b/src/Vegasco-Web/src/app/modules/cars/cars/cars.component.ts @@ -25,6 +25,7 @@ import { throwError } from 'rxjs'; import { CarCardComponent } from './components/car-card/car-card.component'; +import { SelectedCarService } from '@vegasco-web/modules/entries/services/selected-car.service'; @Component({ selector: 'app-entries', @@ -52,6 +53,7 @@ import { CarCardComponent } from './components/car-card/car-card.component'; export class CarsComponent { private readonly carClient = inject(CarClient); private readonly messageService = inject(MessageService); + private readonly selectedCarService = inject(SelectedCarService); protected readonly nonDeletedCars$: Observable; @@ -78,13 +80,21 @@ export class CarsComponent { ); } - onCarDeleted(entry: Car): void { - this.deletedCars$.next([...this.deletedCars$.value, entry.id]); + onCarDeleted(car: Car): void { + this.deletedCars$.next([...this.deletedCars$.value, car.id]); this.messageService.add({ severity: 'success', summary: 'Auto gelöscht', detail: 'Das Auto wurde erfolgreich gelöscht.', }); + this.resetSelectedCarIfDeleted(car); + } + + private resetSelectedCarIfDeleted(car: Car) { + const selectedCarId = this.selectedCarService.getSelectedCarId(); + if (selectedCarId === car.id) { + this.selectedCarService.setSelectedCarId(null); + } } private handleGetCarsError(error: unknown): Observable {