// Copyright (c) 2016, hackbard. All rights reserved. Use of this source code // is governed by a BSD-style license that can be found in the LICENSE file. import 'dart:async'; import 'package:angular2/core.dart'; import 'package:angular2/router.dart'; import 'product.dart'; import 'product_service.dart'; import 'product_category.dart'; import 'product_category_service.dart'; import 'package:angular2_rbi/directives.dart'; @Component( selector: 'my-products', templateUrl: 'product_component.html', styleUrls: const ['product_component.css'], directives: const [MaterialTextfield,MaterialButton], providers: const [ProductService,ProductCategoryService] ) class ProductComponent implements OnInit { final ProductService _prodSrv; final ProductCategoryService _prodcatSrv; final RouteParams _routeParams; final Router _router; String prod_category_id; String prod_category_name; String prod_name; double prod_price; int prodcnt; List products; ProductCategory prodcat; ProductComponent(this._prodSrv,this._prodcatSrv, this._routeParams,this._router) { prod_category_name='Category'; prod_category_id='ID'; } Future ngOnInit() async { var catid=_routeParams.get('id'); if(catid!=null) { products = await (_prodSrv.getByCategory(catid)); prodcat = await (_prodcatSrv.getById(catid)); } prod_category_name=prodcat.name; prod_category_id=prodcat.id; prodcnt=products.length; } Future updateProductCategory() async { bool doupdate=false; bool changecat=false; if(prodcat.id!=prod_category_id) { doupdate=true; changecat=true; } if(prodcat.name!=prod_category_name) { doupdate=true; } if(doupdate) { if(changecat) { for(Product prod in products) { print('Debug: change category of '+ prod.name); await _prodSrv.updateProd( prod.id, prod.name, prod.price, prod_category_id ); } } print('Debug: Updating product category '+ prodcat.name+'/'+prodcat.id+' -> '+ prod_category_name+'/'+prod_category_id); await _prodcatSrv.updateProdCategory( prodcat.id, prod_category_name, prod_category_id ); } } Future deleteProductCategory() async { bool delete=false; if(products.length==0) { delete=true; } else { print('Debug: Not deleting anything!'); } if(delete) { print('Debug: Deleting product category '+ prodcat.name+'/'+prodcat.id); await _prodcatSrv.deleteProdCategory(prodcat.doc_id); } } Future createProduct() async { if(prod_name==null || prod_price==null) return; print('Debug: Creating product '+prod_name+'/'+ prod_price.toString()); await _prodSrv.createProduct(prod_name, double.parse(prod_price), prod_category_id); } choose(Product prod) { goto_product(prod); } Future goto_product(Product prod) => _router.navigate([ 'ProductDetail', {'id': prod.id.toString()} ]); }