From dfadc7052bd5750ff5b1ff863aac07f5cb86a161 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Tue, 12 Nov 2024 09:37:50 +0100 Subject: [PATCH] feat(hotel-details): handle hotel not found scenario --- src/app/hotel-details/hotel-details.component.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/app/hotel-details/hotel-details.component.ts b/src/app/hotel-details/hotel-details.component.ts index ef39b15..2d3c396 100644 --- a/src/app/hotel-details/hotel-details.component.ts +++ b/src/app/hotel-details/hotel-details.component.ts @@ -1,10 +1,11 @@ import { HttpClient } from '@angular/common/http'; import { Component, OnInit } from '@angular/core'; -import { ActivatedRoute } from '@angular/router'; +import { ActivatedRoute, Router } from '@angular/router'; import { Hotel } from '../HotelItem/hotel'; import { CurrencyPipe } from '@angular/common'; import { StarRatingComponent } from '../star-rating/star-rating.component'; import { HotelItem } from '../HotelItem/HotelItem.component'; +import { catchError, EMPTY } from 'rxjs'; @Component({ selector: 'app-hotel-details', @@ -16,14 +17,21 @@ import { HotelItem } from '../HotelItem/HotelItem.component'; export class HotelDetailsComponent implements OnInit { public hotel: any; - constructor(private route: ActivatedRoute, private http: HttpClient) { } + constructor(private route: ActivatedRoute, private http: HttpClient, private router: Router) { } ngOnInit(): void { const routeParams = this.route.snapshot.paramMap; const hotelId = routeParams.get("id"); - this.http.get("api/hotels/" + hotelId).subscribe(res => { + this.http.get("api/hotels/" + hotelId).pipe( + catchError(() => { + alert("Not Found"); + this.router.navigate(["/"]); + + return EMPTY; + }) + ).subscribe(res => { this.hotel = res; - }); + }) } }