@@ -2,72 +2,32 @@ import {Component, OnInit} from '@angular/core';
22import { Observable } from "rxjs" ;
33import { Store } from "@ngrx/store" ;
44
5- import { CurrentSearch } from "./models/current-search.model" ;
6- import { SearchResult } from "./models/search-result.model" ;
7- import { YouTubeService } from "./services/youtube.service" ;
8-
95@Component ( {
106 selector : 'my-app' ,
117 template : `
128 <section class="col-md-8">
139 <h1>{{title}}</h1>
1410 <div class="row col-md-8">
15- <search-box [store]="store"></search-box>
16- <proximity-selector [store]="store" [disabled]="disableSearch"
17- [ngClass]="{ disabled: disableSearch }"></proximity-selector>
18- </div>
19- <div class="row col-md-8 alert alert-danger" *ngIf="disableSearch">
20- <p>Can't use geolocalization with an empty searchbox</p>
11+ <!-- put searchbox and proximity selector here -->
2112 </div>
2213 <div class="row col-md-8">
23- <p>
24- Try to type something in the searchbox, play with the location and with radius: the above state will
25- always be consistent and up to date.
26- </p>
27- <p class="state">{{ state | json }}</p>
28- <p class="state" *ngIf="disableSearch">state is empty</p>
29- <h2 *ngIf="!disableSearch">Search results:</h2>
14+ <!-- put the state here -->
3015 </div>
31- <h2 *ngIf="disableSearch || searchResults.length == 0">No results</h2>
32- <div class="row col-md-8">
33- <div *ngFor="let result of searchResults" class="thumbnail col-sm-6 col-md-4">
34- <div class="caption">
35- <h3>{{ result.title }}</h3>
36- </div>
37- <img src="{{ result.thumbnailUrl }}" />
38- </div>
39- </div>
40- </section>
16+ </section>
4117 `
4218} )
4319export class AppComponent implements OnInit {
4420
4521 title = 'One Source of Truth for Angular 2' ;
4622
47- private state : CurrentSearch ;
48- private currentSearch : Observable < CurrentSearch > ;
49- private searchResults : SearchResult [ ] = [ ] ;
50- private disableSearch = false ;
51-
5223 constructor (
53- private store : Store < CurrentSearch > ,
54- private youtube : YouTubeService
24+ // inject store and youtube service
5525 ) {
56- this . currentSearch = this . store . select < CurrentSearch > ( 'currentSearch' ) ;
57- this . youtube . searchResults . subscribe ( ( results : SearchResult [ ] ) => this . searchResults = results ) ;
26+ // select the state here
5827 }
5928
6029 ngOnInit ( ) {
61- this . currentSearch . subscribe ( ( state : CurrentSearch ) => {
62- this . state = state ;
63- if ( state && state . name && state . name . length > 0 ) {
64- this . disableSearch = false ;
65- this . youtube . search ( state )
66- } else {
67- this . disableSearch = true ;
68- this . searchResults = [ ] ;
69- }
70- } ) ;
30+ // handle here the state's update
7131 }
7232
7333}
0 commit comments