Permalink
Browse files

REFACTOR: added reselect to encapsulate tree shape

  • Loading branch information...
toranb committed Jun 7, 2018
1 parent aba560b commit 114e3a9fdd988a2a68b0a743f11a0ec31d7f07d9
Showing with 35 additions and 4 deletions.
  1. +2 −2 app/components/restaurant-item.js
  2. +2 −1 app/components/restaurant-items.js
  3. +17 −0 app/reducers/restaurants.js
  4. +3 −1 package.json
  5. +11 −0 yarn.lock
@@ -1,8 +1,8 @@
import _ from 'lodash';
import { connect } from 'ember-redux';
import { getSelectedRestaurant } from '../reducers/restaurants';

const stateToComputed = (state) => ({
restaurant: _.get(state.restaurants.all, state.restaurants.selectedId)
restaurant: getSelectedRestaurant(state)
});

export default connect(stateToComputed)();
@@ -1,7 +1,8 @@
import { connect } from 'ember-redux';
import { getRestaurants } from '../reducers/restaurants';

const stateToComputed = (state) => ({
restaurants: state.restaurants.all
restaurants: getRestaurants(state)
});

export default connect(stateToComputed)();
@@ -1,4 +1,7 @@
import _ from 'lodash';
import reselect from 'reselect';

const { createSelector } = reselect;

const initialState = {
all: undefined,
@@ -25,3 +28,17 @@ export default ((state, action) => {
}
}
});

const all = state => state.restaurants.all;
const selectedId = state => state.restaurants.selectedId;

export const getRestaurants = createSelector(
all,
(all) => all
);

export const getSelectedRestaurant = createSelector(
all,
selectedId,
(all, selectedId) => _.get(all, selectedId)
);
@@ -40,6 +40,7 @@
"ember-redux": "^3.1.1",
"ember-redux-shim": "^2.5.0",
"ember-redux-thunk-shim": "^2.4.0",
"ember-reselect-shim": "^2.0.0",
"ember-resolver": "^4.0.0",
"ember-source": "~3.2.0",
"ember-symbol-observable": "1.0.1",
@@ -48,7 +49,8 @@
"loader.js": "^4.2.3",
"qunit-dom": "^0.6.2",
"redux": "3.7.2",
"redux-thunk": "2.2.0"
"redux-thunk": "2.2.0",
"reselect": "3.0.1"
},
"engines": {
"node": "^4.5 || 6.* || >= 7.*"

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 114e3a9

Please sign in to comment.