diff --git a/bikeshop_project/assets/js/bikes/actions.js b/bikeshop_project/assets/js/bikes/actions.js index 0304798..b3e619b 100644 --- a/bikeshop_project/assets/js/bikes/actions.js +++ b/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 editBike = createAction('edit bike'); export const createBike = createAction('create bike'); -export const saveBike = createAction('save bike'); \ No newline at end of file +export const saveBike = createAction('save bike'); +export const mergeBike = createAction('merge bike'); \ No newline at end of file diff --git a/bikeshop_project/assets/js/bikes/reducers.js b/bikeshop_project/assets/js/bikes/reducers.js index c7d67e9..15cf576 100644 --- a/bikeshop_project/assets/js/bikes/reducers.js +++ b/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'; export default handleActions({ @@ -34,4 +34,8 @@ export default handleActions({ 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 }); diff --git a/bikeshop_project/assets/js/bikes/sagas.js b/bikeshop_project/assets/js/bikes/sagas.js index 94ae5b5..9a942e7 100644 --- a/bikeshop_project/assets/js/bikes/sagas.js +++ b/bikeshop_project/assets/js/bikes/sagas.js @@ -1,6 +1,6 @@ import { call, put, takeEvery, takeLatest } from 'redux-saga/effects'; 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 * as schema from './schema'; import Api from './services'; @@ -28,14 +28,14 @@ function* watchFetchBikes() { 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}) + const bike = yield call(Api.saveBike, action.payload); + yield put({ type: mergeBike.toString(), payload: normalize([bike], schema.bikes)}) + yield put({ type: setBikeSaved.toString(), payload: true}) } catch (e) { yield put({ type: setBikeSaveFailed, payload: false }); throw(e); } finally { - yield put({ type: setBikeIsSaving.toSource(), payload: false }); + yield put({ type: setBikeIsSaving.toString(), payload: false }); } }