feat(hotel-details): handle hotel not found scenario
This commit is contained in:
		
					parent
					
						
							
								8dd8b8b438
							
						
					
				
			
			
				commit
				
					
						dfadc7052b
					
				
			
		
					 1 changed files with 12 additions and 4 deletions
				
			
		|  | @ -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<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; | ||||
|     }); | ||||
|     }) | ||||
|   } | ||||
| } | ||||
|  |  | |||
		Reference in a new issue