From dbd269a995e12f3d3ece71b519bc0a6203943c64 Mon Sep 17 00:00:00 2001 From: Zak Timson Date: Tue, 24 Jul 2018 22:16:36 -0400 Subject: [PATCH] Added create formulas (Fixes #11) --- .../newComponent/newComponent.component.ts | 4 +- .../newFormula/newFormula.component.html | 58 ++++++++++++++++++- .../newFormula/newFormula.component.ts | 54 ++++++++++++++++- .../viewComponents.component.html | 8 ++- 4 files changed, 118 insertions(+), 6 deletions(-) diff --git a/src/app/formulaManager/newComponent/newComponent.component.ts b/src/app/formulaManager/newComponent/newComponent.component.ts index db2ea22..6492d5c 100644 --- a/src/app/formulaManager/newComponent/newComponent.component.ts +++ b/src/app/formulaManager/newComponent/newComponent.component.ts @@ -25,11 +25,11 @@ export class NewComponentComponent { submit() { let newComponent = {name: this.name, description: this.description, cost: Number(this.cost)}; - if (!this.data) newComponent['created'] = new Date(); if (!this.data) { + newComponent['created'] = new Date(); this.db - .collection('components', ref => ref.orderBy('name')) + .collection('components') .add(newComponent) .then(data => this.dialogRef.close()); } else { diff --git a/src/app/formulaManager/newFormula/newFormula.component.html b/src/app/formulaManager/newFormula/newFormula.component.html index 8318c86..1d77262 100644 --- a/src/app/formulaManager/newFormula/newFormula.component.html +++ b/src/app/formulaManager/newFormula/newFormula.component.html @@ -1 +1,57 @@ -Test \ No newline at end of file + +

+ Update + Create Formula

+ + + + Approved + + + + {{c.name}} + + + + + + + + + g + oz + kg + lb + + + + + + + + + + + + + + + + + + +
NameQuantity
{{c.name}}{{c.quantity | convertFromG: unit}} {{unit}} + +
+
+ Yield: {{total() | convertFromG: unit}} {{unit}} +
+
+ + + \ No newline at end of file diff --git a/src/app/formulaManager/newFormula/newFormula.component.ts b/src/app/formulaManager/newFormula/newFormula.component.ts index a0e4a06..130f795 100644 --- a/src/app/formulaManager/newFormula/newFormula.component.ts +++ b/src/app/formulaManager/newFormula/newFormula.component.ts @@ -1,15 +1,67 @@ import {Component, Inject} from '@angular/core'; import {MatDialogRef, MAT_DIALOG_DATA} from '@angular/material'; import {AngularFirestore} from 'angularfire2/firestore'; +import {LocalStorage} from '../../../../node_modules/webstorage-decorators'; +import {AppStore} from '../../app.store'; +import {ConvertToGPipe} from '../units.pipe'; @Component({ selector: 'new-formula', templateUrl: './newFormula.component.html' }) export class NewFormulaComponent { + name: string; + amount: number; + approved: boolean = false; + component: string; + components: {component: string; name: string; quantity: number}[] = []; + componentsList = []; + @LocalStorage({defaultValue: 'kg', fieldName: 'newFormulaUnit'}) + unit; + constructor( private dialogRef: MatDialogRef, private db: AngularFirestore, + private store: AppStore, @Inject(MAT_DIALOG_DATA) public data - ) {} + ) { + this.store.components.subscribe(rows => (this.componentsList = rows)); + } + + add() { + let id = this.componentsList.filter(row => row.name == this.component)[0].id; + console.log(id); + let amount = new ConvertToGPipe().transform(Number(this.amount), this.unit); + this.components.push({component: id, name: this.component, quantity: amount}); + this.component = null; + this.amount = null; + } + + remove(i) { + this.components.splice(i, 1); + } + + submit() { + let newFormula = { + name: this.name, + approved: this.approved, + components: this.components.map((row: any) => { + return {component: this.db.collection('components').doc(row.component).ref, quantity: row.quantity}; + }) + }; + + if (!this.data) { + newFormula['created'] = new Date(); + this.db + .collection('formulas') + .add(newFormula) + .then(data => this.dialogRef.close()); + } else { + this.data.ref.update(newFormula).then(data => this.dialogRef.close()); + } + } + + total() { + return this.components.reduce((acc, row) => acc + row.quantity, 0); + } } diff --git a/src/app/formulaManager/viewComponents/viewComponents.component.html b/src/app/formulaManager/viewComponents/viewComponents.component.html index d2de407..15c9a90 100644 --- a/src/app/formulaManager/viewComponents/viewComponents.component.html +++ b/src/app/formulaManager/viewComponents/viewComponents.component.html @@ -18,10 +18,14 @@ {{c.created | date}} {{c.cost | currency}} - edit + - delete +