Browse Source

Tidy up reducer.

feature/bike-tracking
Drew Larson 8 years ago
parent
commit
f702233063
  1. 1
      bikeshop_project/assets/js/bikes/actions.js
  2. 4
      bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx
  3. 18
      bikeshop_project/assets/js/bikes/reducers.js
  4. 2
      bikeshop_project/assets/js/bikes/sagas.js

1
bikeshop_project/assets/js/bikes/actions.js

@ -4,4 +4,3 @@ export const fetchBikes = createAction('fetch bikes');
export const setBikes = createAction('set bikes'); export const setBikes = createAction('set bikes');
export const setBikesIsFetching = createAction('set bikes.isFetching'); export const setBikesIsFetching = createAction('set bikes.isFetching');
export const setBikesFetched = createAction('set bikes.fetched'); export const setBikesFetched = createAction('set bikes.fetched');
export const setBike = createAction('set bike');

4
bikeshop_project/assets/js/bikes/components/BikeTable/index.jsx

@ -68,7 +68,7 @@ class BikeTableComponent extends React.Component {
render() { render() {
if (this.props.bikes.fetched) { if (this.props.bikes.fetched) {
const bikeRows = renderBikes(Object.values(this.props.bikes.entities['bikes'] || [])); const bikeRows = renderBikes(Object.values(this.props.bikes.entities));
return ( return (
<div className="mdl-grid"> <div className="mdl-grid">
<div className="mdl-cell mdl-cell--12-col"> <div className="mdl-cell mdl-cell--12-col">
@ -107,7 +107,7 @@ class BikeTableComponent extends React.Component {
} }
const mapStateToProps = state => ({ const mapStateToProps = state => ({
bikes: state.bikes.bikes, bikes: state.bikes,
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({

18
bikeshop_project/assets/js/bikes/reducers.js

@ -1,27 +1,17 @@
import { setBike, setBikes, setBikesIsFetching, setBikesFetched } from './actions'; import { setBikes, setBikesFetched, setBikesIsFetching } from './actions';
import { handleActions } from 'redux-actions'; import { handleActions } from 'redux-actions';
export default handleActions({ export default handleActions({
[setBikes]: (state, action) => ({ [setBikes]: (state, action) => ({
...state, ...state,
bikes: action.payload, entities: action.payload.entities.bikes,
}), }),
[setBikesIsFetching]: (state, action) => ({ [setBikesIsFetching]: (state, action) => ({
...state, ...state,
bikes: {
...state.bikes,
isFetching: action.payload, isFetching: action.payload,
},
}), }),
[setBikesFetched]: (state, action) => ({ [setBikesFetched]: (state, action) => ({
...state, ...state,
bikes: { fetched: action.payload,
...state.bikes,
fetched: action.payload
}
}), }),
[setBike]: (state, action) => ({ }, { entities: {}, isFetching: false, fetched: false });
...state,
...action.payload,
}),
}, { bikes: [], bike: undefined });

2
bikeshop_project/assets/js/bikes/sagas.js

@ -11,7 +11,7 @@ function* fetchBikes(action) {
yield put({ type: setBikesIsFetching.toString(), payload: true }); yield put({ type: setBikesIsFetching.toString(), payload: true });
const bikes = yield call(Api.fetchBikes); const bikes = yield call(Api.fetchBikes);
yield put({ type: setBikes.toString(), payload: normalize(bikes, schema.bikes) }); yield put({ type: setBikes.toString(), payload: normalize(bikes, schema.bikes) });
yield put({ type: setBikesFetched, payload: true }); yield put({ type: setBikesFetched.toString(), payload: true });
} catch (e) { } catch (e) {
yield put({ type: 'BIKES_FETCH_FAILED', message: e.message }); yield put({ type: 'BIKES_FETCH_FAILED', message: e.message });
throw e; throw e;

Loading…
Cancel
Save