From b2028ca4879e48884436ee0c2aee7fae6ddaeb5a Mon Sep 17 00:00:00 2001 From: hackbard Date: Sat, 15 Oct 2016 19:31:29 +0200 Subject: [PATCH] cleaups and content page refresh (in progress) --- lib/app_component.dart | 6 --- lib/product_category.dart | 7 ++-- lib/product_category_component.dart | 33 +++------------ lib/product_category_component.html | 12 +----- lib/product_category_service.dart | 36 ++++++----------- lib/product_component.dart | 63 +++++++++-------------------- lib/product_component.html | 11 +---- lib/product_detail_component.css | 4 ++ lib/product_detail_component.html | 4 +- lib/product_service.dart | 3 +- 10 files changed, 52 insertions(+), 127 deletions(-) diff --git a/lib/app_component.dart b/lib/app_component.dart index 1f96314..2175bdf 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -6,7 +6,6 @@ import 'package:angular2_rbi/directives.dart'; import 'product_component.dart'; import 'product_category_component.dart'; import 'product_detail_component.dart'; -import 'product_category_detail_component.dart'; import 'product_service.dart'; import 'product_category_service.dart'; @@ -41,11 +40,6 @@ import 'product_category_service.dart'; path: '/product/:id', name: 'ProductDetail', component: ProductDetailComponent - ), - const Route( - path: '/product_category/:id', - name: 'ProductCategoryDetail', - component: ProductCategoryDetailComponent ) ]) diff --git a/lib/product_category.dart b/lib/product_category.dart index 3e99981..67ef684 100644 --- a/lib/product_category.dart +++ b/lib/product_category.dart @@ -1,9 +1,8 @@ class ProductCategory { - String doc_id; - String name; String id; - String doc_type; + String name; + String type; - ProductCategory(this.doc_id,this.name,this.id,this.doc_type); + ProductCategory(this.id,this.name,this.type); } diff --git a/lib/product_category_component.dart b/lib/product_category_component.dart index 7b48a2a..b7d3bf1 100644 --- a/lib/product_category_component.dart +++ b/lib/product_category_component.dart @@ -22,42 +22,19 @@ import 'package:angular2_rbi/directives.dart'; class ProductCategoryComponent implements OnInit { List product_categories; ProductCategory selected_prod_category; - String new_prod_category_name; - String new_prod_category_id; + String new_prod_category_name=''; final ProductCategoryService _prodcatSrv; final Router _router; - var docreate=false; ProductCategoryComponent(this._prodcatSrv,this._router); - Future getProductCategories() async { - product_categories = await _prodcatSrv.getAll(); - } - Future createProductCategory() async { - await _prodcatSrv.createProdCategory( - new_prod_category_name, - new_prod_category_id - ); + await _prodcatSrv.createProdCategory(new_prod_category_name); + await ngOnInit(); } - void ngOnInit() { - getProductCategories(); - } - - void checkInput() { - if(new_prod_category_name=='' || new_prod_category_id=='') { - docreate=false; - } - else { - docreate=true; - for(var cat in product_categories) { - if(cat.id==new_prod_category_id) { - docreate=false; - break; - } - } - } + Future ngOnInit() async { + product_categories = await _prodcatSrv.getAll(); } choose(ProductCategory pt) { diff --git a/lib/product_category_component.html b/lib/product_category_component.html index 6fff730..7b1df92 100644 --- a/lib/product_category_component.html +++ b/lib/product_category_component.html @@ -17,20 +17,12 @@
+ [(ngModel)]="new_prod_category_name">
-
- - -
diff --git a/lib/product_category_service.dart b/lib/product_category_service.dart index c62e7ef..f246a71 100644 --- a/lib/product_category_service.dart +++ b/lib/product_category_service.dart @@ -11,9 +11,7 @@ class ProductCategoryService { static const _server='http://10.8.0.1:5984'; static const _db='tavern'; static const _viewname='_design/product_categories/_view/byname'; - static const _viewid='_design/product_categories/_view/byid'; static const _getnameurl=_server+'/'+_db+'/'+_viewname; - static const _getidurl=_server+'/'+_db+'/'+_viewid; static const _posturl=_server+'/'+_db; final BrowserClient _http; @@ -28,7 +26,6 @@ class ProductCategoryService { prodcats.add(new ProductCategory( item['id'], item['value']['name'], - item['value']['id'], item['value']['type'])); } return prodcats; @@ -38,14 +35,13 @@ class ProductCategoryService { } } - Future createProdCategory(String name,String id) async { + Future createProdCategory(String name) async { try { await _http.post( _posturl, headers: {'Content-Type': 'application/json'}, body: JSON.encode({ 'name': name, - 'id': id, 'type': 'product_category' }) ); @@ -58,14 +54,13 @@ class ProductCategoryService { Future getById(String id) async { try { ProductCategory prodcat; - String url=_getidurl+'?key="'+id+'"'; + String url=_server+'/'+_db+'/'+id; final response = await _http.get(url); var item=JSON.decode(response.body); prodcat = new ProductCategory( - item['rows'][0]['value']['_id'], - item['rows'][0]['value']['name'], - item['rows'][0]['value']['id'], - item['rows'][0]['value']['type'] + item['_id'], + item['name'], + item['type'] ); return prodcat; } @@ -74,31 +69,26 @@ class ProductCategoryService { } } - Future updateProdCategory(String oldid,String name, - String id) async { + Future updateProdCategory(String id,String name) async { try { - String url=_getidurl+'?key="'+oldid+'"'; + String url=_server+'/'+_db+'/'+id; final response = await _http.get(url); - print('Debug UPDATE GET URL: '+url); - print('Debug UPDATE GET response: '+response.body); var resbody=JSON.decode(response.body); - resbody['rows'][0]['value']['name']=name; - resbody['rows'][0]['value']['id']=id; - final response_put = await _http.put( - _posturl+'/'+resbody['rows'][0]['id'], + resbody['name']=name; + await _http.put( + _posturl+'/'+id, headers: {'Content-Type': 'application/json'}, - body: JSON.encode(resbody['rows'][0]['value']) + body: JSON.encode(resbody) ); - print('Debug UPDATE PUT response: '+response_put.body); } catch(e) { throw _handleError(e); } } - Future deleteProdCategory(String doc_id) async { + Future deleteProdCategory(String id) async { try { - var url=_server+'/'+_db+'/'+doc_id; + var url=_server+'/'+_db+'/'+id; var response = await _http.get(url); var reso=JSON.decode(response.body); url=_server+'/'+_db+'/_purge/'; diff --git a/lib/product_component.dart b/lib/product_component.dart index ed08975..6fb5461 100644 --- a/lib/product_component.dart +++ b/lib/product_component.dart @@ -27,8 +27,9 @@ class ProductComponent implements OnInit { final RouteParams _routeParams; final Router _router; - String prod_category_id; + String catid; String prod_category_name; + String prod_name; double prod_price; int prodcnt; @@ -39,67 +40,33 @@ class ProductComponent implements OnInit { 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'); + 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 + catid, + prod_category_name ); + _router.navigate(['ProductCategories']); } } 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); - } + print('Debug: Deleting product category '+ + prodcat.name+'/'+prodcat.id); + await _prodcatSrv.deleteProdCategory(prodcat.id); + _router.navigate(['ProductCategories']); } Future createProduct() async { @@ -107,9 +74,15 @@ class ProductComponent implements OnInit { return; print('Debug: Creating product '+prod_name+'/'+ prod_price.toString()); - await _prodSrv.createProduct(prod_name, + String id = await _prodSrv.createProduct(prod_name, double.parse(prod_price), - prod_category_id); + catid); + // instead of executing ngOnInit to 'reload' content + products.add(new Product( + id,prod_name,double.parse(prod_price),'product',catid + )); + prod_name=''; + prod_price=''; } choose(Product prod) { diff --git a/lib/product_component.html b/lib/product_component.html index 038eab1..6b79bea 100644 --- a/lib/product_component.html +++ b/lib/product_component.html @@ -47,16 +47,9 @@ [(ngModel)]="prod_category_name"> -
- - -