Permalink
Browse files

FEATURE: display list of restaurants from api

  • Loading branch information...
toranb committed Jun 8, 2018
1 parent 120838d commit 27dc1c5d5e85aca082c80394eaa841229ae3db52
@@ -0,0 +1,12 @@
import { Restaurant } from '../types/restaurants';

export interface ListAction {
type: 'RESTAURANTS:TRANSFORM_LIST';
response: Array<Restaurant>;
}

export interface ListDispatch {
(action: ListAction): void;
}

export const TRANSFORM_LIST = 'RESTAURANTS:TRANSFORM_LIST';
@@ -0,0 +1,8 @@
import { connect } from 'ember-redux';
import { RootState } from '../types/index';

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

export default connect(stateToComputed)();
@@ -0,0 +1,6 @@
import { combineReducers } from 'redux';
import restaurants from './restaurants';

export default combineReducers({
restaurants
});
@@ -0,0 +1,20 @@
import _ from 'lodash';
import { Restaurant, RestaurantState } from '../types/restaurants';
import { TRANSFORM_LIST, ListAction } from '../actions/restaurants';

const initialState = {
all: undefined
};

export default ((state: RestaurantState, action: ListAction): RestaurantState => {
switch(action.type) {
case TRANSFORM_LIST: {
const restaurants = _.keyBy(action.response, (restaurant: Restaurant) => restaurant.id);
const merged = _.extend({}, state.all, restaurants);
return Object.assign({}, state, {all: merged});
}
default: {
return state || initialState;
}
}
});
@@ -7,6 +7,7 @@ const Router = EmberRouter.extend({
});

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

export default Router;
@@ -0,0 +1,15 @@
import fetch from 'fetch';
import { route } from 'ember-redux';
import { Restaurants } from '../types/restaurants';
import { ListDispatch, TRANSFORM_LIST } from '../actions/restaurants';

const model = (dispatch: ListDispatch) => {
return fetch('/api/restaurants')
.then((fetched: Response) => fetched.json())
.then((response: Restaurants) => dispatch({
type: TRANSFORM_LIST,
response: response.restaurants
}));
};

export default route({model})();
@@ -1,5 +1 @@
{{!-- The following component displays Ember's default welcome message. --}}
{{welcome-page}}
{{!-- Feel free to remove this! --}}

{{outlet}}
{{outlet}}
@@ -0,0 +1 @@
{{yield restaurants}}
@@ -0,0 +1,5 @@
<ul>
{{#each-in @restaurants as |key restaurant|}}
<li>{{restaurant.name}}</li>
{{/each-in}}
</ul>
@@ -0,0 +1,3 @@
<RestaurantItems as |restaurants|>
<RestaurantList @restaurants={{restaurants}} />
</RestaurantItems>
@@ -0,0 +1,9 @@
import { RestaurantState } from './restaurants';

export interface RootState {
restaurants: RestaurantState;
}

export interface Dictionary<T> {
[index: string]: T;
}
@@ -0,0 +1,14 @@
import { Dictionary } from './index';

export interface Restaurant {
id: number;
name: string;
}

export interface RestaurantState {
all: Dictionary<Restaurant>;
}

export interface Restaurants {
restaurants: Array<Restaurant>;
}
@@ -18,6 +18,7 @@
},
"devDependencies": {
"@types/ember": "^2.8.25",
"@types/lodash": "^4.14.65",
"@types/ember-qunit": "^3.0.2",
"@types/ember-test-helpers": "^0.7.1",
"@types/ember-testing-helpers": "^0.0.3",
@@ -40,14 +41,22 @@
"ember-cli-typescript": "^1.3.2",
"ember-cli-uglify": "^2.0.0",
"ember-export-application-global": "^2.0.0",
"ember-fetch": "^5.0.0",
"ember-load-initializers": "^1.1.0",
"ember-lodash-shim": "^2.0.0",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-redux": "^3.1.1",
"ember-redux-shim": "^2.5.0",
"ember-redux-thunk-shim": "^2.4.0",
"ember-resolver": "^4.0.0",
"ember-source": "~3.2.0",
"ember-symbol-observable": "1.0.1",
"ember-welcome-page": "^3.0.0",
"eslint-plugin-ember": "^5.0.0",
"loader.js": "^4.2.3",
"qunit-dom": "^0.6.2",
"redux": "3.7.2",
"redux-thunk": "2.2.0",
"typescript": "^2.9.1"
},
"engines": {
@@ -20,6 +20,9 @@
"baseUrl": ".",
"module": "es6",
"paths": {
"ember-redux": ["node_modules/ember-redux/index.d.ts"],
"redux": ["node_modules/redux/index.d.ts"],
"fetch": ["node_modules/ember-fetch/index.d.ts"],
"guides/*": [
"app/*"
],
Oops, something went wrong.

0 comments on commit 27dc1c5

Please sign in to comment.