-
Notifications
You must be signed in to change notification settings - Fork 146
/
SearchSpotifyService.ts
32 lines (28 loc) · 1.05 KB
/
SearchSpotifyService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// reference: http://blog.rangle.io/recipes-for-redux/
import {Injectable} from "@angular/core";
import {Http} from "@angular/http";
import {Observable} from "rxjs";
import {ActionsObservable} from "redux-observable";
export interface IPayloadAction {
payload?: any;
}
@Injectable()
export class SearchEpic {
constructor(private http: Http) {
}
searchSpotify = (action$: ActionsObservable<IPayloadAction>) => {
return action$.ofType('SEARCH_SPOTIFY')
.debounceTime(500)
.mergeMap(({payload}) => {
const {term, type} = payload;
const url = `https://api.spotify.com/v1/search?q=${term}&type=${type}`;
return this.http
.get(url)
.map((res) => ({type: 'SEARCH_RESULTS', payload: res.json()}))
.takeUntil(action$.ofType('SEARCH_CANCELLED'))
.catch((err: any, b: any) => {
return Observable.of({type: 'SEARCH_ERROR', payload: err});
})
})
}
}