2017-07-13 18:55:29 +00:00
|
|
|
/* global fetch */
|
|
|
|
|
2019-01-25 21:25:08 +00:00
|
|
|
import { all, call, delay, put, take, takeLatest } from 'redux-saga/effects'
|
2017-07-13 18:55:29 +00:00
|
|
|
import es6promise from 'es6-promise'
|
2017-11-04 14:05:16 +00:00
|
|
|
import 'isomorphic-unfetch'
|
2017-07-13 18:55:29 +00:00
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
import { actionTypes, failure, loadDataSuccess, tickClock } from './actions'
|
2017-07-13 18:55:29 +00:00
|
|
|
|
|
|
|
es6promise.polyfill()
|
|
|
|
|
|
|
|
function * runClockSaga () {
|
|
|
|
yield take(actionTypes.START_CLOCK)
|
|
|
|
while (true) {
|
|
|
|
yield put(tickClock(false))
|
2019-01-25 21:25:08 +00:00
|
|
|
yield delay(1000)
|
2017-07-13 18:55:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function * loadDataSaga () {
|
|
|
|
try {
|
|
|
|
const res = yield fetch('https://jsonplaceholder.typicode.com/users')
|
|
|
|
const data = yield res.json()
|
|
|
|
yield put(loadDataSuccess(data))
|
|
|
|
} catch (err) {
|
|
|
|
yield put(failure(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function * rootSaga () {
|
|
|
|
yield all([
|
|
|
|
call(runClockSaga),
|
|
|
|
takeLatest(actionTypes.LOAD_DATA, loadDataSaga)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
export default rootSaga
|