1 // Copyright (c) 2016, hackbard. All rights reserved. Use of this source code
2 // is governed by a BSD-style license that can be found in the LICENSE file.
6 import 'package:angular2/core.dart';
7 import 'package:angular2/router.dart';
10 import 'product_service.dart';
11 import 'product_category.dart';
12 import 'product_category_service.dart';
14 import 'package:angular2_rbi/directives.dart';
17 selector: 'my-products',
18 templateUrl: 'product_component.html',
19 styleUrls: const ['product_component.css'],
20 directives: const [MaterialTextfield,MaterialButton],
21 providers: const [ProductService,ProductCategoryService]
24 class ProductComponent implements OnInit {
25 final ProductService _prodSrv;
26 final ProductCategoryService _prodcatSrv;
27 final RouteParams _routeParams;
30 String prod_category_id;
31 String prod_category_name;
36 List<Product> products;
37 ProductCategory prodcat;
39 ProductComponent(this._prodSrv,this._prodcatSrv,
40 this._routeParams,this._router) {
41 prod_category_name='Category';
42 prod_category_id='ID';
45 Future<Null> ngOnInit() async {
46 var catid=_routeParams.get('id');
48 products = await (_prodSrv.getByCategory(catid));
49 prodcat = await (_prodcatSrv.getById(catid));
51 prod_category_name=prodcat.name;
52 prod_category_id=prodcat.id;
53 prodcnt=products.length;
56 Future<Null> updateProductCategory() async {
59 if(prodcat.id!=prod_category_id) {
63 if(prodcat.name!=prod_category_name) {
68 for(Product prod in products) {
69 print('Debug: change category of '+
71 await _prodSrv.updateProd(
79 print('Debug: Updating product category '+
80 prodcat.name+'/'+prodcat.id+' -> '+
81 prod_category_name+'/'+prod_category_id);
82 await _prodcatSrv.updateProdCategory(
90 Future<Null> deleteProductCategory() async {
92 if(products.length==0) {
96 print('Debug: Not deleting anything!');
99 print('Debug: Deleting product category '+
100 prodcat.name+'/'+prodcat.id);
101 await _prodcatSrv.deleteProdCategory(prodcat.doc_id);
105 Future<Null> createProduct() async {
106 if(prod_name==null || prod_price==null)
108 print('Debug: Creating product '+prod_name+'/'+
109 prod_price.toString());
110 await _prodSrv.createProduct(prod_name,
111 double.parse(prod_price),
115 choose(Product prod) {
119 Future<Null> goto_product(Product prod) => _router.navigate([
121 {'id': prod.id.toString()}