update views + reduced providers in main app
[outofuni/tavern2.git] / lib / product_detail_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-product-details',
18         templateUrl: 'product_detail_component.html',
19         styleUrls: const ['product_detail_component.css'],
20         directives: const [MaterialTextfield,MaterialButton],
21         providers: const [ProductService,ProductCategoryService]
22 )
23
24 class ProductDetailComponent implements OnInit {
25         final ProductService _prodSrv;
26         final ProductCategoryService _prodcatSrv;
27         final RouteParams _routeParams;
28
29         List<ProductCategory> prodcats;
30         Product prod;
31
32         String prod_name;
33         String prod_category;
34         double prod_price;
35
36         ProductDetailComponent(this._prodSrv,this._prodcatSrv,
37                                this._routeParams) {
38                 prod_name='Product';
39                 prod_price=0;
40         }
41
42         Future<Null> ngOnInit() async {
43                 var id=_routeParams.get('id');
44                 if(id!=null) {
45                         prod = await (_prodSrv.getById(id));
46                         prodcats = await (_prodcatSrv.getAll());
47                 }
48                 prod_name=prod.name;
49                 prod_price=prod.price;
50                 prod_category=prod.category;
51         }
52
53         Future<Null> updateProduct() async {
54                 bool doupdate=false;
55                 if(prod.name!=prod_name) {
56                         doupdate=true;
57                 }
58                 if(prod.price!=prod_price) {
59                         doupdate=true;
60                 }
61                 if(prod.category!=prod_category) {
62                         doupdate=true;
63                 }
64                 if(doupdate) {
65                         print('Debug: Updating product '+
66                               prod.name+'/'+prod.price.toString()+'/'+
67                               prod.category+' -> '+
68                               prod_name+'/'+prod_price.toString()+'/'+
69                               prod_category);
70                         await _prodSrv.updateProd(
71                                 prod.id,
72                                 prod_name,
73                                 prod_price,
74                                 prod_category
75                         );
76                         prod.price=prod_price;
77                         prod.name=prod_name;
78                         prod.category=prod_category;
79                 }
80         }
81
82         Future<Null> deleteProduct(Product prod) async {
83                 print('Debug: Deleting product '+prod.name);
84                 await _prodSrv.deleteProduct(prod.id);
85         }
86
87 }
88