mirror of https://github.com/fspc/workstand.git
Drew Larson
8 years ago
13 changed files with 420 additions and 206 deletions
@ -0,0 +1,7 @@ |
|||
import { createAction } from 'redux-actions'; |
|||
|
|||
export const fetchBikes = createAction('fetch bikes'); |
|||
export const setBikes = createAction('set bikes'); |
|||
export const setBikesIsFetching = createAction('set bikes.isFetching'); |
|||
export const setBikesFetched = createAction('set bikes.fetched'); |
|||
export const setBike = createAction('set bike'); |
@ -0,0 +1,27 @@ |
|||
import { setBike, setBikes, setBikesIsFetching, setBikesFetched } from './actions'; |
|||
import { handleActions } from 'redux-actions'; |
|||
|
|||
export default handleActions({ |
|||
[setBikes]: (state, action) => ({ |
|||
...state, |
|||
bikes: action.payload, |
|||
}), |
|||
[setBikesIsFetching]: (state, action) => ({ |
|||
...state, |
|||
bikes: { |
|||
...state.bikes, |
|||
isFetching: action.payload, |
|||
}, |
|||
}), |
|||
[setBikesFetched]: (state, action) => ({ |
|||
...state, |
|||
bikes: { |
|||
...state.bikes, |
|||
fetched: action.payload |
|||
} |
|||
}), |
|||
[setBike]: (state, action) => ({ |
|||
...state, |
|||
...action.payload, |
|||
}), |
|||
}, { bikes: [], bike: undefined }); |
@ -0,0 +1,27 @@ |
|||
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'; |
|||
import { fetchBikes as fetchBikesAction, setBikes, setBikesIsFetching, setBikesFetched } 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) }); |
|||
yield put({ type: setBikesFetched, payload: true }); |
|||
} catch (e) { |
|||
yield put({ type: 'BIKES_FETCH_FAILED', message: e.message }); |
|||
throw e; |
|||
} finally { |
|||
yield put({ type: setBikesIsFetching.toString(), payload: false }); |
|||
} |
|||
} |
|||
|
|||
function* watchFetchBikes() { |
|||
yield takeEvery(fetchBikesAction.toString(), fetchBikes); |
|||
} |
|||
|
|||
export default watchFetchBikes; |
@ -0,0 +1,4 @@ |
|||
import { normalize, schema } from 'normalizr'; |
|||
|
|||
const bike = new schema.Entity('bikes'); |
|||
export const bikes = new schema.Array(bike); |
@ -0,0 +1,28 @@ |
|||
import fetch from 'isomorphic-fetch'; |
|||
|
|||
const checkStatus = (response) => { |
|||
if (response.status >= 200 && response.status < 300) { |
|||
return response; |
|||
} |
|||
const error = new Error(response.statusText); |
|||
error.response = response; |
|||
throw error; |
|||
}; |
|||
|
|||
const parseJson = response => response.json(); |
|||
|
|||
const Api = { |
|||
fetchBikes() { |
|||
return fetch('/api/v1/bikes/', { |
|||
credentials: 'same-origin', |
|||
}) |
|||
.then(checkStatus) |
|||
.then(parseJson) |
|||
.then(data => data) |
|||
.catch((error) => { |
|||
console.log('request failed', error); |
|||
}); |
|||
}, |
|||
}; |
|||
|
|||
export default Api; |
Loading…
Reference in new issue