Browse Source

Update bike details.

feature/bike-tracking
Drew Larson 8 years ago
parent
commit
780825b828
  1. 3
      bikeshop_project/assets/js/bikes/actions.js
  2. 6
      bikeshop_project/assets/js/bikes/reducers.js
  3. 10
      bikeshop_project/assets/js/bikes/sagas.js

3
bikeshop_project/assets/js/bikes/actions.js

@ -10,4 +10,5 @@ export const setBikeIsSaving = createAction('set bike.isSaving');
export const setBikeSaveFailed = createAction('set bike.isSaving'); export const setBikeSaveFailed = createAction('set bike.isSaving');
export const editBike = createAction('edit bike'); export const editBike = createAction('edit bike');
export const createBike = createAction('create bike'); export const createBike = createAction('create bike');
export const saveBike = createAction('save bike'); export const saveBike = createAction('save bike');
export const mergeBike = createAction('merge bike');

6
bikeshop_project/assets/js/bikes/reducers.js

@ -1,4 +1,4 @@
import { setBikes, setBikesFetched, setBikesIsFetching, setBikesFetchFailed, createBike, editBike } from './actions'; import { setBikes, setBikesFetched, setBikesIsFetching, setBikesFetchFailed, createBike, editBike, mergeBike } from './actions';
import { handleActions } from 'redux-actions'; import { handleActions } from 'redux-actions';
export default handleActions({ export default handleActions({
@ -34,4 +34,8 @@ export default handleActions({
create: true, create: true,
}, },
}), }),
[mergeBike]: (state, action) => ({
...state,
entities: { ...state.entities, ...action.payload.entities.bikes },
})
}, { entities: {}, form: { bike: null, create: undefined }, isFetching: false, fetched: false, fetchFailed: undefined }); }, { entities: {}, form: { bike: null, create: undefined }, isFetching: false, fetched: false, fetchFailed: undefined });

10
bikeshop_project/assets/js/bikes/sagas.js

@ -1,6 +1,6 @@
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'; import { call, put, takeEvery, takeLatest } from 'redux-saga/effects';
import { fetchBikes as fetchBikesAction, setBikes, setBikesIsFetching, setBikesFetched, import { fetchBikes as fetchBikesAction, setBikes, setBikesIsFetching, setBikesFetched,
setBikesFetchFailed, setBikeSaved, setBikeSaveFailed, setBikeIsSaving, saveBike as saveBikeAction } from './actions'; setBikesFetchFailed, setBikeSaved, setBikeSaveFailed, setBikeIsSaving, saveBike as saveBikeAction, mergeBike } from './actions';
import { normalize, denormalize } from 'normalizr'; import { normalize, denormalize } from 'normalizr';
import * as schema from './schema'; import * as schema from './schema';
import Api from './services'; import Api from './services';
@ -28,14 +28,14 @@ function* watchFetchBikes() {
function* saveBike(action) { function* saveBike(action) {
try { try {
yield put({ type: setBikeIsSaving.toString(), payload: true }); yield put({ type: setBikeIsSaving.toString(), payload: true });
const bike = yield call(Api.saveBike(action.payload)); const bike = yield call(Api.saveBike, action.payload);
yield put({ type: setBikes.toString(), payload: normalize([state])}) yield put({ type: mergeBike.toString(), payload: normalize([bike], schema.bikes)})
yield put({ type: setBikeSaved, payload: true}) yield put({ type: setBikeSaved.toString(), payload: true})
} catch (e) { } catch (e) {
yield put({ type: setBikeSaveFailed, payload: false }); yield put({ type: setBikeSaveFailed, payload: false });
throw(e); throw(e);
} finally { } finally {
yield put({ type: setBikeIsSaving.toSource(), payload: false }); yield put({ type: setBikeIsSaving.toString(), payload: false });
} }
} }

Loading…
Cancel
Save