-
{{user.email}}
+
+
+ {{store.user.email}}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts
index c89e335..450c83a 100644
--- a/src/app/app.component.ts
+++ b/src/app/app.component.ts
@@ -1,12 +1,12 @@
import {Component, OnInit} from '@angular/core';
import {Router, NavigationEnd} from '@angular/router';
import {ElectronService} from 'ngx-electron';
-import {AngularFirestore} from 'angularfire2/firestore';
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';
+import {AppStore} from './app.store';
+import {AngularFireAuth} from '../../node_modules/angularfire2/auth';
@Component({
selector: 'app-root',
@@ -16,29 +16,29 @@ export class AppComponent implements OnInit {
@LocalStorage({defaultValue: []})
cart: {id: string; item: string; price: number; currency: 'CAD' | 'USD'; quantity: number}[];
- categories;
- user;
-
constructor(
private router: Router,
- private db: AngularFirestore,
private dialog: MatDialog,
- public afAuth: AngularFireAuth,
- public electron: ElectronService
- ) {
- this.categories = this.db.collection('categories').valueChanges();
- }
+ public electron: ElectronService,
+ public store: AppStore,
+ public afAuth: AngularFireAuth
+ ) {}
- addToCart(id: string, item: string, price: number, currency: 'CAD' | 'USD', quantity: number) {
+ cartAdd(id: string, item: string, price: number, currency: 'CAD' | 'USD', quantity: number) {
this.cart = [{id: id, item: item, price: Number(price), currency: currency, quantity: Number(quantity)}].concat(
this.cart
);
}
- cartItemCount() {
+ cartCount() {
return this.cart.map(row => row.quantity).reduce((acc, row) => acc + row, 0);
}
+ cartRemove(i) {
+ let temp = this.cart;
+ this.cart = temp.slice(i, 1);
+ }
+
login() {
this.dialog.open(LoginComponent);
}
@@ -57,9 +57,5 @@ export class AppComponent implements OnInit {
if (this.electron.isElectronApp) {
this.router.navigate(['/formulaManager']);
}
-
- this.afAuth.user.subscribe(user => {
- this.user = user;
- });
}
}
diff --git a/src/app/app.module.ts b/src/app/app.module.ts
index 6fd2852..dd9f89f 100644
--- a/src/app/app.module.ts
+++ b/src/app/app.module.ts
@@ -15,7 +15,7 @@ import {ServiceWorkerModule} from '@angular/service-worker';
import {FormulaManagerComponent} from './formulaManager/formulaManager.component';
import {NgxElectronModule} from 'ngx-electron';
import {AboutComponent} from './about/about.component';
-import {CategoriesComponent} from './store/categories/categories.component';
+import {CategoriesComponent} from './store/categories.component';
import {AngularFireStorageModule} from 'angularfire2/storage';
import {LoginComponent} from './login/login.component';
import {AngularFireAuthModule} from 'angularfire2/auth';
@@ -27,6 +27,8 @@ import {CartComponent} from './store/cart/cart.component';
import {ViewComponents} from './formulaManager/viewComponents/viewComponents.component';
import {NewComponentComponent} from './formulaManager/newComponent/newComponent.component';
import {HttpModule} from '@angular/http';
+import {NewFormulaComponent} from './formulaManager/newFormula/newFormula.component';
+import {AppStore} from './app.store';
@NgModule({
declarations: [
@@ -42,6 +44,7 @@ import {HttpModule} from '@angular/http';
LoginComponent,
NewCategoryComponent,
NewComponentComponent,
+ NewFormulaComponent,
NewProductComponent,
ProductsComponent,
ScalePipe,
@@ -70,12 +73,13 @@ import {HttpModule} from '@angular/http';
]),
ServiceWorkerModule.register('/ngsw-worker.js', {enabled: environment.production})
],
- providers: [],
+ providers: [AppStore],
entryComponents: [
DeleteComponent,
LoginComponent,
NewCategoryComponent,
NewComponentComponent,
+ NewFormulaComponent,
NewProductComponent,
ViewComponents
],
diff --git a/src/app/app.store.ts b/src/app/app.store.ts
new file mode 100644
index 0000000..86b6d88
--- /dev/null
+++ b/src/app/app.store.ts
@@ -0,0 +1,90 @@
+import {Injectable} from '@angular/core';
+import {AngularFirestore} from 'angularfire2/firestore';
+import {Category} from './store/category';
+import {Observable, combineLatest} from 'rxjs';
+import {map, shareReplay} from 'rxjs/operators';
+import {DomSanitizer} from '@angular/platform-browser';
+import {Product} from './store/product';
+import {AngularFireAuth} from '../../node_modules/angularfire2/auth';
+import {Component} from './formulaManager/component';
+import {Formula} from './formulaManager/formula';
+
+@Injectable()
+export class AppStore {
+ categories: Observable
;
+ components: Observable;
+ formulas: Observable;
+ products: Observable;
+ user: any;
+
+ constructor(private domSanitizer: DomSanitizer, private db: AngularFirestore, private auth: AngularFireAuth) {
+ this.categories = this.db
+ .collection('categories', row => row.orderBy('name'))
+ .snapshotChanges()
+ .pipe(
+ map(rows =>
+ rows.map((row: any) => {
+ let temp = Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data());
+ temp.image = this.domSanitizer.bypassSecurityTrustUrl(temp.image);
+ return temp;
+ })
+ ),
+ shareReplay(1)
+ );
+
+ this.components = this.db
+ .collection('components', row => row.orderBy('name'))
+ .snapshotChanges()
+ .pipe(
+ map(rows =>
+ rows.map((row: any) => {
+ let temp = Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data());
+ temp.created = temp.created.toDate();
+ return temp;
+ })
+ ),
+ shareReplay(1)
+ );
+
+ this.formulas = combineLatest(
+ this.db.collection('formulas', row => row.orderBy('name')).snapshotChanges(),
+ this.components
+ ).pipe(
+ map(data =>
+ data[0].map(row => {
+ let temp = Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data());
+ temp.created = temp.created.toDate();
+
+ let newComponents = [];
+ temp.components.forEach((row: any) => {
+ let c = data[1].filter(c => c.id == row.component.id)[0];
+ newComponents.push({component: c, quantity: row.quantity});
+ });
+ temp.components = newComponents;
+
+ return temp;
+ })
+ ),
+ shareReplay(1)
+ );
+
+ this.products = this.db
+ .collection('products', row => row.orderBy('name'))
+ .snapshotChanges()
+ .pipe(
+ map(rows =>
+ rows.map((row: any) => {
+ let temp = Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data());
+ temp.image = this.domSanitizer.bypassSecurityTrustUrl(temp.image);
+ temp.description = this.domSanitizer.bypassSecurityTrustHtml(
+ temp.description.replace(/(\r\n|\r|\n)/g, '
')
+ );
+ return temp;
+ })
+ ),
+ shareReplay(1)
+ );
+
+ this.auth.user.subscribe(row => (this.user = row));
+ }
+}
diff --git a/src/app/formulaManager/component.ts b/src/app/formulaManager/component.ts
new file mode 100644
index 0000000..934925b
--- /dev/null
+++ b/src/app/formulaManager/component.ts
@@ -0,0 +1,10 @@
+import {DocumentReference} from 'angularfire2/firestore';
+
+export interface Component {
+ cost: number;
+ created: Date;
+ description: string;
+ id: string;
+ name: string;
+ ref: DocumentReference;
+}
diff --git a/src/app/formulaManager/formula.ts b/src/app/formulaManager/formula.ts
new file mode 100644
index 0000000..16689c3
--- /dev/null
+++ b/src/app/formulaManager/formula.ts
@@ -0,0 +1,11 @@
+import {Component} from '@angular/core';
+import {DocumentReference} from 'angularfire2/firestore';
+
+export interface Formula {
+ approved: boolean;
+ components: {component: Component; quantity: number}[];
+ created: Date;
+ id: string;
+ name: string;
+ ref: DocumentReference;
+}
diff --git a/src/app/formulaManager/formulaManager.component.html b/src/app/formulaManager/formulaManager.component.html
index f9be6f6..ccb910e 100644
--- a/src/app/formulaManager/formulaManager.component.html
+++ b/src/app/formulaManager/formulaManager.component.html
@@ -1,11 +1,11 @@
-
+
-
@@ -18,7 +18,7 @@
search
-
+
0">
{{f.name}}
diff --git a/src/app/formulaManager/formulaManager.component.ts b/src/app/formulaManager/formulaManager.component.ts
index aee8119..401b7d3 100644
--- a/src/app/formulaManager/formulaManager.component.ts
+++ b/src/app/formulaManager/formulaManager.component.ts
@@ -6,21 +6,17 @@ import {LocalStorage} from 'webstorage-decorators';
import {MatDialog} from '@angular/material';
import {ViewComponents} from './viewComponents/viewComponents.component';
import {AngularFireAuth} from 'angularfire2/auth';
+import {NewFormulaComponent} from './newFormula/newFormula.component';
+import {AppStore} from '../app.store';
@Component({
selector: 'formula-manager',
templateUrl: './formulaManager.component.html'
})
export class FormulaManagerComponent {
- @ViewChildren('cost') componentCosts: ElementRef[];
-
- formulas;
formula;
- installPrompt;
- components;
@LocalStorage({defaultValue: 'g'})
unit;
- user;
_newTotal: number = 0;
get newTotal() {
@@ -30,36 +26,13 @@ export class FormulaManagerComponent {
this._newTotal = new ConvertToGPipe().transform(total, this.unit);
}
- constructor(
- private db: AngularFirestore,
- public electron: ElectronService,
- private dialog: MatDialog,
- private afAuth: AngularFireAuth
- ) {
- this.formulas = this.db.collection('formulas', ref => ref.orderBy('name')).valueChanges();
- this.afAuth.user.subscribe(user => {
- this.user = user;
- });
- }
-
- create(row: string) {
- let data = new RegExp(/(.+?)\t(.+?)\t(.+?)\t\$(\d\.\d\d)\s*?(\w.*)/).exec(row);
- this.db.collection('components').add({
- name: data[1],
- vendor: 'GCm9FzeJ8NNpBl6G9BCu',
- description: data[3],
- cost: data[4],
- created: new Date(data[5])
- });
- }
+ constructor(public electron: ElectronService, private dialog: MatDialog, public store: AppStore) {}
openComponents() {
this.dialog.open(ViewComponents, {height: '500px'});
}
cost() {
- if (!this.formula || this.formula.components.filter(row => typeof row.component.get == 'function').length > 0)
- return 0;
let cost = 0;
this.formula.components.forEach(
row => (cost += (((row.quantity / this.formula.total) * this._newTotal) / 1000) * row.component.cost)
@@ -68,21 +41,12 @@ export class FormulaManagerComponent {
}
displayFormula(formula) {
- formula.components
- .filter(row => typeof row.component.get == 'function')
- .forEach((row, i, arr) => row.component.get().then(row => (arr[i].component = row.data())));
formula.total = formula.components.reduce((acc, row) => (acc += row.quantity), 0);
this.newTotal = new ConvertFromGPipe().transform(formula.total, this.unit);
this.formula = formula;
}
- prompt() {
- if (this.installPrompt) this.installPrompt.prompt();
- }
-
- @HostListener('beforeinstallprompt', ['$event'])
- setPrompt(e) {
- this.installPrompt = e;
- this.installPrompt.preventDefault();
+ newFormula() {
+ this.dialog.open(NewFormulaComponent);
}
}
diff --git a/src/app/formulaManager/newFormula/newFormula.component.html b/src/app/formulaManager/newFormula/newFormula.component.html
new file mode 100644
index 0000000..8318c86
--- /dev/null
+++ b/src/app/formulaManager/newFormula/newFormula.component.html
@@ -0,0 +1 @@
+Test
\ No newline at end of file
diff --git a/src/app/formulaManager/newFormula/newFormula.component.ts b/src/app/formulaManager/newFormula/newFormula.component.ts
new file mode 100644
index 0000000..a0e4a06
--- /dev/null
+++ b/src/app/formulaManager/newFormula/newFormula.component.ts
@@ -0,0 +1,15 @@
+import {Component, Inject} from '@angular/core';
+import {MatDialogRef, MAT_DIALOG_DATA} from '@angular/material';
+import {AngularFirestore} from 'angularfire2/firestore';
+
+@Component({
+ selector: 'new-formula',
+ templateUrl: './newFormula.component.html'
+})
+export class NewFormulaComponent {
+ constructor(
+ private dialogRef: MatDialogRef,
+ private db: AngularFirestore,
+ @Inject(MAT_DIALOG_DATA) public data
+ ) {}
+}
diff --git a/src/app/formulaManager/viewComponents/viewComponents.component.html b/src/app/formulaManager/viewComponents/viewComponents.component.html
index d2267d6..d2de407 100644
--- a/src/app/formulaManager/viewComponents/viewComponents.component.html
+++ b/src/app/formulaManager/viewComponents/viewComponents.component.html
@@ -12,7 +12,7 @@
-
+
{{c.name}} |
{{c.description}} |
{{c.created | date}} |
diff --git a/src/app/formulaManager/viewComponents/viewComponents.component.ts b/src/app/formulaManager/viewComponents/viewComponents.component.ts
index 20bd6eb..7cf23a3 100644
--- a/src/app/formulaManager/viewComponents/viewComponents.component.ts
+++ b/src/app/formulaManager/viewComponents/viewComponents.component.ts
@@ -4,32 +4,18 @@ import {map} from 'rxjs/operators';
import {MatDialog} from '@angular/material';
import {DeleteComponent} from '../../delete/delete.component';
import {NewComponentComponent} from '../newComponent/newComponent.component';
+import {AppStore} from '../../app.store';
@Component({
selector: '',
templateUrl: './viewComponents.component.html'
})
export class ViewComponents {
- components;
-
- constructor(private db: AngularFirestore, private dialog: MatDialog) {
- this.components = this.db
- .collection('components')
- .snapshotChanges()
- .pipe(
- map(rows =>
- rows.map((row: any) => {
- row = Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data());
- row.created = row.created.toDate();
- return row;
- })
- )
- );
- }
+ constructor(private dialog: MatDialog, public store: AppStore) {}
createComponent(component?) {
- if(component) {
- this.dialog.open(NewComponentComponent, { data: component })
+ if (component) {
+ this.dialog.open(NewComponentComponent, {data: component});
} else {
this.dialog.open(NewComponentComponent);
}
diff --git a/src/app/store/cart/cart.component.html b/src/app/store/cart/cart.component.html
index 93fabe2..283ce62 100644
--- a/src/app/store/cart/cart.component.html
+++ b/src/app/store/cart/cart.component.html
@@ -11,14 +11,14 @@
-
+
{{i + 1}} |
{{item.item}}
|
{{item.quantity}} |
{{item.currency}} {{item.price | currency}} |
-
+ |
delete
@@ -26,6 +26,6 @@
|
-
+
\ No newline at end of file
diff --git a/src/app/store/cart/cart.component.ts b/src/app/store/cart/cart.component.ts
index 134df7e..eb6ba0b 100644
--- a/src/app/store/cart/cart.component.ts
+++ b/src/app/store/cart/cart.component.ts
@@ -1,27 +1,21 @@
import {Component} from '@angular/core';
-import {LocalStorage} from 'webstorage-decorators';
-import {access} from 'fs';
-import {Http} from '../../../../node_modules/@angular/http';
-import {Router} from '../../../../node_modules/@angular/router';
+import {AppComponent} from '../../app.component';
@Component({
selector: 'cart',
templateUrl: 'cart.component.html'
})
export class CartComponent {
- @LocalStorage({defaultValue: []})
- cart: {id: string; item: string; price: number; curency: 'CAD' | 'USD'; quantity: number}[];
-
address1: string;
address2: string;
city: string;
province: string;
postal: string;
- constructor(private http: Http, private router: Router) {}
+ constructor(public app: AppComponent) {}
ngOnInit() {
- if (this.cart.length > 0) {
+ if (this.app.cartCount()) {
window['paypal'].Button.render(
{
env: 'sandbox',
@@ -43,15 +37,4 @@ export class CartComponent {
);
}
}
-
- remove(i: number) {
- console.log('fire');
- let c = this.cart;
- c.splice(i, 1);
- this.cart = c;
- }
-
- total() {
- return this.cart.reduce((acc, row) => acc + row.price * row.quantity, 0);
- }
}
diff --git a/src/app/store/categories/categories.component.html b/src/app/store/categories.component.html
similarity index 92%
rename from src/app/store/categories/categories.component.html
rename to src/app/store/categories.component.html
index 8ae59d5..2fd8341 100644
--- a/src/app/store/categories/categories.component.html
+++ b/src/app/store/categories.component.html
@@ -2,7 +2,7 @@
-
+
playlist_add Category
@@ -18,27 +18,7 @@
-
-
-
-
-
-
- {{c.name}}
-
-
-
-
- edit
-
-
- delete
-
-
-
-
-
-
+
@@ -51,6 +31,26 @@
+
+
+
+
+
+
+ {{c.name}}
+
+
+
+
+ edit
+
+
+ delete
+
+
+
+
+
@@ -60,7 +60,7 @@
{{p.name}}
-
+
edit
diff --git a/src/app/store/categories.component.ts b/src/app/store/categories.component.ts
new file mode 100644
index 0000000..8cf8196
--- /dev/null
+++ b/src/app/store/categories.component.ts
@@ -0,0 +1,51 @@
+import {Component} from '@angular/core';
+import {map} from 'rxjs/operators';
+import {ActivatedRoute} from '@angular/router';
+import {MatDialog} from '@angular/material';
+import {NewCategoryComponent} from './newCategory/newCategory.component';
+import {AppComponent} from '../app.component';
+import {NewProductComponent} from './newProduct/newProduct.component';
+import {DeleteComponent} from '../delete/delete.component';
+import {AppStore} from '../app.store';
+import {Category} from './category';
+import {Observable} from 'rxjs';
+import {Product} from './product';
+
+@Component({
+ selector: 'store',
+ templateUrl: 'categories.component.html'
+})
+export class CategoriesComponent {
+ category: string;
+ categories: Observable;
+ products: Observable;
+
+ constructor(
+ private store: AppStore,
+ private route: ActivatedRoute,
+ private dialog: MatDialog,
+ public app: AppComponent
+ ) {}
+
+ ngOnInit() {
+ this.route.params.subscribe(params => {
+ this.category = params['category'];
+
+ this.categories = this.store.categories.pipe(map(rows => rows.filter(row => row.parent == this.category)));
+
+ this.products = this.store.products.pipe(map(rows => rows.filter(row => row.category == this.category)));
+ });
+ }
+
+ createCategory(category) {
+ this.dialog.open(NewCategoryComponent, {data: {category: category, currentCategory: this.category}});
+ }
+
+ createProduct(product) {
+ this.dialog.open(NewProductComponent, {data: {product: product, currentCategory: this.category}});
+ }
+
+ delete(obj) {
+ this.dialog.open(DeleteComponent, {data: obj});
+ }
+}
diff --git a/src/app/store/categories/categories.component.ts b/src/app/store/categories/categories.component.ts
deleted file mode 100644
index ff48392..0000000
--- a/src/app/store/categories/categories.component.ts
+++ /dev/null
@@ -1,81 +0,0 @@
-import {Component} from '@angular/core';
-import {AngularFirestore} from 'angularfire2/firestore';
-import {map} from 'rxjs/operators';
-import {ActivatedRoute, Router} from '@angular/router';
-import {MatDialog} from '@angular/material';
-import {NewCategoryComponent} from '../newCategory/newCategory.component';
-import {AppComponent} from '../../app.component';
-import {DomSanitizer} from '@angular/platform-browser';
-import {NewProductComponent} from '../newProduct/newProduct.component';
-import {DeleteComponent} from '../../delete/delete.component';
-
-@Component({
- selector: 'store',
- templateUrl: 'categories.component.html'
-})
-export class CategoriesComponent {
- category: string;
- categories;
- products;
-
- constructor(
- private db: AngularFirestore,
- private router: Router,
- private route: ActivatedRoute,
- private dialog: MatDialog,
- private domSanitizer: DomSanitizer,
- public app: AppComponent
- ) {}
-
- ngOnInit() {
- this.route.params.subscribe(params => {
- this.category = params['category'];
-
- this.categories = this.db
- .collection('categories', ref => ref.orderBy('name'))
- .snapshotChanges()
- .pipe(
- map(rows =>
- rows
- .map((row: any) =>
- Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data())
- )
- .filter((row: any) => (!this.category && !row.parent) || (this.category && row.parent == this.category))
- .map((row: any) => {
- row.image = this.domSanitizer.bypassSecurityTrustUrl(row.image);
- return row;
- })
- )
- );
-
- this.products = this.db
- .collection('products', ref => ref.orderBy('name'))
- .snapshotChanges()
- .pipe(
- map(rows =>
- rows
- .map((row: any) =>
- Object.assign({id: row.payload.doc.id, ref: row.payload.doc.ref}, row.payload.doc.data())
- )
- .filter((row: any) => row.category == this.category)
- .map((row: any) => {
- row.image = this.domSanitizer.bypassSecurityTrustUrl(row.image);
- return row;
- })
- )
- );
- });
- }
-
- createCategory(category) {
- this.dialog.open(NewCategoryComponent, {data: {category: category, currentCategory: this.category}});
- }
-
- createProduct(product) {
- this.dialog.open(NewProductComponent, {data: {product: product, currentCategory: this.category}});
- }
-
- delete(obj) {
- this.dialog.open(DeleteComponent, {data: obj});
- }
-}
diff --git a/src/app/store/category.ts b/src/app/store/category.ts
new file mode 100644
index 0000000..c16fb06
--- /dev/null
+++ b/src/app/store/category.ts
@@ -0,0 +1,10 @@
+import {DocumentReference} from 'angularfire2/firestore';
+import {SafeUrl} from '@angular/platform-browser';
+
+export interface Category {
+ image: SafeUrl;
+ id: string;
+ name: string;
+ parent: string;
+ ref: DocumentReference;
+}
diff --git a/src/app/store/newCategory/newCategory.component.html b/src/app/store/newCategory/newCategory.component.html
index 6a5ea0c..9f4af58 100644
--- a/src/app/store/newCategory/newCategory.component.html
+++ b/src/app/store/newCategory/newCategory.component.html
@@ -9,7 +9,7 @@
root
- {{c.name}}
+ {{c.name}}
diff --git a/src/app/store/newCategory/newCategory.component.ts b/src/app/store/newCategory/newCategory.component.ts
index a163370..aa4bfda 100644
--- a/src/app/store/newCategory/newCategory.component.ts
+++ b/src/app/store/newCategory/newCategory.component.ts
@@ -1,6 +1,7 @@
import {Component, ViewChild, Inject} from '@angular/core';
import {AngularFirestore} from 'angularfire2/firestore';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
+import {AppStore} from '../../app.store';
@Component({
selector: 'new-category',
@@ -9,7 +10,6 @@ import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
export class NewCategoryComponent {
@ViewChild('fileInput') fileInput;
- categories;
parent: string = 'root';
name: string;
image: string;
@@ -17,9 +17,9 @@ export class NewCategoryComponent {
constructor(
private dialogRef: MatDialogRef,
private db: AngularFirestore,
- @Inject(MAT_DIALOG_DATA) public data
+ @Inject(MAT_DIALOG_DATA) public data,
+ public store: AppStore
) {
- this.categories = this.db.collection('categories', ref => ref.orderBy('name')).valueChanges();
if (data.currentCategory) this.parent = data.currentCategory;
if (data.category) {
diff --git a/src/app/store/newProduct/newProduct.component.html b/src/app/store/newProduct/newProduct.component.html
index 64804f9..32597fb 100644
--- a/src/app/store/newProduct/newProduct.component.html
+++ b/src/app/store/newProduct/newProduct.component.html
@@ -8,7 +8,7 @@
- {{c.name}}
+ {{c.name}}
diff --git a/src/app/store/newProduct/newProduct.component.ts b/src/app/store/newProduct/newProduct.component.ts
index 7de74f2..e53db50 100644
--- a/src/app/store/newProduct/newProduct.component.ts
+++ b/src/app/store/newProduct/newProduct.component.ts
@@ -1,6 +1,7 @@
import {Component, ViewChild, Inject} from '@angular/core';
import {AngularFirestore} from 'angularfire2/firestore';
import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
+import {AppStore} from '../../app.store';
@Component({
selector: 'new-item',
@@ -9,7 +10,6 @@ import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material';
export class NewProductComponent {
@ViewChild('fileInput') fileInput;
- categories;
category;
currency = 'CAD';
description: string;
@@ -20,9 +20,9 @@ export class NewProductComponent {
constructor(
private dialogRef: MatDialogRef,
private db: AngularFirestore,
- @Inject(MAT_DIALOG_DATA) public data
+ @Inject(MAT_DIALOG_DATA) public data,
+ public store: AppStore
) {
- this.categories = this.db.collection('categories', ref => ref.orderBy('name')).valueChanges();
if (data.currentCategory) this.category = data.currentCategory;
if (data.product) {
diff --git a/src/app/store/product.ts b/src/app/store/product.ts
new file mode 100644
index 0000000..b25f5b0
--- /dev/null
+++ b/src/app/store/product.ts
@@ -0,0 +1,13 @@
+import {DocumentReference} from 'angularfire2/firestore';
+import {SafeUrl, SafeHtml} from '@angular/platform-browser';
+
+export interface Product {
+ category: string;
+ currency: 'CAD' | 'USD';
+ description: SafeHtml;
+ id: string;
+ image: SafeUrl;
+ name: string;
+ price: number;
+ ref: DocumentReference;
+}
diff --git a/src/app/store/products/products.component.html b/src/app/store/products/products.component.html
index 5084457..8ca91bd 100644
--- a/src/app/store/products/products.component.html
+++ b/src/app/store/products/products.component.html
@@ -7,7 +7,7 @@
-
+
add_shopping_cart Buy
diff --git a/src/app/store/products/products.component.ts b/src/app/store/products/products.component.ts
index ff3b752..31f8feb 100644
--- a/src/app/store/products/products.component.ts
+++ b/src/app/store/products/products.component.ts
@@ -1,9 +1,8 @@
import {Component} from '@angular/core';
-import {AngularFirestore} from 'angularfire2/firestore';
import {ActivatedRoute} from '@angular/router';
-import {DomSanitizer} from '@angular/platform-browser';
import {AppComponent} from '../../app.component';
import {map} from 'rxjs/operators';
+import {AppStore} from '../../app.store';
@Component({
selector: 'products',
@@ -12,32 +11,13 @@ import {map} from 'rxjs/operators';
export class ProductsComponent {
product;
- constructor(
- private route: ActivatedRoute,
- private db: AngularFirestore,
- private domSanitizer: DomSanitizer,
- public app: AppComponent
- ) {}
+ constructor(private store: AppStore, private route: ActivatedRoute, public app: AppComponent) {}
ngOnInit() {
this.route.params.subscribe(params => {
- this.db
- .collection('products', ref => ref.where('name', '==', params['product']))
- .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.store.products.pipe(map(rows => rows.filter(row => row.name == params['product']))).subscribe(data => {
+ this.product = data[0];
+ });
});
}
}