Permalink
Browse files

FEATURE: display restaurant details from api

  • Loading branch information...
toranb committed Jun 7, 2018
1 parent a379ca7 commit aba560b5c8091686db93428e8c1496666d711480
@@ -0,0 +1,8 @@
import _ from 'lodash';
import { connect } from 'ember-redux';

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

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

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

export default ((state, action) => {
@@ -11,6 +12,14 @@ export default ((state, action) => {
const merged = _.extend({}, state.all, restaurants);
return Object.assign({}, state, {all: merged});
}
case 'RESTAURANTS:TRANSFORM_DETAIL': {
const restaurant = {[action.response.id]: action.response};
const merge = _.extend({}, state.all, restaurant);
return Object.assign({}, state, {
all: merge,
selectedId: action.response.id
});
}
default: {
return state || initialState;
}
@@ -7,7 +7,9 @@ const Router = EmberRouter.extend({
});

Router.map(function() {
this.route('restaurants', {path: '/'});
this.route('restaurants', {path: '/'}, function() {
this.route('detail', {path: '/detail/:id'});
});
});

export default Router;
@@ -0,0 +1,13 @@
import fetch from 'fetch';
import { route } from 'ember-redux';

const model = (dispatch, params) => {
return fetch(`/api/restaurants/${params.id}`)
.then(fetched => fetched.json())
.then(response => dispatch({
type: 'RESTAURANTS:TRANSFORM_DETAIL',
response: response.restaurants
}));
};

export default route({model})();
@@ -0,0 +1,7 @@
<ul>
{{#each @restaurant.reviews as |review|}}
<li>{{review.rating}} ★</li>
{{else}}
<li>no reviews</li>
{{/each}}
</ul>
@@ -0,0 +1 @@
{{yield restaurant}}
@@ -1,5 +1,5 @@
<ul>
{{#each-in @restaurants as |key restaurant|}}
<li>{{restaurant.name}}</li>
<li>{{#link-to "restaurants.detail" restaurant.id}}{{restaurant.name}}{{/link-to}}</li>
{{/each-in}}
</ul>
@@ -1,3 +1,4 @@
<RestaurantItems as |restaurants|>
<RestaurantList @restaurants={{restaurants}} />
{{outlet}}
</RestaurantItems>
@@ -0,0 +1,3 @@
<RestaurantItem as |restaurant|>
<RestaurantDetail @restaurant={{restaurant}} />
</RestaurantItem>

0 comments on commit aba560b

Please sign in to comment.