2018-06-25 11:19:17 -04:00
|
|
|
import {Component} from '@angular/core';
|
|
|
|
import {AngularFirestore} from 'angularfire2/firestore';
|
2018-06-25 00:50:47 -04:00
|
|
|
import {share} from 'rxjs/operators';
|
2018-06-25 11:19:17 -04:00
|
|
|
import {ConvertFromGPipe, ConvertToGPipe} from './units.pipe';
|
2018-06-24 21:11:31 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-root',
|
|
|
|
templateUrl: './app.component.html',
|
|
|
|
styleUrls: ['./app.component.css']
|
|
|
|
})
|
|
|
|
export class AppComponent {
|
2018-06-25 00:50:47 -04:00
|
|
|
formulas;
|
|
|
|
formula;
|
|
|
|
components;
|
|
|
|
unit = 'g';
|
2018-06-25 11:19:17 -04:00
|
|
|
|
|
|
|
_newTotal: number = 0;
|
|
|
|
get newTotal() {
|
|
|
|
return new ConvertFromGPipe().transform(this._newTotal, this.unit);
|
|
|
|
}
|
|
|
|
set newTotal(total) {
|
|
|
|
this._newTotal = new ConvertToGPipe().transform(total, this.unit);
|
|
|
|
}
|
2018-06-25 00:50:47 -04:00
|
|
|
|
|
|
|
constructor(private db: AngularFirestore) {
|
|
|
|
this.formulas = this.db.collection('formulas').valueChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
displayFormula(formula) {
|
2018-06-25 11:19:17 -04:00
|
|
|
formula.components.map(
|
|
|
|
row =>
|
|
|
|
(row.component = this.db
|
|
|
|
.doc(`components/${row.component.id}`)
|
|
|
|
.valueChanges()
|
|
|
|
.pipe(share()))
|
|
|
|
);
|
|
|
|
formula.total = formula.components.reduce((acc, row) => (acc += row.quantity), 0);
|
2018-06-25 00:50:47 -04:00
|
|
|
this.newTotal = formula.total;
|
|
|
|
this.formula = formula;
|
|
|
|
}
|
2018-06-24 21:11:31 -04:00
|
|
|
}
|