Skip to content

Commit dab6463

Browse files
author
Pietro Grandi
committed
refactoring
1 parent aa60f20 commit dab6463

9 files changed

+83
-22
lines changed

app/app.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {Component, OnInit} from '@angular/core';
2-
import {SearchBox} from "./components/search-box/search-box.component";
3-
import {searchReducer} from "./components/reducers/search.reducer";
42
import {Store, provideStore, combineReducers} from "@ngrx/store";
53
import {Observable} from "rxjs/Rx";
6-
import {locationReducer} from "./components/reducers/location.reducer";
7-
import {ProximitySelector} from "./components/location/proximity-selector.component";
8-
import {CurrentSearch} from "./components/reducers/current-search.model";
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 {locationReducer} from "./reducers/location.reducer";
8+
import {CurrentSearch} from "./models/current-search.model";
99

1010
const mainReducer = combineReducers({
1111
searchByName: searchReducer,
@@ -28,7 +28,6 @@ const mainReducer = combineReducers({
2828
<courses-list [searchResults]="results"></courses-list>
2929
</div>
3030
`
31-
3231
})
3332

3433
export class AppComponent implements OnInit {
@@ -45,6 +44,7 @@ export class AppComponent implements OnInit {
4544

4645
ngOnInit() {
4746
this.currentSearch.subscribe((state: CurrentSearch) => {
47+
// the logic is here
4848
console.log(state);
4949
});
5050
}

app/components/location/proximity-selector.component.ts renamed to app/components/proximity-selector.component.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Component, Input} from "@angular/core";
22
import {Store} from "@ngrx/store";
3-
import {CurrentSearch} from "../reducers/current-search.model";
3+
import {CurrentSearch} from "../models/current-search.model";
44

55
@Component({
66
selector: 'proximity-selector',
@@ -10,12 +10,13 @@ import {CurrentSearch} from "../reducers/current-search.model";
1010
<label for="useLocation">Use current location</label>
1111
<input type="checkbox"
1212
[disabled]="disabled"
13-
(change)="turnOnOffLocation($event)">
13+
(change)="onLocation($event)">
1414
</div>
1515
<div class="input-group">
1616
<label for="locationRadius">Radius</label>
17-
<input type="range" min="1" max="100" value="{{defaultRadius}}"
18-
(change)="onRadius($event)">
17+
<input type="range" min="1" max="100" value="50"
18+
[disabled]="!active"
19+
(change)="onRadius($event)">
1920
</div>
2021
`
2122
})
@@ -33,9 +34,7 @@ export class ProximitySelector {
3334

3435
active = false;
3536

36-
defaultRadius = 50;
37-
38-
turnOnOffLocation($event: any) {
37+
onLocation($event: any) {
3938
this.active = $event.target.checked;
4039
if (this.active) {
4140
navigator.geolocation.getCurrentPosition((position: any) => {

app/components/search-box/search-box.component.ts renamed to app/components/search-box.component.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {Observable} from "rxjs/Rx";
22
import {ElementRef, OnInit, Component, Input} from "@angular/core";
33
import {Store} from "@ngrx/store";
4-
import {CurrentSearch} from "../reducers/current-search.model";
54

65
@Component({
76
inputs: ['store'],
@@ -18,15 +17,14 @@ export class SearchBox implements OnInit {
1817
};
1918

2019
@Input()
21-
store: Store<CurrentSearch>;
20+
store: Store<any>;
2221

2322
constructor(private el: ElementRef) {}
2423

2524
ngOnInit(): void {
2625
Observable.fromEvent(this.el.nativeElement, 'keyup')
2726
.map((e: any) => e.target.value)
28-
.filter((text: string) => text.length > 3 || text.length == 0)
29-
.debounceTime(250)
27+
.debounceTime(500)
3028
.subscribe((text: string) =>
3129
this.store.dispatch({
3230
type: SearchBox.StoreEvents.text,

app/models/search-query.model.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export interface SearchQuery {
2+
name: string;
3+
location?: {
4+
latitude: number,
5+
longitude: number,
6+
radius: number
7+
}
8+
}

app/models/search-result.model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface SearchResult {
2+
id: string;
3+
title: string;
4+
thumbnailUrl: string;
5+
}

app/components/reducers/location.reducer.ts renamed to app/reducers/location.reducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ActionReducer, Action } from '@ngrx/store';
2-
import {CurrentSearch} from "./current-search.model";
3-
import {ProximitySelector} from "../location/proximity-selector.component";
2+
import {CurrentSearch} from "../models/current-search.model";
3+
import {ProximitySelector} from "../components/proximity-selector.component";
44

55
export const locationReducer: ActionReducer<CurrentSearch> = (state: CurrentSearch, action: Action) => {
66
switch (action.type) {

app/components/reducers/search.reducer.ts renamed to app/reducers/search.reducer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ActionReducer, Action } from '@ngrx/store';
2-
import {SearchBox} from "../search-box/search-box.component";
3-
import {CurrentSearch} from "./current-search.model";
4-
2+
import {CurrentSearch} from "../models/current-search.model";
3+
import {SearchBox} from "../components/search-box.component";
54

65
export const searchReducer: ActionReducer<CurrentSearch> = (state: CurrentSearch, action: Action) => {
76
switch (action.type) {

app/services/youtube.service.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {BehaviorSubject, Observable} from "rxjs/Rx";
2+
import {Injectable} from "@angular/core";
3+
import {Response, Http} from "@angular/http";
4+
import {SearchResult} from "../models/search-result.model";
5+
import {SearchQuery} from "../models/search-query.model";
6+
7+
const YOUTUBE_API_KEY = "AIzaSyDOfT_BO81aEZScosfTYMruJobmpjqNeEk";
8+
const YOUTUBE_API_URL = "https://www.googleapis.com/youtube/v3/search";
9+
const LOCATION_TEMPLATE = 'location={latitude},{longitude}&locationRadius={radius}km';
10+
11+
12+
@Injectable()
13+
export class YouTubeService {
14+
15+
searchResults: BehaviorSubject<SearchResult[]> = new BehaviorSubject<SearchResult[]>([]);
16+
17+
constructor( private http: Http ) {}
18+
19+
search(query: SearchQuery): Observable<SearchResult[]> {
20+
let params = [
21+
`q=${name}`,
22+
`key=${YOUTUBE_API_KEY}`,
23+
`part=snippet`,
24+
`type=video`,
25+
`maxResults=50`
26+
];
27+
28+
if (query.location) {
29+
const location =
30+
LOCATION_TEMPLATE
31+
.replace(/\{latitude\}/g, query.location.latitude.toString())
32+
.replace(/\{longitude\}/g, query.location.longitude.toString())
33+
.replace(/\{radius\}/g, query.location.radius.toString());
34+
params.push(location);
35+
}
36+
37+
const queryUrl: string = `${YOUTUBE_API_URL}?${params.join('&')}`;
38+
39+
return this.http.get(queryUrl)
40+
.map((response: Response) => {
41+
console.log(response);
42+
return (<any>response.json()).items.map(item => {
43+
return {
44+
id: item.id.videoId,
45+
title: item.snippet.title,
46+
thumbnailUrl: item.snippet.thumbnails.high.url
47+
};
48+
});
49+
});
50+
}
51+
52+
}

0 commit comments

Comments
 (0)