From 4924a725092a6751c7c81a950694bfb3689e6beb Mon Sep 17 00:00:00 2001 From: Zak Timson Date: Tue, 17 Jul 2018 23:10:38 -0400 Subject: [PATCH] Added basic cart system --- src/app/app.component.html | 9 ++++-- src/app/app.component.ts | 20 +++++++++--- .../store/products/products.component.html | 8 +++++ src/app/store/products/products.component.ts | 32 +++++++++++++------ 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index 6275282..0e75bae 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -25,10 +25,11 @@
-
+
{{user.email}}
+ shopping_cart
@@ -74,7 +75,8 @@
CONTACT

416-744-2723 -
416-744-4078 +
+ 416-744-4078
Toll Free: 1-888-422-7737

@@ -84,7 +86,8 @@ diff --git a/src/app/app.component.ts b/src/app/app.component.ts index adf28dc..29e41d0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -6,12 +6,16 @@ import {filter} from 'rxjs/operators'; import {MatDialog} from '@angular/material'; import {LoginComponent} from './login/login.component'; import {AngularFireAuth} from 'angularfire2/auth'; +import {LocalStorage} from 'webstorage-decorators'; @Component({ selector: 'app-root', templateUrl: 'app.component.html' }) export class AppComponent implements OnInit { + @LocalStorage({defaultValue: []}) + cart: {id: string; item: string; price: number; quantity: number}[]; + categories; user; @@ -25,6 +29,18 @@ export class AppComponent implements OnInit { this.categories = this.db.collection('categories').valueChanges(); } + addToCart(id: string, item: string, price: number, quantity: number) { + this.cart = [{id: id, item: item, price: Number(price), quantity: Number(quantity)}].concat(this.cart); + } + + cartItemCount() { + return this.cart.map(row => row.quantity).reduce((acc, row) => acc + row, 0); + } + + login() { + this.dialog.open(LoginComponent); + } + ngOnInit() { // Record routing for analytics this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe((event: NavigationEnd) => { @@ -44,8 +60,4 @@ export class AppComponent implements OnInit { this.user = user; }); } - - login() { - this.dialog.open(LoginComponent); - } } diff --git a/src/app/store/products/products.component.html b/src/app/store/products/products.component.html index 71fefea..db532e9 100644 --- a/src/app/store/products/products.component.html +++ b/src/app/store/products/products.component.html @@ -3,6 +3,14 @@
+
+ + + + +

{{product.name}}

{{product.price | currency}}
Contact For Price
diff --git a/src/app/store/products/products.component.ts b/src/app/store/products/products.component.ts index d3c107e..88388e6 100644 --- a/src/app/store/products/products.component.ts +++ b/src/app/store/products/products.component.ts @@ -1,7 +1,9 @@ import {Component} from '@angular/core'; -import {AngularFirestore} from '../../../../node_modules/angularfire2/firestore'; -import {ActivatedRoute} from '../../../../node_modules/@angular/router'; -import {DomSanitizer} from '../../../../node_modules/@angular/platform-browser'; +import {AngularFirestore} from 'angularfire2/firestore'; +import {ActivatedRoute} from '@angular/router'; +import {DomSanitizer} from 'node_modules/@angular/platform-browser'; +import {AppComponent} from '../../app.component'; +import {map} from 'rxjs/operators'; @Component({ selector: 'products', @@ -10,19 +12,31 @@ import {DomSanitizer} from '../../../../node_modules/@angular/platform-browser'; export class ProductsComponent { product; - constructor(private route: ActivatedRoute, private db: AngularFirestore, private domSanitizer: DomSanitizer) {} + constructor( + private route: ActivatedRoute, + private db: AngularFirestore, + private domSanitizer: DomSanitizer, + public app: AppComponent + ) {} ngOnInit() { this.route.params.subscribe(params => { this.db .collection('products', ref => ref.where('name', '==', params['product'])) - .valueChanges() + .snapshotChanges() + .pipe( + map(rows => + rows.map((row: any) => Object.assign({id: row.payload.doc.id}, row.payload.doc.data())).map((row: any) => { + row.image = this.domSanitizer.bypassSecurityTrustUrl(row.image); + row.description = this.domSanitizer.bypassSecurityTrustHtml( + row.description.replace(/(\r\n|\r|\n)/g, '
') + ); + return row; + }) + ) + ) .subscribe(data => { this.product = data[0]; - this.product.image = this.domSanitizer.bypassSecurityTrustUrl(this.product.image); - this.product.description = this.domSanitizer.bypassSecurityTrustHtml( - this.product.description.replace(/(\r\n|\r|\n)/g, '
') - ); }); }); }