2019-02-14 19:05:27 +01:00
|
|
|
import { all, call, delay, put, take, takeLatest } from 'redux-saga/effects'
|
2018-12-17 17:34:32 +01:00
|
|
|
import { actionTypes, failure, loadDataSuccess, tickClock } from './actions'
|
2017-07-13 20:55:29 +02:00
|
|
|
|
2019-11-11 04:24:53 +01:00
|
|
|
function* runClockSaga() {
|
2017-07-13 20:55:29 +02:00
|
|
|
yield take(actionTypes.START_CLOCK)
|
|
|
|
while (true) {
|
|
|
|
yield put(tickClock(false))
|
2019-02-14 19:05:27 +01:00
|
|
|
yield delay(1000)
|
2017-07-13 20:55:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 04:24:53 +01:00
|
|
|
function* loadDataSaga() {
|
2017-07-13 20:55:29 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 04:24:53 +01:00
|
|
|
function* rootSaga() {
|
2017-07-13 20:55:29 +02:00
|
|
|
yield all([
|
|
|
|
call(runClockSaga),
|
2019-11-11 04:24:53 +01:00
|
|
|
takeLatest(actionTypes.LOAD_DATA, loadDataSaga),
|
2017-07-13 20:55:29 +02:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
export default rootSaga
|