feat(hotel-details): handle hotel not found scenario

This commit is contained in:
Jan Gleytenhoover 2024-11-12 09:37:50 +01:00
parent 8dd8b8b438
commit dfadc7052b
Signed by: jank
GPG Key ID: B267751B8AE29EFE

@ -1,10 +1,11 @@
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute, Router } from '@angular/router';
import { Hotel } from '../HotelItem/hotel'; import { Hotel } from '../HotelItem/hotel';
import { CurrencyPipe } from '@angular/common'; import { CurrencyPipe } from '@angular/common';
import { StarRatingComponent } from '../star-rating/star-rating.component'; import { StarRatingComponent } from '../star-rating/star-rating.component';
import { HotelItem } from '../HotelItem/HotelItem.component'; import { HotelItem } from '../HotelItem/HotelItem.component';
import { catchError, EMPTY } from 'rxjs';
@Component({ @Component({
selector: 'app-hotel-details', selector: 'app-hotel-details',
@ -16,14 +17,21 @@ import { HotelItem } from '../HotelItem/HotelItem.component';
export class HotelDetailsComponent implements OnInit { export class HotelDetailsComponent implements OnInit {
public hotel: any; public hotel: any;
constructor(private route: ActivatedRoute, private http: HttpClient) { } constructor(private route: ActivatedRoute, private http: HttpClient, private router: Router) { }
ngOnInit(): void { ngOnInit(): void {
const routeParams = this.route.snapshot.paramMap; const routeParams = this.route.snapshot.paramMap;
const hotelId = routeParams.get("id"); const hotelId = routeParams.get("id");
this.http.get<Hotel>("api/hotels/" + hotelId).subscribe(res => { this.http.get<Hotel>("api/hotels/" + hotelId).pipe(
catchError(() => {
alert("Not Found");
this.router.navigate(["/"]);
return EMPTY;
})
).subscribe(res => {
this.hotel = res; this.hotel = res;
}); })
} }
} }