|
1 |
| -import {Component, OnInit} from '@angular/core'; |
2 |
| -import {Store, provideStore} from '@ngrx/store'; |
3 |
| -import {Observable} from 'rxjs/Rx'; |
4 |
| -import {ProximitySelector} from './components/proximity-selector.component'; |
5 |
| -import {SearchBox} from './components/search-box.component'; |
6 |
| -import {SearchReducer} from './reducers/search.reducer'; |
7 |
| -import {CurrentSearch} from "./models/current-search.model"; |
8 |
| -import {YouTubeService} from "./services/youtube.service"; |
9 |
| -import {SearchResult} from "./models/search-result.model"; |
10 |
| - |
11 |
| -const storeManager = provideStore({ currentSearch: SearchReducer }); |
12 |
| - |
13 |
| -@Component({ |
14 |
| - selector: 'my-app', |
15 |
| - providers: [ storeManager ], |
16 |
| - directives: [ SearchBox, ProximitySelector ], |
17 |
| - template: ` |
18 |
| - <section class="col-md-8"> |
19 |
| - <h1>{{title}}</h1> |
20 |
| - <div class="row col-md-8"> |
21 |
| - <search-box [store]="store"></search-box> |
22 |
| - <proximity-selector [store]="store" [disabled]="disableSearch" |
23 |
| - [ngClass]="{ disabled: disableSearch }"></proximity-selector> |
24 |
| - </div> |
25 |
| - <div class="row col-md-8 alert alert-danger" *ngIf="disableSearch"> |
26 |
| - <p>Can't use geolocalization with an empty searchbox</p> |
27 |
| - </div> |
28 |
| - <div class="row col-md-8"> |
29 |
| - <p> |
30 |
| - Try to type something in the searchbox, play with the location and with radius: the above state will |
31 |
| - always be consistent and up to date. |
32 |
| - </p> |
33 |
| - <p class="state">{{ state | json }}</p> |
34 |
| - <p class="state" *ngIf="disableSearch">{ }</p> |
35 |
| - </div> |
36 |
| - <h2 *ngIf="!disableSearch">Search results:</h2> |
37 |
| - <h2 *ngIf="disableSearch || searchResults.length == 0">No results</h2> |
38 |
| - <div class="row col-md-8"> |
39 |
| - <div *ngFor="let result of searchResults" class="thumbnail col-sm-6 col-md-4"> |
40 |
| - <div class="caption"> |
41 |
| - <h3>{{ result.title }}</h3> |
42 |
| - </div> |
43 |
| - <!--<img src="{{ result.thumbnailUrl }}" />--> |
44 |
| - </div> |
45 |
| - </div> |
46 |
| - </section> |
47 |
| - ` |
48 |
| -}) |
49 |
| - |
50 |
| -export class AppComponent implements OnInit { |
51 |
| - |
52 |
| - title = 'One Source of Truth for Angular 2'; |
53 |
| - |
54 |
| - private state: CurrentSearch; |
55 |
| - private currentSearch: Observable<CurrentSearch>; |
56 |
| - private searchResults: SearchResult[] = []; |
57 |
| - private disableSearch = false; |
58 |
| - |
59 |
| - constructor( |
60 |
| - private store: Store<CurrentSearch>, |
61 |
| - private youtube: YouTubeService |
62 |
| - ) { |
63 |
| - this.currentSearch = this.store.select<CurrentSearch>('currentSearch'); |
64 |
| - this.youtube.searchResults.subscribe((results: SearchResult[]) => this.searchResults = results); |
65 |
| - } |
66 |
| - |
67 |
| - ngOnInit() { |
68 |
| - this.currentSearch.subscribe((state: CurrentSearch) => { |
69 |
| - this.state = state; |
70 |
| - if (state && state.name && state.name.length > 0) { |
71 |
| - this.disableSearch = false; |
72 |
| - this.youtube.search(state) |
73 |
| - } else { |
74 |
| - this.disableSearch = true; |
75 |
| - this.searchResults = []; |
76 |
| - } |
77 |
| - }); |
78 |
| - } |
79 |
| - |
80 |
| -} |
| 1 | +import {Component, OnInit} from '@angular/core'; |
| 2 | +import {Observable} from "rxjs"; |
| 3 | +import {Store} from "@ngrx/store"; |
| 4 | + |
| 5 | +import {CurrentSearch} from "./models/current-search.model"; |
| 6 | +import {SearchResult} from "./models/search-result.model"; |
| 7 | + |
| 8 | +/* |
| 9 | + <div class="row col-md-8"> |
| 10 | + <search-box [store]="store"></search-box> |
| 11 | + <proximity-selector [store]="store" [disabled]="disableSearch" |
| 12 | + [ngClass]="{ disabled: disableSearch }"></proximity-selector> |
| 13 | + </div> |
| 14 | + <div class="row col-md-8 alert alert-danger" *ngIf="disableSearch"> |
| 15 | + <p>Can't use geolocalization with an empty searchbox</p> |
| 16 | + </div> |
| 17 | + <div class="row col-md-8"> |
| 18 | + <p> |
| 19 | + Try to type something in the searchbox, play with the location and with radius: the above state will |
| 20 | + always be consistent and up to date. |
| 21 | + </p> |
| 22 | + <p class="state">{{ state | json }}</p> |
| 23 | + <p class="state" *ngIf="disableSearch">{ }</p> |
| 24 | + </div> |
| 25 | + <h2 *ngIf="!disableSearch">Search results:</h2> |
| 26 | + <h2 *ngIf="disableSearch || searchResults.length == 0">No results</h2> |
| 27 | + <div class="row col-md-8"> |
| 28 | + <div *ngFor="let result of searchResults" class="thumbnail col-sm-6 col-md-4"> |
| 29 | + <div class="caption"> |
| 30 | + <h3>{{ result.title }}</h3> |
| 31 | + </div> |
| 32 | + <!--<img src="{{ result.thumbnailUrl }}" />--> |
| 33 | + </div> |
| 34 | + </div> |
| 35 | + */ |
| 36 | + |
| 37 | +@Component({ |
| 38 | + selector: 'my-app', |
| 39 | + template: ` |
| 40 | + <section class="col-md-8"> |
| 41 | + <h1>{{title}}</h1> |
| 42 | +
|
| 43 | + </section> |
| 44 | + ` |
| 45 | +}) |
| 46 | +export class AppComponent implements OnInit { |
| 47 | + |
| 48 | + title = 'One Source of Truth for Angular 2'; |
| 49 | + |
| 50 | + private state: CurrentSearch; |
| 51 | + private currentSearch: Observable<CurrentSearch>; |
| 52 | + private searchResults: SearchResult[] = []; |
| 53 | + private disableSearch = false; |
| 54 | + |
| 55 | + constructor( |
| 56 | + private store: Store<CurrentSearch>, |
| 57 | + // private youtube: YouTubeService |
| 58 | + ) { |
| 59 | + this.currentSearch = this.store.select<CurrentSearch>('currentSearch'); |
| 60 | + console.log(`component~!!!`); |
| 61 | + // this.youtube.searchResults.subscribe((results: SearchResult[]) => this.searchResults = results); |
| 62 | + } |
| 63 | + |
| 64 | + ngOnInit() { |
| 65 | + this.currentSearch.subscribe((state: CurrentSearch) => { |
| 66 | + this.state = state; |
| 67 | + if (state && state.name && state.name.length > 0) { |
| 68 | + this.disableSearch = false; |
| 69 | + // this.youtube.search(state) |
| 70 | + } else { |
| 71 | + this.disableSearch = true; |
| 72 | + this.searchResults = []; |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments