website/src/app/app.component.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

import {Component} from '@angular/core';
import {AngularFirestore} from 'angularfire2/firestore';
2018-06-25 00:50:47 -04:00
import {share} from 'rxjs/operators';
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';
_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) {
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
}