├── src ├── assets │ └── .gitkeep ├── app │ ├── food-list │ │ ├── food-list.component.css │ │ ├── food-list.component.spec.ts │ │ ├── food-list.component.ts │ │ └── food-list.component.html │ ├── food-detail │ │ ├── food-detail.component.css │ │ ├── food-detail.component.spec.ts │ │ ├── food-detail.component.html │ │ └── food-detail.component.ts │ ├── search-results │ │ ├── search-results.component.css │ │ ├── food-result │ │ │ ├── food-result.component.css │ │ │ ├── food-result.component.spec.ts │ │ │ ├── food-result.component.html │ │ │ └── food-result.component.ts │ │ ├── search-results.component.html │ │ ├── search-results.component.spec.ts │ │ └── search-results.component.ts │ ├── search-input │ │ ├── search-input.component.css │ │ ├── search-input.component.html │ │ ├── search-input.component.spec.ts │ │ └── search-input.component.ts │ ├── app.component.css │ ├── models │ │ ├── search-result.ts │ │ └── food.ts │ ├── app.component.html │ ├── services │ │ ├── store.service.spec.ts │ │ ├── nutrition.service.spec.ts │ │ ├── store.service.ts │ │ └── nutrition.service.ts │ ├── app.component.ts │ ├── shared │ │ └── material.module.ts │ ├── app.component.spec.ts │ ├── store │ │ ├── effects.ts │ │ ├── actions.ts │ │ └── reducer.ts │ └── app.module.ts ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── typings.d.ts ├── tsconfig.app.json ├── main.ts ├── styles │ ├── theme.scss │ └── styles.css ├── tsconfig.spec.json ├── index.html ├── test.ts └── polyfills.ts ├── .firebaserc ├── database.rules.json ├── firebase.json ├── functions ├── package.json └── index.js ├── e2e ├── tsconfig.e2e.json ├── app.po.ts └── app.e2e-spec.ts ├── .editorconfig ├── tsconfig.json ├── .gitignore ├── protractor.conf.js ├── README.md ├── karma.conf.js ├── .angular-cli.json ├── package.json └── tslint.json /src/assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/food-list/food-list.component.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/food-detail/food-detail.component.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/search-results/search-results.component.css: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/app/search-results/food-result/food-result.component.css: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.firebaserc: -------------------------------------------------------------------------------- 1 | { 2 | "projects": { 3 | "default": "beslen-app" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /src/app/search-input/search-input.component.css: -------------------------------------------------------------------------------- 1 | :host() { 2 | width: 100%; 3 | } -------------------------------------------------------------------------------- /src/app/app.component.css: -------------------------------------------------------------------------------- 1 | :host() { 2 | display: flex; 3 | height: 100%; 4 | } -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/denizcoskun/nutrifacts-ngrx-store/HEAD/src/favicon.ico -------------------------------------------------------------------------------- /database.rules.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | ".read": "auth != null", 4 | ".write": "auth != null" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/typings.d.ts: -------------------------------------------------------------------------------- 1 | /* SystemJS module definition */ 2 | declare var module: NodeModule; 3 | interface NodeModule { 4 | id: string; 5 | } 6 | -------------------------------------------------------------------------------- /firebase.json: -------------------------------------------------------------------------------- 1 | { 2 | "database": { 3 | "rules": "database.rules.json" 4 | }, 5 | "hosting": { 6 | "public": "dist" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/app/search-input/search-input.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 |
-------------------------------------------------------------------------------- /functions/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "functions", 3 | "description": "Cloud Functions for Firebase", 4 | "dependencies": { 5 | "firebase-admin": "^4.1.2", 6 | "firebase-functions": "^0.5" 7 | }, 8 | "private": true 9 | } 10 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/e2e", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types":[ 8 | "jasmine", 9 | "node" 10 | ] 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /e2e/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, element, by } from 'protractor'; 2 | 3 | export class NgrxFirebasePage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getParagraphText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "es2015", 6 | "baseUrl": "", 7 | "types": [] 8 | }, 9 | "exclude": [ 10 | "test.ts", 11 | "**/*.spec.ts" 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /functions/index.js: -------------------------------------------------------------------------------- 1 | var functions = require('firebase-functions'); 2 | 3 | // // Start writing Firebase Functions 4 | // // https://firebase.google.com/functions/write-firebase-functions 5 | // 6 | // exports.helloWorld = functions.https.onRequest((request, response) => { 7 | // response.send("Hello from Firebase!"); 8 | // }) 9 | -------------------------------------------------------------------------------- /src/app/models/search-result.ts: -------------------------------------------------------------------------------- 1 | export interface ISearchResult { 2 | id: string; 3 | name: string; 4 | } 5 | 6 | export class SearchResult implements ISearchResult { 7 | id: string; 8 | name: string; 9 | constructor(obj?: any) { 10 | this.id = obj.ndbno || ''; 11 | this.name = obj.name || ''; 12 | } 13 | } -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule); 12 | -------------------------------------------------------------------------------- /e2e/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { NgrxFirebasePage } from './app.po'; 2 | 3 | describe('ngrx-firebase App', () => { 4 | let page: NgrxFirebasePage; 5 | 6 | beforeEach(() => { 7 | page = new NgrxFirebasePage(); 8 | }); 9 | 10 | it('should display message saying app works', () => { 11 | page.navigateTo(); 12 | expect(page.getParagraphText()).toEqual('app works!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /src/styles/theme.scss: -------------------------------------------------------------------------------- 1 | // https://material.angular.io/guide/theming 2 | @import '~@angular/material/theming'; 3 | @include mat-core(); 4 | 5 | $primary: mat-palette($mat-blue, 800); 6 | $accent: mat-palette($mat-pink, A200, A100, A400); 7 | $warn: mat-palette($mat-deep-orange); 8 | $light-theme: mat-light-theme($primary, $accent, $warn); 9 | @include angular-material-theme($light-theme); 10 | -------------------------------------------------------------------------------- /src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "baseUrl": "", 8 | "types": [ 9 | "jasmine", 10 | "node" 11 | ] 12 | }, 13 | "files": [ 14 | "test.ts" 15 | ], 16 | "include": [ 17 | "**/*.spec.ts", 18 | "**/*.d.ts" 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 |
2 | 3 | Nutrifacts 4 | restaurant 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 |
13 | 14 |
-------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // The file contents for the current environment will overwrite these during build. 2 | // The build system defaults to the dev environment which uses `environment.ts`, but if you do 3 | // `ng build --env=prod` then `environment.prod.ts` will be used instead. 4 | // The list of which env maps to which file can be found in `.angular-cli.json`. 5 | 6 | export const environment = { 7 | production: false 8 | }; 9 | -------------------------------------------------------------------------------- /src/app/services/store.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, inject } from '@angular/core/testing'; 2 | 3 | import { StoreService } from './store.service'; 4 | 5 | describe('StoreService', () => { 6 | beforeEach(() => { 7 | TestBed.configureTestingModule({ 8 | providers: [StoreService] 9 | }); 10 | }); 11 | 12 | it('should ...', inject([StoreService], (service: StoreService) => { 13 | expect(service).toBeTruthy(); 14 | })); 15 | }); 16 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, ViewChild, ElementRef, ChangeDetectionStrategy } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.css'], 7 | changeDetection: ChangeDetectionStrategy.OnPush 8 | }) 9 | export class AppComponent implements OnInit { 10 | 11 | 12 | constructor() { 13 | } 14 | 15 | ngOnInit(): void { 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NutriFacts 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | Loading... 15 | 16 | 17 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "outDir": "./dist/out-tsc", 5 | "baseUrl": "src", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "moduleResolution": "node", 9 | "emitDecoratorMetadata": true, 10 | "experimentalDecorators": true, 11 | "target": "es5", 12 | "typeRoots": [ 13 | "node_modules/@types" 14 | ], 15 | "lib": [ 16 | "es2016", 17 | "dom" 18 | ] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/app/services/nutrition.service.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, inject } from '@angular/core/testing'; 2 | 3 | import { NutritionService } from './nutrition.service'; 4 | 5 | describe('NutrionServiceService', () => { 6 | beforeEach(() => { 7 | TestBed.configureTestingModule({ 8 | providers: [NutritionService] 9 | }); 10 | }); 11 | 12 | it('should ...', inject([NutritionService], (service: NutritionService) => { 13 | expect(service).toBeTruthy(); 14 | })); 15 | }); 16 | -------------------------------------------------------------------------------- /src/app/models/food.ts: -------------------------------------------------------------------------------- 1 | export interface Nutrients { 2 | nutrient_id: string; 3 | nutrient: string; 4 | unit: string; 5 | value: number; 6 | gm: number; 7 | } 8 | export interface IFood { 9 | id: string; 10 | name: string; 11 | nutrients: Nutrients[]; 12 | } 13 | 14 | export class Food implements IFood{ 15 | id: string; 16 | name: string; 17 | nutrients: Nutrients[]; 18 | constructor(obj?: any) { 19 | this.id = obj.ndbno || ''; 20 | this.name = obj.name || ''; 21 | this.nutrients = obj.nutrients || []; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/app/search-results/search-results.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | Results 4 |
5 | 6 |
7 | 8 | 9 | {{result.name}} 10 | 11 | 12 |
13 |
-------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | 8 | # dependencies 9 | /node_modules 10 | 11 | # IDEs and editors 12 | /.idea 13 | .project 14 | .classpath 15 | .c9/ 16 | *.launch 17 | .settings/ 18 | *.sublime-workspace 19 | 20 | # IDE - VSCode 21 | .vscode/* 22 | !.vscode/settings.json 23 | !.vscode/tasks.json 24 | !.vscode/launch.json 25 | !.vscode/extensions.json 26 | 27 | # misc 28 | /.sass-cache 29 | /connect.lock 30 | /coverage 31 | /libpeerconnection.log 32 | npm-debug.log 33 | testem.log 34 | /typings 35 | 36 | # e2e 37 | /e2e/*.js 38 | /e2e/*.map 39 | 40 | # System Files 41 | .DS_Store 42 | Thumbs.db 43 | -------------------------------------------------------------------------------- /src/app/food-list/food-list.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { FoodListComponent } from './food-list.component'; 4 | 5 | describe('FoodListComponent', () => { 6 | let component: FoodListComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ FoodListComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(FoodListComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/food-detail/food-detail.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { FoodDetailComponent } from './food-detail.component'; 4 | 5 | describe('FoodDetailComponent', () => { 6 | let component: FoodDetailComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ FoodDetailComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(FoodDetailComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/search-input/search-input.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { SearchInputComponent } from './search-input.component'; 4 | 5 | describe('SearchInputComponent', () => { 6 | let component: SearchInputComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ SearchInputComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(SearchInputComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/search-results/search-results.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { SearchResultsComponent } from './search-results.component'; 4 | 5 | describe('SearchResultsComponent', () => { 6 | let component: SearchResultsComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ SearchResultsComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(SearchResultsComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/food-list/food-list.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Observable } from "rxjs/Observable"; 3 | import { Food } from ".././models/food";; 4 | 5 | import { Store } from "@ngrx/store"; 6 | import * as Actions from ".././store/actions"; 7 | import * as fromRoot from ".././store/reducer"; 8 | 9 | @Component({ 10 | selector: 'app-food-list', 11 | templateUrl: './food-list.component.html', 12 | styleUrls: ['./food-list.component.css'] 13 | }) 14 | export class FoodListComponent implements OnInit { 15 | foodList: Observable; 16 | constructor(private store: Store) { } 17 | 18 | ngOnInit() { 19 | this.foodList = this.store.select(state => state.foodList);; 20 | } 21 | 22 | removeFood(food: Food) { 23 | this.store.dispatch(new Actions.RemoveFood(food)); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/app/search-results/food-result/food-result.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { SearchResultDetailComponent } from './search-result-detail.component'; 4 | 5 | describe('SearchResultDetailComponent', () => { 6 | let component: SearchResultDetailComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ SearchResultDetailComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(SearchResultDetailComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './e2e/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | beforeLaunch: function() { 23 | require('ts-node').register({ 24 | project: 'e2e/tsconfig.e2e.json' 25 | }); 26 | }, 27 | onPrepare() { 28 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /src/app/search-results/search-results.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, Input } from '@angular/core'; 2 | import { SearchResult } from '../models/search-result'; 3 | import { Observable } from 'rxjs/Observable'; 4 | 5 | import { Store } from '@ngrx/store'; 6 | import * as fromRoot from '.././store/reducer'; 7 | import * as Actions from '.././store/actions'; 8 | 9 | @Component({ 10 | selector: 'app-search-results', 11 | templateUrl: './search-results.component.html', 12 | styleUrls: ['./search-results.component.css'] 13 | }) 14 | export class SearchResultsComponent implements OnInit { 15 | 16 | results: Observable; 17 | loading: Observable; 18 | 19 | constructor( private store: Store) { 20 | this.results = this.store.select(state => state.results); 21 | this.loading = this.store.select(state => state.loading); 22 | } 23 | 24 | ngOnInit() { 25 | 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/app/food-list/food-list.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | My Food List 4 |
5 | 6 | 7 | 8 |
9 | {{food.name}} 10 | Energy: {{food.nutrients[1].gm}} kJ 11 | 12 |
13 |
14 | delete 15 |
16 |
17 |
18 | 19 |
20 | Search some food e.g. Yogurt, egg... 21 | search 22 |
23 |
24 |
25 | -------------------------------------------------------------------------------- /src/app/shared/material.module.ts: -------------------------------------------------------------------------------- 1 | import { CommonModule } from '@angular/common'; 2 | import { NgModule } from '@angular/core'; 3 | import { FormsModule } from '@angular/forms'; 4 | import { 5 | MdButtonModule, 6 | MdCheckboxModule, 7 | MdInputModule, 8 | MdListModule, 9 | MdProgressBarModule, 10 | MdProgressSpinnerModule, 11 | MdSelectModule, 12 | MdTabsModule, 13 | MdToolbarModule, 14 | MdSnackBarModule, 15 | MdAutocompleteModule, 16 | MdIconModule 17 | } from '@angular/material'; 18 | 19 | 20 | const modules = [ 21 | CommonModule, 22 | FormsModule, 23 | MdButtonModule, 24 | MdCheckboxModule, 25 | MdInputModule, 26 | MdListModule, 27 | MdSelectModule, 28 | MdProgressSpinnerModule, 29 | MdProgressBarModule, 30 | MdSelectModule, 31 | MdSnackBarModule, 32 | MdTabsModule, 33 | MdToolbarModule, 34 | MdAutocompleteModule, 35 | MdIconModule 36 | ]; 37 | 38 | @NgModule({ 39 | imports: [ modules ], 40 | exports: [ modules ], 41 | declarations: [], 42 | }) 43 | export class MaterialModule { } -------------------------------------------------------------------------------- /src/app/food-detail/food-detail.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | Nutrition Facts 5 | 6 | (per 100 g) 7 |
8 |
9 |
10 | 11 |
{{food?.name}}
12 | 13 | 14 | 15 | 16 | 17 | 18 |
{{nutrients.nutrient}}{{nutrients.gm}} {{nutrients.unit}}
19 | 20 | 22 |
23 | 24 | 25 |
26 | Nothing found 27 |
28 |
29 | 30 |
-------------------------------------------------------------------------------- /src/app/search-results/food-result/food-result.component.html: -------------------------------------------------------------------------------- 1 |
2 |
3 |
4 | Nutrition Facts 5 | 6 | (per 100 g) 7 |
8 |
9 |
10 | 11 |
{{food?.name}}
12 | 13 | 14 | 15 | 16 | 17 | 18 |
{{nutrients.nutrient}}{{nutrients.gm}} {{nutrients.unit}}
19 | 20 | 22 |
23 | 24 |
25 | Nothing found 26 |
27 |
28 | 29 |
-------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | 3 | import { AppComponent } from './app.component'; 4 | 5 | describe('AppComponent', () => { 6 | beforeEach(async(() => { 7 | TestBed.configureTestingModule({ 8 | declarations: [ 9 | AppComponent 10 | ], 11 | }).compileComponents(); 12 | })); 13 | 14 | it('should create the app', async(() => { 15 | const fixture = TestBed.createComponent(AppComponent); 16 | const app = fixture.debugElement.componentInstance; 17 | expect(app).toBeTruthy(); 18 | })); 19 | 20 | it(`should have as title 'app works!'`, async(() => { 21 | const fixture = TestBed.createComponent(AppComponent); 22 | const app = fixture.debugElement.componentInstance; 23 | expect(app.title).toEqual('app works!'); 24 | })); 25 | 26 | it('should render title in a h1 tag', async(() => { 27 | const fixture = TestBed.createComponent(AppComponent); 28 | fixture.detectChanges(); 29 | const compiled = fixture.debugElement.nativeElement; 30 | expect(compiled.querySelector('h1').textContent).toContain('app works!'); 31 | })); 32 | }); 33 | -------------------------------------------------------------------------------- /src/app/services/store.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Observable, BehaviorSubject } from 'rxjs'; 3 | import { Food } from '.././models/food'; 4 | import { SearchResult } from '.././models/search-result'; 5 | import { NutritionService } from '.././services/nutrition.service'; 6 | import { Store } from "@ngrx/store"; 7 | import * as fromRoot from ".././store/reducer"; 8 | import * as Actions from '.././store/actions'; 9 | 10 | @Injectable() 11 | export class StoreService { 12 | 13 | state: Observable; 14 | constructor(private nutritionService: NutritionService, private store: Store) { 15 | this.state = this.store; 16 | } 17 | 18 | 19 | searchFood(query) { 20 | this.store.dispatch(new Actions.Search(query)); 21 | } 22 | 23 | fetchFood(id) { 24 | this.store.dispatch(new Actions.FetchFood(id)); 25 | } 26 | 27 | getFood(id) { 28 | this.store.dispatch(new Actions.GetFood(id)); 29 | } 30 | 31 | addBasket() { 32 | this.store.dispatch(new Actions.AddFood()); 33 | } 34 | 35 | removeBasket(food: Food) { 36 | this.store.dispatch(new Actions.RemoveFood(food)); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/app/store/effects.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Effect, Actions, toPayload } from '@ngrx/effects'; 3 | import { Action } from '@ngrx/store'; 4 | import { Observable } from 'rxjs/Observable'; 5 | 6 | import * as FoodActions from "./actions"; 7 | import { NutritionService } from ".././services/nutrition.service"; 8 | 9 | 10 | @Injectable() 11 | 12 | export class FoodEffects { 13 | 14 | @Effect() 15 | searchFood$: Observable = this.actions$ 16 | .ofType(FoodActions.SEARCH) 17 | .map(toPayload) 18 | .switchMap(query => { 19 | return this.nutritionService.searchFood(query) 20 | .map(results => new FoodActions.SearchDone(results)); 21 | // catch(() => of(new FoodActions.FetchFoodFail())) 22 | }); 23 | 24 | @Effect() 25 | fetchFood$: Observable = this.actions$ 26 | .ofType(FoodActions.FETCH_FOOD) 27 | .map(toPayload) 28 | .switchMap(query => { 29 | return this.nutritionService.fetchFood(query) 30 | .map(food => new FoodActions.FetchFoodDone(food)); 31 | // catch(() => of(new FoodActions.FetchFoodFail())) 32 | }); 33 | 34 | constructor(private actions$: Actions, private nutritionService: NutritionService) {} 35 | 36 | 37 | } -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/long-stack-trace-zone'; 4 | import 'zone.js/dist/proxy.js'; 5 | import 'zone.js/dist/sync-test'; 6 | import 'zone.js/dist/jasmine-patch'; 7 | import 'zone.js/dist/async-test'; 8 | import 'zone.js/dist/fake-async-test'; 9 | import { getTestBed } from '@angular/core/testing'; 10 | import { 11 | BrowserDynamicTestingModule, 12 | platformBrowserDynamicTesting 13 | } from '@angular/platform-browser-dynamic/testing'; 14 | 15 | // Unfortunately there's no typing for the `__karma__` variable. Just declare it as any. 16 | declare var __karma__: any; 17 | declare var require: any; 18 | 19 | // Prevent Karma from running prematurely. 20 | __karma__.loaded = function () {}; 21 | 22 | // First, initialize the Angular testing environment. 23 | getTestBed().initTestEnvironment( 24 | BrowserDynamicTestingModule, 25 | platformBrowserDynamicTesting() 26 | ); 27 | // Then we find all the tests. 28 | const context = require.context('./', true, /\.spec\.ts$/); 29 | // And load the modules. 30 | context.keys().map(context); 31 | // Finally, start Karma to run the tests. 32 | __karma__.start(); 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nutrifacts 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.0.0. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 8 | 9 | ## Code scaffolding 10 | 11 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive/pipe/service/class/module`. 12 | 13 | ## Build 14 | 15 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `-prod` flag for a production build. 16 | 17 | ## Running unit tests 18 | 19 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 20 | 21 | ## Running end-to-end tests 22 | 23 | Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). 24 | Before running the tests make sure you are serving the app via `ng serve`. 25 | 26 | ## Further help 27 | 28 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). 29 | # nutrifacts 30 | # nutrifacts-ngrx-store 31 | -------------------------------------------------------------------------------- /src/app/food-detail/food-detail.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Food } from '../models/food'; 3 | import { ActivatedRoute } from '@angular/router'; 4 | import { Observable } from 'rxjs/Observable'; 5 | import { Router } from '@angular/router'; 6 | 7 | import { Store } from "@ngrx/store"; 8 | import * as fromRoot from ".././store/reducer"; 9 | import * as Actions from ".././store/actions"; 10 | 11 | @Component({ 12 | selector: 'app-food-detail', 13 | templateUrl: './food-detail.component.html', 14 | styleUrls: ['./food-detail.component.css'] 15 | }) 16 | export class FoodDetailComponent implements OnInit { 17 | 18 | food: Observable; 19 | loading: Observable; 20 | 21 | constructor(private route: ActivatedRoute, private router: Router, 22 | private store: Store) {} 23 | 24 | ngOnInit() { 25 | 26 | this.food = this.store.select(state => state.selectedFood); 27 | this.loading = this.store.select(state => state.loading); 28 | 29 | this.route.params 30 | .map(params => params.id) 31 | .do(id => this.store.dispatch(new Actions.GetFood(id))) 32 | .subscribe(); 33 | } 34 | 35 | removeFromList(food: Food): void { 36 | this.store.dispatch(new Actions.RemoveFood(food)); 37 | this.router.navigate(['myfoods']); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/0.13/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular/cli'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular/cli/plugins/karma') 14 | ], 15 | client:{ 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | files: [ 19 | { pattern: './src/test.ts', watched: false } 20 | ], 21 | preprocessors: { 22 | './src/test.ts': ['@angular/cli'] 23 | }, 24 | mime: { 25 | 'text/x-typescript': ['ts','tsx'] 26 | }, 27 | coverageIstanbulReporter: { 28 | reports: [ 'html', 'lcovonly' ], 29 | fixWebpackSourcePaths: true 30 | }, 31 | angularCli: { 32 | environment: 'dev' 33 | }, 34 | reporters: config.angularCli && config.angularCli.codeCoverage 35 | ? ['progress', 'coverage-istanbul'] 36 | : ['progress', 'kjhtml'], 37 | port: 9876, 38 | colors: true, 39 | logLevel: config.LOG_INFO, 40 | autoWatch: true, 41 | browsers: ['Chrome'], 42 | singleRun: false 43 | }); 44 | }; 45 | -------------------------------------------------------------------------------- /src/app/search-input/search-input.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit,ViewChild, ElementRef } from '@angular/core'; 2 | import { FormControl } from "@angular/forms"; 3 | import { Observable, BehaviorSubject } from 'rxjs'; 4 | import { Store } from '@ngrx/store'; 5 | import * as fromRoot from '.././store/reducer'; 6 | import * as Actions from '.././store/actions'; 7 | 8 | import 'rxjs/add/operator/debounceTime'; 9 | import 'rxjs/add/operator/do'; 10 | import 'rxjs/add/operator/switch'; 11 | 12 | @Component({ 13 | selector: 'app-search-input', 14 | templateUrl: './search-input.component.html', 15 | styleUrls: ['./search-input.component.css'] 16 | }) 17 | export class SearchInputComponent implements OnInit { 18 | 19 | // @ViewChild('input') input: ElementRef; 20 | 21 | searchControl = new FormControl(''); 22 | 23 | constructor(private store: Store) { } 24 | 25 | ngOnInit() { 26 | this.searchControl.valueChanges 27 | .debounceTime(300) 28 | .switch() 29 | .filter((value) => value.trim()) 30 | .do(() => this.store.dispatch(new Actions.Search(this.searchControl.value))) 31 | .subscribe(); 32 | 33 | /* 34 | Observable.fromEvent(this.input.nativeElement, 'keyup') 35 | .map((e: any) => e.target.value) 36 | .filter((text: string) => text.length > 1) 37 | .debounceTime(250) 38 | .do((query: string) => this.store.dispatch(new Actions.Search(query))) 39 | .switch() 40 | .subscribe(); */ 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /.angular-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "project": { 4 | "name": "ngrx-firebase" 5 | }, 6 | "apps": [ 7 | { 8 | "root": "src", 9 | "outDir": "dist", 10 | "assets": [ 11 | "assets", 12 | "favicon.ico" 13 | ], 14 | "index": "index.html", 15 | "main": "main.ts", 16 | "polyfills": "polyfills.ts", 17 | "test": "test.ts", 18 | "tsconfig": "tsconfig.app.json", 19 | "testTsconfig": "tsconfig.spec.json", 20 | "prefix": "app", 21 | "styles": [ 22 | "./styles/styles.css", 23 | "./styles/theme.scss" 24 | ], 25 | "stylePreprocessorOptions": { 26 | "includePaths": [ 27 | "styles" 28 | ] 29 | }, 30 | "scripts": [], 31 | "environmentSource": "environments/environment.ts", 32 | "environments": { 33 | "dev": "environments/environment.ts", 34 | "prod": "environments/environment.prod.ts" 35 | } 36 | } 37 | ], 38 | "e2e": { 39 | "protractor": { 40 | "config": "./protractor.conf.js" 41 | } 42 | }, 43 | "lint": [ 44 | { 45 | "project": "src/tsconfig.app.json" 46 | }, 47 | { 48 | "project": "src/tsconfig.spec.json" 49 | }, 50 | { 51 | "project": "e2e/tsconfig.e2e.json" 52 | } 53 | ], 54 | "test": { 55 | "karma": { 56 | "config": "./karma.conf.js" 57 | } 58 | }, 59 | "defaults": { 60 | "styleExt": "css", 61 | "component": {} 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ngrx-firebase", 3 | "version": "0.0.0", 4 | "license": "MIT", 5 | "scripts": { 6 | "ng": "ng", 7 | "start": "ng serve", 8 | "build": "ng build", 9 | "test": "ng test", 10 | "lint": "ng lint", 11 | "e2e": "ng e2e" 12 | }, 13 | "private": true, 14 | "dependencies": { 15 | "@angular/animations": "^4.1.3", 16 | "@angular/common": "^4.0.0", 17 | "@angular/compiler": "^4.0.0", 18 | "@angular/core": "^4.0.0", 19 | "@angular/flex-layout": "^2.0.0-beta.8", 20 | "@angular/forms": "^4.0.0", 21 | "@angular/http": "^4.0.0", 22 | "@angular/material": "^2.0.0-beta.6", 23 | "@angular/platform-browser": "^4.0.0", 24 | "@angular/platform-browser-dynamic": "^4.0.0", 25 | "@angular/router": "^4.0.0", 26 | "@ngrx/core": "^1.2.0", 27 | "@ngrx/effects": "^2.0.3", 28 | "@ngrx/store": "^2.2.2", 29 | "core-js": "^2.4.1", 30 | "rxjs": "^5.1.0", 31 | "zone.js": "^0.8.4" 32 | }, 33 | "devDependencies": { 34 | "@angular/cli": "1.0.0", 35 | "@angular/compiler-cli": "^4.0.0", 36 | "@types/jasmine": "2.5.38", 37 | "@types/node": "~6.0.60", 38 | "codelyzer": "~2.0.0", 39 | "jasmine-core": "~2.5.2", 40 | "jasmine-spec-reporter": "~3.2.0", 41 | "karma": "~1.4.1", 42 | "karma-chrome-launcher": "~2.0.0", 43 | "karma-cli": "~1.0.1", 44 | "karma-jasmine": "~1.1.0", 45 | "karma-jasmine-html-reporter": "^0.2.2", 46 | "karma-coverage-istanbul-reporter": "^0.2.0", 47 | "protractor": "~5.1.0", 48 | "ts-node": "~2.0.0", 49 | "tslint": "~4.5.0", 50 | "typescript": "~2.2.0" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/app/search-results/food-result/food-result.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, OnDestroy } from '@angular/core'; 2 | 3 | import { ActivatedRoute } from '@angular/router'; 4 | import { Observable } from 'rxjs/Observable'; 5 | import { Subject } from 'rxjs/Subject'; 6 | import { Router } from '@angular/router'; 7 | import { Food } from '../../models/food'; 8 | 9 | import { Store } from '@ngrx/store'; 10 | import * as fromRoot from '../.././store/reducer'; 11 | import * as Actions from '../.././store/actions'; 12 | 13 | import 'rxjs/add/operator/takeUntil'; 14 | 15 | @Component({ 16 | selector: 'app-food-result', 17 | templateUrl: './food-result.component.html', 18 | styleUrls: ['./food-result.component.css'] 19 | }) 20 | export class FoodResultComponent implements OnInit, OnDestroy { 21 | 22 | // a subject to manage unsubscription 23 | private destroyed: Subject<{}> = new Subject(); 24 | 25 | food: Observable; 26 | loading: Observable; 27 | 28 | constructor(private route: ActivatedRoute, private router: Router, 29 | private store: Store) { 30 | } 31 | 32 | ngOnInit() { 33 | this.food = this.store.select(state => state.selectedFood); 34 | this.loading = this.store.select(state => state.loading); 35 | 36 | this.route.params 37 | .map(params => params.id) 38 | .takeUntil(this.destroyed) 39 | .do((id) => this.store.dispatch(new Actions.FetchFood(id))) 40 | .subscribe(); 41 | } 42 | 43 | addToList(): void{ 44 | this.store.dispatch(new Actions.AddFood()); 45 | this.router.navigate(['myfoods']); 46 | } 47 | 48 | ngOnDestroy() { 49 | this.destroyed.next(); // emits an action to unsubcsribe from the observable 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/app/store/actions.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Action, Store } from '@ngrx/store'; 3 | import { Food } from '.././models/food'; 4 | import { SearchResult } from '.././models/search-result'; 5 | 6 | // True while fetching data from API 7 | export const LOADING = 'Food Load'; 8 | 9 | // Searching Food via Food Search API 10 | export const SEARCH = 'Food Search'; 11 | export const SEARCH_DONE = 'Food Search Done'; 12 | 13 | // Fetching Food Details via Food Report API 14 | export const FETCH_FOOD = 'Fetch Food'; 15 | export const FETCH_FOOD_DONE = 'Fetch Food Done'; 16 | 17 | // Adding Food to My-Food-list 18 | export const ADD_FOOD = 'Add Food'; 19 | 20 | // Getting Food Details from My-Food-list 21 | export const GET_FOOD = 'Get Food'; 22 | 23 | // Removing Food to My-Food-list 24 | export const REMOVE_FOOD = 'Remove Food'; 25 | 26 | 27 | 28 | 29 | export class Search implements Action { 30 | readonly type = SEARCH; 31 | constructor(public payload: string) { }; 32 | } 33 | 34 | export class SearchDone implements Action { 35 | readonly type = SEARCH_DONE; 36 | constructor(public payload: SearchResult[]) { }; 37 | } 38 | 39 | export class FetchFood implements Action { 40 | readonly type = FETCH_FOOD; 41 | constructor(public payload: string) {} 42 | } 43 | 44 | export class FetchFoodDone implements Action { 45 | readonly type = FETCH_FOOD_DONE; 46 | constructor(public payload: Food) {} 47 | } 48 | 49 | export class AddFood implements Action { 50 | readonly type = ADD_FOOD; 51 | constructor() {} 52 | } 53 | 54 | export class GetFood implements Action { 55 | readonly type = GET_FOOD; 56 | constructor(public payload: string) {} 57 | } 58 | 59 | export class RemoveFood implements Action { 60 | readonly type = REMOVE_FOOD; 61 | constructor(public payload: Food) {} 62 | } 63 | 64 | 65 | export type Actions = Search | SearchDone 66 | | AddFood | RemoveFood 67 | | FetchFood | FetchFoodDone 68 | | GetFood; 69 | -------------------------------------------------------------------------------- /src/app/store/reducer.ts: -------------------------------------------------------------------------------- 1 | import { Food } from '.././models/food'; 2 | import { SearchResult } from '.././models/search-result'; 3 | 4 | 5 | import * as FoodActions from './actions'; 6 | 7 | export interface State { 8 | loading: boolean; 9 | searchTerms: string; 10 | results: SearchResult[]; 11 | selectedFood: Food; 12 | foodList: Food[]; 13 | }; 14 | 15 | const initialState: State = { 16 | loading: false, 17 | searchTerms: '', 18 | results: [], 19 | selectedFood: null, 20 | foodList: [] 21 | } 22 | 23 | export function reducer(state = initialState, action: FoodActions.Actions): State { 24 | switch (action.type) { 25 | case FoodActions.SEARCH: { 26 | return { 27 | ...state, 28 | loading: true, 29 | searchTerms: action.payload 30 | } 31 | } 32 | case FoodActions.SEARCH_DONE: { 33 | return { 34 | ...state, 35 | loading: false, 36 | results: action.payload 37 | } 38 | } 39 | case FoodActions.FETCH_FOOD: { 40 | return { 41 | ...state, 42 | loading: true 43 | } 44 | } 45 | case FoodActions.FETCH_FOOD_DONE: { 46 | return { 47 | ...state, 48 | loading: false, 49 | selectedFood: action.payload 50 | } 51 | } 52 | case FoodActions.ADD_FOOD: { 53 | return { 54 | ...state, 55 | foodList: [...state.foodList, state.selectedFood] 56 | } 57 | } 58 | case FoodActions.GET_FOOD: { 59 | return { 60 | ...state, 61 | selectedFood: state.foodList[action.payload] 62 | } 63 | } 64 | case FoodActions.REMOVE_FOOD: { 65 | return { 66 | ...state, 67 | foodList: state.foodList.filter(food => 68 | food.id !== action.payload.id) 69 | } 70 | } 71 | 72 | default: { 73 | return state; 74 | } 75 | 76 | } 77 | } -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule } from '@angular/core'; 3 | import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 4 | import { HttpModule } from '@angular/http'; 5 | import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; 6 | import { MaterialModule } from './shared/material.module' 7 | import { AppComponent } from './app.component'; 8 | import { FlexLayoutModule } from '@angular/flex-layout'; 9 | import { RouterModule, Routes } from '@angular/router'; 10 | import { StoreModule } from '@ngrx/store'; 11 | import { EffectsModule } from '@ngrx/effects'; 12 | 13 | import { NutritionService } from './services/nutrition.service'; 14 | import { StoreService } from './services/store.service'; 15 | 16 | import { FoodListComponent } from './food-list/food-list.component'; 17 | import { FoodDetailComponent } from './food-detail/food-detail.component'; 18 | import { SearchResultsComponent } from './search-results/search-results.component'; 19 | import { FoodResultComponent } from './search-results/food-result/food-result.component'; 20 | 21 | import { reducer } from './store/reducer'; 22 | import { FoodEffects } from './store/effects'; 23 | import { SearchInputComponent } from './search-input/search-input.component'; 24 | 25 | const routes: Routes = [ 26 | {path: '', pathMatch:'full', redirectTo: 'myfoods' }, 27 | { path: 'search', component: SearchResultsComponent }, 28 | { path: 'search/:id', component: FoodResultComponent}, 29 | { path: 'myfoods', component: FoodListComponent }, 30 | { path: 'myfoods/:id', component: FoodDetailComponent}, 31 | { path: '**', pathMatch: 'full', redirectTo: 'myfoods' } 32 | ]; 33 | 34 | @NgModule({ 35 | declarations: [ 36 | AppComponent, 37 | FoodListComponent, 38 | FoodDetailComponent, 39 | SearchResultsComponent, 40 | FoodResultComponent, 41 | SearchInputComponent 42 | ], 43 | imports: [ 44 | BrowserModule, 45 | FormsModule, 46 | ReactiveFormsModule, 47 | HttpModule, 48 | BrowserAnimationsModule, 49 | MaterialModule, 50 | FlexLayoutModule, 51 | RouterModule.forRoot(routes), 52 | StoreModule.provideStore(reducer), 53 | EffectsModule.run(FoodEffects) 54 | ], 55 | providers: [NutritionService, StoreService], 56 | bootstrap: [AppComponent] 57 | }) 58 | export class AppModule { } 59 | -------------------------------------------------------------------------------- /src/styles/styles.css: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | * { 3 | -webkit-font-smoothing: antialiased; 4 | -moz-osx-font-smoothing: grayscale; 5 | box-sizing: border-box; 6 | } 7 | 8 | html, body { 9 | font: 100%/1.5 Roboto, Helvetica, Arial, sans-serif; 10 | margin: 0; 11 | padding: 0; 12 | background: #fafafa; 13 | color: rgba(0, 0, 0, 0.87); 14 | height: 100vh; 15 | } 16 | 17 | tr { 18 | height: 32px; 19 | } 20 | 21 | th { 22 | font-weight: normal; 23 | } 24 | 25 | th.left { 26 | text-align: left; 27 | } 28 | 29 | th.right { 30 | text-align: right; 31 | font-weight: 500; 32 | } 33 | 34 | .food-name { 35 | font-size: 20px; 36 | text-align: center; 37 | min-height: 40px; 38 | border-bottom: 1px solid; 39 | margin: 24px; 40 | font-weight: 500; 41 | } 42 | .nutrition-facts { 43 | margin: auto; 44 | width: 300px; 45 | } 46 | 47 | 48 | .search-input-container { 49 | width: 100%; 50 | background-color: white; 51 | height: 44px; 52 | margin: auto; 53 | position: relative; 54 | padding: 6px 9px 0; 55 | border-radius: 2px; 56 | box-sizing: border-box; 57 | } 58 | 59 | .search-input { 60 | font-size: 18px; 61 | border: none; 62 | padding: 0px; 63 | margin: 0px; 64 | height: auto; 65 | width: 100%; 66 | background: url(data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D) transparent; 67 | position: absolute; 68 | margin: auto; 69 | z-index: 6; 70 | outline: none; 71 | font: 16px arial,sans-serif; 72 | line-height: 34px; 73 | height: 34px !important; 74 | } 75 | 76 | .search-results { 77 | overflow-y: scroll; 78 | overflow-x: hidden; 79 | } 80 | .search-result-item:hover { 81 | cursor: pointer; 82 | background-color: #E0E0E0; 83 | } 84 | 85 | .search-result-item-name { 86 | max-width: 90vw; 87 | white-space: pre; 88 | overflow: hidden; 89 | text-overflow: ellipsis; 90 | margin-right: auto; 91 | } 92 | 93 | md-list-item span { 94 | max-width: 80vw; 95 | } 96 | 97 | 98 | .subheader { 99 | height: 48px; 100 | padding: 16px 16px 8px; 101 | background: #1565c0; 102 | color: rgba(255, 255, 255, 0.87); 103 | } 104 | 105 | .search-info { 106 | font-size: 16px; 107 | margin: 32px 24px 12px; 108 | font-weight: 400 109 | } 110 | 111 | .search-icon { 112 | font-size:64px; 113 | height:64px; 114 | width: 64px; 115 | color:grey 116 | } -------------------------------------------------------------------------------- /src/app/services/nutrition.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Http, Response } from '@angular/http'; 3 | import { Observable } from 'rxjs/Observable'; 4 | 5 | import 'rxjs/add/operator/catch'; 6 | import 'rxjs/add/operator/map'; 7 | 8 | import { Food } from '../models/food'; 9 | import { SearchResult } from '../models/search-result'; 10 | @Injectable() 11 | export class NutritionService { 12 | apiKey: string; 13 | 14 | constructor(private http: Http) { 15 | this.apiKey = 'KxhNfT3pMwlnn21HUCCs61iG2JwT9EmMrgOnOiU7'; 16 | 17 | } 18 | 19 | searchFood(query: string): Observable { 20 | const url = 'https://api.nal.usda.gov/ndb/search/?format=json&'; 21 | const params: string = [ 22 | `q=${query}`, 23 | `sort=r`, // sort by relevance 24 | `max=25`, // maximum number of results 25 | `offset=0`, // beginning row in the result set to begin 26 | `ds=Standard%20Reference`, // 'Standard Reference' or 'Branded Food Products 27 | `api_key=${this.apiKey}` // Your api key 28 | ].join('&'); 29 | 30 | const queryUrl = `${url}${params}`; 31 | 32 | return this.http.get(queryUrl).map((response: Response) => 33 | response.json().list ? response.json().list.item.map(item => { 34 | return new SearchResult(item); 35 | }) 36 | : []); 37 | } 38 | 39 | fetchFood(query: string): Observable { 40 | const url = 'https://api.nal.usda.gov/ndb/nutrients/?format=json&'; 41 | const params: string = [ 42 | `ndbno=${query}`, 43 | `nutrients=255`, // Water 44 | `nutrients=208`, // Energy 45 | `nutrients=203`, // Protein 46 | `nutrients=204`, // Total lipid 47 | `nutrients=205`, // Carbohydrate 48 | `nutrients=268`, // Energy 49 | `nutrients=269`, // Sugars 50 | `nutrients=291`, // Fiber 51 | `api_key=${this.apiKey}` 52 | ].join('&'); 53 | 54 | const queryUrl = `${url}${params}`; 55 | 56 | return this.http.get(queryUrl) 57 | .map(this.extractData) 58 | .catch(this.handleError) 59 | } 60 | 61 | private extractData(res: Response): Food { 62 | const body = res.json().report.foods[0]; 63 | return new Food(body); 64 | } 65 | 66 | private handleError(error: Response | any) { 67 | let errorMsg: string; 68 | if(error instanceof Response) { 69 | const body = error.json() ||  ''; 70 | const err = body.error ||  JSON.stringify(body); 71 | errorMsg = err; 72 | } else { 73 | errorMsg = error.message ? error.message : error.toString(); 74 | } 75 | return Observable.throw(errorMsg); 76 | } 77 | 78 | } 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/docs/ts/latest/guide/browser-support.html 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/set'; 35 | 36 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 37 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 38 | 39 | /** IE10 and IE11 requires the following to support `@angular/animation`. */ 40 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 41 | 42 | 43 | /** Evergreen browsers require these. **/ 44 | import 'core-js/es6/reflect'; 45 | import 'core-js/es7/reflect'; 46 | 47 | 48 | /** ALL Firefox browsers require the following to support `@angular/animation`. **/ 49 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 50 | 51 | 52 | 53 | /*************************************************************************************************** 54 | * Zone JS is required by Angular itself. 55 | */ 56 | import 'zone.js/dist/zone'; // Included with Angular CLI. 57 | 58 | 59 | 60 | /*************************************************************************************************** 61 | * APPLICATION IMPORTS 62 | */ 63 | 64 | /** 65 | * Date, currency, decimal and percent pipes. 66 | * Needed for: All but Chrome, Firefox, Edge, IE11 and Safari 10 67 | */ 68 | // import 'intl'; // Run `npm install --save intl`. 69 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer" 4 | ], 5 | "rules": { 6 | "callable-types": true, 7 | "class-name": true, 8 | "comment-format": [ 9 | true, 10 | "check-space" 11 | ], 12 | "curly": true, 13 | "eofline": true, 14 | "forin": true, 15 | "import-blacklist": [true, "rxjs"], 16 | "import-spacing": true, 17 | "indent": [ 18 | true, 19 | "spaces" 20 | ], 21 | "interface-over-type-literal": true, 22 | "label-position": true, 23 | "max-line-length": [ 24 | true, 25 | 140 26 | ], 27 | "member-access": false, 28 | "member-ordering": [ 29 | true, 30 | "static-before-instance", 31 | "variables-before-functions" 32 | ], 33 | "no-arg": true, 34 | "no-bitwise": true, 35 | "no-console": [ 36 | true, 37 | "debug", 38 | "info", 39 | "time", 40 | "timeEnd", 41 | "trace" 42 | ], 43 | "no-construct": true, 44 | "no-debugger": true, 45 | "no-duplicate-variable": true, 46 | "no-empty": false, 47 | "no-empty-interface": true, 48 | "no-eval": true, 49 | "no-inferrable-types": [true, "ignore-params"], 50 | "no-shadowed-variable": true, 51 | "no-string-literal": false, 52 | "no-string-throw": true, 53 | "no-switch-case-fall-through": true, 54 | "no-trailing-whitespace": true, 55 | "no-unused-expression": true, 56 | "no-use-before-declare": true, 57 | "no-var-keyword": true, 58 | "object-literal-sort-keys": false, 59 | "one-line": [ 60 | true, 61 | "check-open-brace", 62 | "check-catch", 63 | "check-else", 64 | "check-whitespace" 65 | ], 66 | "prefer-const": true, 67 | "quotemark": [ 68 | true, 69 | "single" 70 | ], 71 | "radix": true, 72 | "semicolon": [ 73 | "always" 74 | ], 75 | "triple-equals": [ 76 | true, 77 | "allow-null-check" 78 | ], 79 | "typedef-whitespace": [ 80 | true, 81 | { 82 | "call-signature": "nospace", 83 | "index-signature": "nospace", 84 | "parameter": "nospace", 85 | "property-declaration": "nospace", 86 | "variable-declaration": "nospace" 87 | } 88 | ], 89 | "typeof-compare": true, 90 | "unified-signatures": true, 91 | "variable-name": false, 92 | "whitespace": [ 93 | true, 94 | "check-branch", 95 | "check-decl", 96 | "check-operator", 97 | "check-separator", 98 | "check-type" 99 | ], 100 | 101 | "directive-selector": [true, "attribute", "app", "camelCase"], 102 | "component-selector": [true, "element", "app", "kebab-case"], 103 | "use-input-property-decorator": true, 104 | "use-output-property-decorator": true, 105 | "use-host-property-decorator": true, 106 | "no-input-rename": true, 107 | "no-output-rename": true, 108 | "use-life-cycle-interface": true, 109 | "use-pipe-transform-interface": true, 110 | "component-class-suffix": true, 111 | "directive-class-suffix": true, 112 | "no-access-missing-member": true, 113 | "templates-use-public": true, 114 | "invoke-injectable": true 115 | } 116 | } 117 | --------------------------------------------------------------------------------