-
Notifications
You must be signed in to change notification settings - Fork 1
/
saga.js
61 lines (54 loc) · 1.58 KB
/
saga.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { put, takeLatest } from 'redux-saga/effects';
import { actionTypes, failure, fetchPlaylistSuccess, fetchShowSuccess, searchDataSuccess } from './actions';
import omdb from './api/omdb';
function* searchData({ term }) {
try {
const res = yield omdb.get('', { params: { s: term } });
console.log(res.data.Search);
if (res.data.Error) yield put(failure(res.data.Error));
else yield put(searchDataSuccess(res.data.Search));
} catch (err) {
yield put(failure(err));
}
}
function* fetchTitle(imdbId) {
console.log(imdbId);
const res = yield omdb.get('', { params: { i: imdbId } });
console.log(res.data);
if (res.data.Error) yield put(failure(res.data.Error));
return res;
}
function* fetchShow({ imdbId }) {
try {
const res = yield fetchTitle(imdbId);
console.log(res.data);
yield put(fetchShowSuccess(res.data));
} catch (err) {
yield put(failure(err));
}
}
function* fetchPlaylist({ playlist }) {
try {
const fetchedPlaylist = [];
for (let i = 0; i < playlist.shows.length; i++) {
let res = yield fetchTitle(playlist.shows[i]);
yield fetchedPlaylist.push(res.data);
}
console.log(fetchedPlaylist);
yield put(
fetchPlaylistSuccess({
name: playlist.name,
id: playlist.id,
shows: fetchedPlaylist,
}),
);
} catch (err) {
yield put(failure(err));
}
}
function* rootSaga() {
yield takeLatest(actionTypes.FETCH_SHOW, fetchShow);
yield takeLatest(actionTypes.SEARCH_DATA, searchData);
yield takeLatest(actionTypes.FETCH_PLAYLIST, fetchPlaylist);
}
export default rootSaga;