Permalink
Browse files

REFACTOR: added normalizr to expose reviews w/out restaurant

  • Loading branch information...
toranb committed Jun 7, 2018
1 parent 114e3a9 commit 3e16c5ef3b7b334d8e158943e86b31fd8dff9ce5
@@ -1,8 +1,8 @@
import { connect } from 'ember-redux';
import { getSelectedRestaurant } from '../reducers/restaurants';
import { getReviews } from '../reducers/restaurants';

const stateToComputed = (state) => ({
restaurant: getSelectedRestaurant(state)
reviews: getReviews(state)
});

export default connect(stateToComputed)();
@@ -1,25 +1,41 @@
import _ from 'lodash';
import reselect from 'reselect';
import { normalize, schema } from 'normalizr';

const { createSelector } = reselect;

const reviewSchema = new schema.Entity('reviews');
const restaurantSchema = new schema.Entity('restaurants', {
reviews: [reviewSchema]
});

const initialState = {
all: undefined,
selectedId: undefined
selectedId: undefined,
reviews: undefined
};

export default ((state, action) => {
switch(action.type) {
case 'RESTAURANTS:TRANSFORM_LIST': {
const restaurants = _.keyBy(action.response, restaurant => restaurant.id);
const merged = _.extend({}, state.all, restaurants);
return Object.assign({}, state, {all: merged});
const normalized = normalize(action.response, [restaurantSchema]);
const { restaurants, reviews } = normalized.entities;
const merged = _.extend({}, state.all, _.keyBy(restaurants, r => r.id));
const mergedReviews = _.extend({}, state.reviews, _.keyBy(reviews, r => r.id));
return Object.assign({}, state, {
all: merged,
reviews: mergedReviews
});
}
case 'RESTAURANTS:TRANSFORM_DETAIL': {
const restaurant = {[action.response.id]: action.response};
const merge = _.extend({}, state.all, restaurant);
const normalized = normalize(restaurant, [restaurantSchema]);
const { restaurants, reviews } = normalized.entities;
const merge = _.extend({}, state.all, restaurants);
const mergeReviews = _.extend({}, state.reviews, _.keyBy(reviews, r => r.id));
return Object.assign({}, state, {
all: merge,
reviews: mergeReviews,
selectedId: action.response.id
});
}
@@ -31,6 +47,7 @@ export default ((state, action) => {

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

export const getRestaurants = createSelector(
all,
@@ -42,3 +59,13 @@ export const getSelectedRestaurant = createSelector(
selectedId,
(all, selectedId) => _.get(all, selectedId)
);

export const getReviews = createSelector(
reviews,
getSelectedRestaurant,
(reviews, selectedRestaurant) => {
return _.map(selectedRestaurant.reviews, reviewId => {
return _.get(reviews, reviewId);
});
}
);
@@ -1,5 +1,5 @@
<ul>
{{#each @restaurant.reviews as |review|}}
{{#each @reviews as |review|}}
<li>{{review.rating}} ★</li>
{{else}}
<li>no reviews</li>
@@ -1 +1 @@
{{yield restaurant}}
{{yield reviews}}
@@ -1,3 +1,3 @@
<RestaurantItem as |restaurant|>
<RestaurantDetail @restaurant={{restaurant}} />
<RestaurantItem as |reviews|>
<RestaurantDetail @reviews={{reviews}} />
</RestaurantItem>
@@ -28,6 +28,7 @@
"ember-cli-htmlbars-inline-precompile": "^1.0.0",
"ember-cli-inject-live-reload": "^1.4.1",
"ember-cli-mirage": "^0.4.7",
"ember-cli-normalizr-shim": "^1.0.0",
"ember-cli-qunit": "^4.1.1",
"ember-cli-shims": "^1.2.0",
"ember-cli-sri": "^2.1.0",
@@ -47,6 +48,7 @@
"ember-welcome-page": "^3.0.0",
"eslint-plugin-ember": "^5.0.0",
"loader.js": "^4.2.3",
"normalizr": "3.2.2",
"qunit-dom": "^0.6.2",
"redux": "3.7.2",
"redux-thunk": "2.2.0",

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

Oops, something went wrong.

0 comments on commit 3e16c5e

Please sign in to comment.