| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  | // #docplaster
 | 
					
						
							|  |  |  | // #docregion
 | 
					
						
							|  |  |  | import { Component, OnInit } from '@angular/core'; | 
					
						
							|  |  |  | import { Router }            from '@angular/router'; | 
					
						
							|  |  |  | import { Observable }        from 'rxjs/Observable'; | 
					
						
							|  |  |  | import { Subject }           from 'rxjs/Subject'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { HeroSearchService } from './hero-search.service'; | 
					
						
							|  |  |  | import { Hero } from './hero'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							| 
									
										
										
										
											2016-09-25 18:51:54 -07:00
										 |  |  |   moduleId: module.id, | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |   selector: 'hero-search', | 
					
						
							| 
									
										
										
										
											2016-09-25 18:51:54 -07:00
										 |  |  |   templateUrl: 'hero-search.component.html', | 
					
						
							|  |  |  |   styleUrls: [ 'hero-search.component.css' ], | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |   providers: [HeroSearchService] | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | export class HeroSearchComponent implements OnInit { | 
					
						
							|  |  |  |   // #docregion search
 | 
					
						
							| 
									
										
										
										
											2016-07-19 19:15:23 -04:00
										 |  |  |   heroes: Observable<Hero[]>; | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |   // #enddocregion search
 | 
					
						
							| 
									
										
										
										
											2016-08-01 11:22:23 -07:00
										 |  |  |   // #docregion searchTerms
 | 
					
						
							| 
									
										
										
										
											2016-08-02 09:59:35 -07:00
										 |  |  |   private searchTerms = new Subject<string>(); | 
					
						
							| 
									
										
										
										
											2016-08-01 11:22:23 -07:00
										 |  |  |   // #enddocregion searchTerms
 | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor( | 
					
						
							|  |  |  |     private heroSearchService: HeroSearchService, | 
					
						
							|  |  |  |     private router: Router) {} | 
					
						
							| 
									
										
										
										
											2016-08-01 11:22:23 -07:00
										 |  |  |   // #docregion searchTerms
 | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-19 12:26:14 -07:00
										 |  |  |   // Push a search term into the observable stream.
 | 
					
						
							| 
									
										
										
										
											2016-07-27 15:00:59 +02:00
										 |  |  |   search(term: string): void { | 
					
						
							|  |  |  |     this.searchTerms.next(term); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-01 11:22:23 -07:00
										 |  |  |   // #enddocregion searchTerms
 | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |   // #docregion search
 | 
					
						
							| 
									
										
										
										
											2016-07-19 12:26:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-27 15:00:59 +02:00
										 |  |  |   ngOnInit(): void { | 
					
						
							| 
									
										
										
										
											2016-08-01 11:22:23 -07:00
										 |  |  |     this.heroes = this.searchTerms | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |       .debounceTime(300)        // wait for 300ms pause in events
 | 
					
						
							|  |  |  |       .distinctUntilChanged()   // ignore if next search term is same as previous
 | 
					
						
							|  |  |  |       .switchMap(term => term   // switch to new observable each time
 | 
					
						
							|  |  |  |         // return the http search observable
 | 
					
						
							|  |  |  |         ? this.heroSearchService.search(term) | 
					
						
							|  |  |  |         // or the observable of empty heroes if no search term
 | 
					
						
							|  |  |  |         : Observable.of<Hero[]>([])) | 
					
						
							|  |  |  |       .catch(error => { | 
					
						
							| 
									
										
										
										
											2016-08-01 11:22:23 -07:00
										 |  |  |         // TODO: real error handling
 | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |         console.log(error); | 
					
						
							| 
									
										
										
										
											2016-07-19 19:15:23 -04:00
										 |  |  |         return Observable.of<Hero[]>([]); | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |       }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // #enddocregion search
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-27 15:00:59 +02:00
										 |  |  |   gotoDetail(hero: Hero): void { | 
					
						
							| 
									
										
										
										
											2016-05-23 22:24:15 -04:00
										 |  |  |     let link = ['/detail', hero.id]; | 
					
						
							|  |  |  |     this.router.navigate(link); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-08-18 19:30:34 +01:00
										 |  |  | } |