]> hackdaworld.org Git - outofuni/tavern2.git/commitdiff
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 1f96314c02499d4584e50e31c8b03e4447bfc660..2175bdfae18b17e987b6f138c53ee7aec62dc6bc 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 3e99981c61960c52398db11ba8590cab6800c33f..67ef684c1e39181e970abf556ab12f2b4dfbccd4 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 7b48a2aef0a8da676a90f16808295d9ffa9e8d07..b7d3bf12f562bc0db73f564ee54d0ddce895fe67 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 6fff73015a57f277a6a5d757ee825ff2226f3b1a..7b1df92ada845c14f9f25fc08f11819f061e89b7 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 c62e7ef0e18e2475141e5afbcf54c8a4d867c475..f246a71183bb69148c668305e146d9b89c94cc8a 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 ed08975fdc6f6f47b340641ee8765ea3e5085621..6fb5461cb7095d3191aa8166e03392b4cc79e93a 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 038eab1174d48f787f69727e747633b0e5e0be3e..6b79bea86e457e07030b702eb88d6292bb566d92 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 ff02e6317f1ff440249bc0e9d424d260f2ec81ea..beab8b70a8faebf2ab53ab4d94ffc5a9817b022d 100644 (file)
@@ -27,3 +27,7 @@
        margin-left: 5%;
 }
 
+.aseldesc {
+       font-size: 12px;
+       margin-bottom: 10px;
+}
index c51b1b1800ffb2599b4daa0ed7fde7c14f95ed57..7a61e3731f56ab40f023d3fc80fc08d8910b26cc 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 41af5cb9e3f81ad1d3c8cffeed3dc09397cc1d4c..0ffe964b17c0038520314349e0d6ffa1b3077071 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);