Skip to content

Commit 7ab7087

Browse files
author
pigr2
committed
undergoing modifications....
1 parent 6b357f4 commit 7ab7087

File tree

8 files changed

+64
-15
lines changed

8 files changed

+64
-15
lines changed

app/app.component.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {ProximitySelector} from './components/proximity-selector.component';
55
import {SearchBox} from './components/search-box.component';
66
import {SearchReducer} from './reducers/search.reducer';
77
import {SearchQuery} from "./models/search-query.model";
8+
import {YouTubeService} from "./services/youtube.service";
9+
import {SearchResult} from "./models/search-result.model";
810

911
const storeManager = provideStore({ currentSearch: SearchReducer });
1012

@@ -26,27 +28,57 @@ const storeManager = provideStore({ currentSearch: SearchReducer });
2628
</p>
2729
<p>{{ state | json }}</p>
2830
</div>
31+
<div class="row col-md-8">
32+
<ul>
33+
<li *ngFor="let result of searchResults">{{ result.title }}</li>
34+
</ul>
35+
</div>
2936
</section>
3037
`
3138
})
3239

40+
//
41+
3342
export class AppComponent implements OnInit {
3443

35-
title = 'One Source of Truth for Angular 2';
44+
static StoreEvents = {
45+
results: 'RESULTS'
46+
};
47+
48+
title = ''; //'One Source of Truth for Angular 2';
3649

3750
private state: SearchQuery;
3851

3952
private currentSearch: Observable<SearchQuery>;
53+
private searchResults: SearchResult[] = [];
4054

4155
constructor(
42-
private store: Store<SearchQuery>
56+
private store: Store<SearchQuery>,
57+
private youtube: YouTubeService
4358
) {
4459
this.currentSearch = this.store.select<SearchQuery>('currentSearch');
4560
}
4661

4762
ngOnInit() {
4863
this.currentSearch.subscribe((state: SearchQuery) => {
4964
this.state = state;
65+
if (state && state.name) {
66+
this.youtube.search(state).subscribe((results: SearchResult[]) =>
67+
this.store.dispatch({
68+
type: AppComponent.StoreEvents.results,
69+
payload: {
70+
results: results
71+
}
72+
})
73+
)
74+
} else {
75+
this.store.dispatch({
76+
type: AppComponent.StoreEvents.results,
77+
payload: {
78+
results: []
79+
}
80+
})
81+
}
5082
});
5183
}
5284

app/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { bootstrap } from '@angular/platform-browser-dynamic';
22
import { HTTP_PROVIDERS } from '@angular/http';
33

44
import { AppComponent } from './app.component';
5+
import {YouTubeService} from "./services/youtube.service";
56

67
bootstrap(AppComponent, [
7-
HTTP_PROVIDERS
8+
HTTP_PROVIDERS,
9+
YouTubeService
810
]);

app/models/search-query.model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import {SearchResult} from "./search-result.model";
12
export interface SearchQuery {
23
name: string;
34
location?: {
45
latitude: number,
56
longitude: number,
67
radius: number
7-
}
8+
},
9+
results: SearchResult[]
810
}

app/reducers/search.reducer.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@ import { ActionReducer, Action } from '@ngrx/store';
22
import {SearchBox} from '../components/search-box.component';
33
import {ProximitySelector} from '../components/proximity-selector.component';
44
import {SearchQuery} from "../models/search-query.model";
5+
import {AppComponent} from "../app.component";
56

67
export const SearchReducer: ActionReducer<SearchQuery> = (state: SearchQuery, action: Action) => {
78
switch (action.type) {
89
case SearchBox.StoreEvents.text:
910
return Object.assign({}, state, {
10-
text: action.payload.text
11+
name: action.payload.text
1112
});
1213
case ProximitySelector.StoreEvents.position:
1314
return Object.assign({}, state, {
14-
position: {
15+
location: {
1516
latitude: action.payload.position.latitude,
1617
longitude: action.payload.position.longitude
1718
}
1819
});
1920
case ProximitySelector.StoreEvents.radius:
21+
//const position = Object.assign(state.location)
2022
return Object.assign({}, state, {
21-
position: {
23+
location: {
2224
radius: action.payload.radius
2325
}
2426
});
2527
case ProximitySelector.StoreEvents.off:
2628
return Object.assign({}, state, {
27-
position: null
29+
location: null
30+
});
31+
case AppComponent.StoreEvents.results:
32+
return Object.assign({}, state, {
33+
results: action.payload.results
2834
});
2935
default:
3036
return state;

app/services/youtube.service.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {BehaviorSubject, Observable} from 'rxjs/Rx';
1+
import {Observable, BehaviorSubject} from 'rxjs/Rx';
22
import {Injectable} from '@angular/core';
33
import {Response, Http} from '@angular/http';
44
import {SearchResult} from '../models/search-result.model';
@@ -18,35 +18,41 @@ export class YouTubeService {
1818

1919
search(query: SearchQuery): Observable<SearchResult[]> {
2020
let params = [
21-
`q=${name}`,
21+
`q=${query.name}`,
2222
`key=${YOUTUBE_API_KEY}`,
2323
`part=snippet`,
2424
`type=video`,
2525
`maxResults=50`
2626
];
2727

2828
if (query.location) {
29+
const radius = query.location.radius ? query.location.radius : 50;
2930
const location =
3031
LOCATION_TEMPLATE
3132
.replace(/\{latitude\}/g, query.location.latitude.toString())
3233
.replace(/\{longitude\}/g, query.location.longitude.toString())
33-
.replace(/\{radius\}/g, query.location.radius.toString());
34+
.replace(/\{radius\}/g, radius.toString());
3435
params.push(location);
3536
}
3637

3738
const queryUrl: string = `${YOUTUBE_API_URL}?${params.join('&')}`;
39+
40+
console.log(queryUrl);
3841

39-
return this.http.get(queryUrl)
42+
this.http.get(queryUrl)
4043
.map((response: Response) => {
4144
console.log(response);
42-
return (<any>response.json()).items.map(item => {
45+
return <any>response.json().items.map(item => {
4346
return {
4447
id: item.id.videoId,
4548
title: item.snippet.title,
4649
thumbnailUrl: item.snippet.thumbnails.high.url
4750
};
4851
});
49-
});
52+
})
53+
.subscribe((results: SearchResult[]) => this.searchResults.next(results));
54+
55+
return this.searchResults;
5056
}
5157

5258
}

index.html

100644100755
File mode changed.

start.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npm run start

systemjs.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function(global) {
22
// map tells the System loader where to look for things
33
var map = {
4-
'app': 'app',
4+
'app': 'dist',
55
'@angular': 'node_modules/@angular',
66
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
77
'rxjs': 'node_modules/rxjs',

0 commit comments

Comments
 (0)