cleaups and content page refresh (in progress)
[outofuni/tavern2.git] / lib / product_component.dart
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.
3
4 import 'dart:async';
5
6 import 'package:angular2/core.dart';
7 import 'package:angular2/router.dart';
8
9 import 'product.dart';
10 import 'product_service.dart';
11 import 'product_category.dart';
12 import 'product_category_service.dart';
13
14 import 'package:angular2_rbi/directives.dart';
15
16 @Component(
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]
22 )
23
24 class ProductComponent implements OnInit {
25         final ProductService _prodSrv;
26         final ProductCategoryService _prodcatSrv;
27         final RouteParams _routeParams;
28         final Router _router;
29
30         String catid;
31         String prod_category_name;
32
33         String prod_name;
34         double prod_price;
35         int prodcnt;
36
37         List<Product> products;
38         ProductCategory prodcat;
39
40         ProductComponent(this._prodSrv,this._prodcatSrv,
41                          this._routeParams,this._router) {
42                 prod_category_name='Category';
43         }
44
45         Future<Null> ngOnInit() async {
46                 catid=_routeParams.get('id');
47                 if(catid!=null) {
48                         products = await (_prodSrv.getByCategory(catid));
49                         prodcat = await (_prodcatSrv.getById(catid));
50                 }
51                 prod_category_name=prodcat.name;
52                 prodcnt=products.length;
53         }
54
55         Future<Null> updateProductCategory() async {
56                 if(prodcat.name!=prod_category_name) {
57                         await _prodcatSrv.updateProdCategory(
58                                 catid,
59                                 prod_category_name
60                         );
61                         _router.navigate(['ProductCategories']);
62                 }
63         }
64
65         Future<Null> deleteProductCategory() async {
66                 print('Debug: Deleting product category '+
67                       prodcat.name+'/'+prodcat.id);
68                 await _prodcatSrv.deleteProdCategory(prodcat.id);
69                 _router.navigate(['ProductCategories']);
70         }
71
72         Future<Null> createProduct() async {
73                 if(prod_name==null || prod_price==null)
74                         return;
75                 print('Debug: Creating product '+prod_name+'/'+
76                       prod_price.toString());
77                 String id = await _prodSrv.createProduct(prod_name,
78                                              double.parse(prod_price),
79                                              catid);
80                 // instead of executing ngOnInit to 'reload' content
81                 products.add(new Product(
82                         id,prod_name,double.parse(prod_price),'product',catid
83                 ));
84                 prod_name='';
85                 prod_price='';
86         }
87
88         choose(Product prod) {
89                 goto_product(prod);
90         }
91
92         Future<Null> goto_product(Product prod) => _router.navigate([
93                 'ProductDetail',
94                 {'id': prod.id.toString()}
95         ]);
96
97 }
98