cleaups and content page refresh (in progress)
authorhackbard <hackbard@hackdaworld.org>
Sat, 15 Oct 2016 17:31:29 +0000 (19:31 +0200)
committerhackbard <hackbard@hackdaworld.org>
Sat, 15 Oct 2016 17:31:29 +0000 (19:31 +0200)
lib/app_component.dart
lib/product_category.dart
lib/product_category_component.dart
lib/product_category_component.html
lib/product_category_service.dart
lib/product_component.dart
lib/product_component.html
lib/product_detail_component.css
lib/product_detail_component.html
lib/product_service.dart

index 1f96314..2175bdf 100644 (file)
@@ -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
        )
 ])
 
index 3e99981..67ef684 100644 (file)
@@ -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);
 }
 
index 7b48a2a..b7d3bf1 100644 (file)
@@ -22,42 +22,19 @@ import 'package:angular2_rbi/directives.dart';
 class ProductCategoryComponent implements OnInit {
        List<ProductCategory> 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<Null> getProductCategories() async {
-               product_categories = await _prodcatSrv.getAll();
-       }
-
        Future<Null> 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<Null> ngOnInit() async {
+               product_categories = await _prodcatSrv.getAll();
        }
 
        choose(ProductCategory pt) {
index 6fff730..7b1df92 100644 (file)
        <div class="mdl-textfield mdl-js-textfield
                    mdl-textfield--floating-label ain">
                <input class="mdl-textfield__input" type=text id=ptn
-                      [(ngModel)]="new_prod_category_name"
-                      (keyup)="checkInput()">
+                      [(ngModel)]="new_prod_category_name">
                <label class="mdl-textfield__label" for=ptn>Name</label>
        </div>
-       <div class="mdl-textfield mdl-js-textfield
-                   mdl-textfield--floating-label ain">
-               <input class="mdl-textfield__input" type=text id=pti
-                      [(ngModel)]="new_prod_category_id"
-                      (keyup)="checkInput()">
-               <label class="mdl-textfield__label" for=pti>Identifier</label>
-       </div>
        <button class="mdl-button mdl-js-button mdl-button--raised abtn"
                (click)="createProductCategory()"
-               [disabled]="!docreate">
+               [disabled]="new_prod_category_name==''">
                Create
        </button>
 </div>
index c62e7ef..f246a71 100644 (file)
@@ -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<Null> createProdCategory(String name,String id) async {
+       Future<Null> 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<ProductCategory> 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<Null> updateProdCategory(String oldid,String name,
-                                       String id) async {
+       Future<Null> 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<Null> deleteProdCategory(String doc_id) async {
+       Future<Null> 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/';
index ed08975..6fb5461 100644 (file)
@@ -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<Null> 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<Null> 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<Null> 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<Null> 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) {
index 038eab1..6b79bea 100644 (file)
                       [(ngModel)]="prod_category_name">
                <label class="mdl-textfield__label" for="ptn">Name</label>
        </div>
-       <div class="mdl-textfield mdl-js-textfield
-                   mdl-textfield--floating-label ain">
-               <input class="mdl-textfield__input" type="text" id="pti"
-                      [(ngModel)]="prod_category_id">
-               <label class="mdl-textfield__label" for="pti">
-                       Identifier (arbitrary but unique)
-               </label>
-       </div>
        <button class="mdl-button mdl-js-button mdl-button--raised abtn"
-               (click)="updateProductCategory()">
+               (click)="updateProductCategory()"
+               [disabled]="prod_category_name==''">
                Update
        </button>
        <button class="mdl-button mdl-js-button mdl-button--raised abtn"
index ff02e63..beab8b7 100644 (file)
@@ -27,3 +27,7 @@
        margin-left: 5%;
 }
 
+.aseldesc {
+       font-size: 12px;
+       margin-bottom: 10px;
+}
index c51b1b1..7a61e37 100644 (file)
@@ -29,7 +29,9 @@
        </div>
        </div>
        <div class=asel>
-               Product category<br><br>
+               <div class=aseldesc>
+               Product category
+               </div>
                <select [(ngModel)]="prod_category" required>
                        <option *ngFor="let prodcat of prodcats"
                                [value]="prodcat.id">
index 41af5cb..0ffe964 100644 (file)
@@ -60,7 +60,7 @@ class ProductService {
        Future<Null> createProduct(String name,double price,String cat) async {
                try {
                        var url=_server+'/'+_db;
-                       await _http.post(
+                       var response = await _http.post(
                                url,
                                headers: {'Content-Type': 'application/json'},
                                body: JSON.encode({
@@ -70,6 +70,7 @@ class ProductService {
                                        'category': cat
                                })
                        );
+                       return(JSON.decode(response.body)['id']);
                }
                catch(e) {
                        throw _handleError(e);