You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

import { call, put, takeEvery, takeLatest } from 'redux-saga/effects';
import { fetchBikes as fetchBikesAction, setBikes, setBikesIsFetching, setBikesFetched,
setBikesFetchFailed, setBikeSaved, setBikeSaveFailed, setBikeIsSaving } from './actions';
import { normalize } from 'normalizr';
import * as schema from './schema';
import Api from './services';
// worker Saga: will be fired on USER_FETCH_REQUESTED actions
function* fetchBikes(action) {
try {
yield put({ type: setBikesIsFetching.toString(), payload: true });
const bikes = yield call(Api.fetchBikes);
yield put({ type: setBikes.toString(), payload: normalize(bikes, schema.bikes) });
8 years ago
yield put({ type: setBikesFetched.toString(), payload: true });
} catch (e) {
8 years ago
yield put({ type: setBikesFetchFailed.toString(), payload: e.message });
throw e;
} finally {
yield put({ type: setBikesIsFetching.toString(), payload: false });
}
}
function* watchFetchBikes() {
yield takeEvery(fetchBikesAction.toString(), fetchBikes);
}
// function *saveBike(action) {
// try {
// yield put({ type: setBikeIsSaving.toString(), payload: true });
// const bike = yield call(Api.saveBike(action.payload));
// yield put({ type: setBikes.toString(), payload: normalize([state])})
// yield put({ type: setBikeSaved, payload: true})
// }
// }
export default watchFetchBikes;