LP1840050 org unit admin UI WIP
authorBill Erickson <berickxx@gmail.com>
Fri, 16 Aug 2019 19:44:54 +0000 (15:44 -0400)
committerBill Erickson <berickxx@gmail.com>
Fri, 16 Aug 2019 19:44:54 +0000 (15:44 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/staff/admin/server/org-addr.component.html
Open-ILS/src/eg2/src/app/staff/admin/server/org-addr.component.ts
Open-ILS/src/eg2/src/app/staff/admin/server/org-unit.component.html
Open-ILS/src/eg2/src/app/staff/admin/server/org-unit.component.ts

index 0545025..b3a787b 100644 (file)
@@ -1,9 +1,9 @@
 
-<ngb-tabset #addressTabs *ngIf="orgUnit">
+<ngb-tabset #addressTabs *ngIf="orgUnit" (tabChange)="tabChanged($event)">
   <ng-container *ngFor="let type of addrTypes()">
     <b>type = {{type}}</b>
 
-    <ngb-tab
+    <ngb-tab *ngIf="addr(type)"
       i18n-title id="{{type}}"
       title="{{type === 'billing_address' ? 'Physical Address' : 
         (type === 'holds_address' ? 'Holds Address' : 
         <eg-fm-record-editor idlClass="aoa" readonlyFields="org_unit" 
           [mode]="addr(type).isnew() ? 'create': 'update'" 
           [hideBanner]="true" displayMode="inline" hiddenFields="id"
-          [showDelete]="true" (recordSaved)="addrSaved($event)" 
-          (recordDeleted)="addrDeleted($event)"
+          (recordSaved)="addrSaved($event)" 
           [record]="addr(type)"
           fieldOrder="address_type,street1,street2,city,county,state,country,post_code,san,valid"
           >
+          <eg-fm-record-editor-action i18n-label label="Delete" *ngIf="!addr(type).isnew()"
+            (actionClick)="deleteAddress($event)" buttonCss="btn-warning">
+          </eg-fm-record-editor-action>
         </eg-fm-record-editor>
 
         <ng-container *ngIf="sharedAddress(addr(type).id())">
index 1bc2863..3b4a498 100644 (file)
@@ -2,6 +2,7 @@ import {Component, Input, Output, EventEmitter} from '@angular/core';
 import {IdlService, IdlObject} from '@eg/core/idl.service';
 import {OrgService} from '@eg/core/org.service';
 import {PcrudService} from '@eg/core/pcrud.service';
+import {NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
 
 const ADDR_TYPES =
     ['billing_address', 'holds_address', 'mailing_address', 'ill_address'];
@@ -13,6 +14,7 @@ const ADDR_TYPES =
 export class OrgAddressComponent {
 
     private orgUnit: IdlObject = null;
+    private tabName: string;
 
     private _orgId: number;
 
@@ -37,6 +39,7 @@ export class OrgAddressComponent {
         private pcrud: PcrudService
     ) {
         this.addrChange = new EventEmitter<IdlObject>();
+        this.tabName = 'billing_address';
     }
 
     init() {
@@ -55,6 +58,10 @@ export class OrgAddressComponent {
         });
     }
 
+    tabChanged($event: NgbTabChangeEvent) {
+        this.tabName = $event.nextId;
+    }
+
     addrTypes(): string[] { // for UI
         return ADDR_TYPES;
     }
@@ -68,7 +75,7 @@ export class OrgAddressComponent {
         const addr = this.idl.create('aoa');
         addr.isnew(true);
         addr.valid('t');
-        addr.org_unit(this.orgUnit);
+        addr.org_unit(this.orgId);
         this.orgUnit[addrType](addr);
     }
 
@@ -99,15 +106,58 @@ export class OrgAddressComponent {
         }).length > 1;
     }
 
+    deleteAddress($event: any) {
+        const addr = $event.record;
+        const tmpOrg = this.updatableOrg();
+
+        // Set the FKey to NULL on the org unit for deleted addresses
+        ADDR_TYPES.forEach(aType => {
+            const a = this.addr(aType);
+            if (a && a.id() === addr.id()) {
+                tmpOrg[aType](null);
+                this.createAddress(aType);
+            }
+        });
+
+        this.pcrud.update(tmpOrg).toPromise()
+        .then(_ => this.pcrud.remove(addr).toPromise())
+        .then(_ => this.addrChange.emit(addr));
+    }
+
+    // Addr saved by fm-editor.
+    // In the case of new address creation, point the org unit at
+    // the new address ID.
     addrSaved(addr: number | IdlObject) {
-        const id = typeof addr === 'object' ? addr.id() : addr;
 
-        // TODO: update org if needed...
+        if (typeof addr !== 'object') {
+            // pcrud returns a number on 'update' calls.  No need to
+            // reload the data on a simple address change. it's changed
+            // in place.
+            return;
+        }
+
+        // update local copy with version that has an ID.
+        this.orgUnit[this.tabName](addr);
+
+        const org = this.updatableOrg();
+        org[this.tabName](addr.id());
+
+        // Creating a new address -- tell our org about it.
+        this.pcrud.update(org).toPromise().then(_ => this.addrChange.emit(addr));
     }
 
-    addrDeleted(addr: number | IdlObject) {
-        const id = typeof addr === 'object' ? addr.id() : addr;
-        // TODO: update org
+    // Create an unfleshed org unit object that's a clone of this.orgUnit
+    // to use when pushing updates to the server.
+    updatableOrg(): IdlObject {
+        const org = this.idl.clone(this.orgUnit);
+
+        ADDR_TYPES.forEach(aType => {
+            const addr = this.addr(aType);
+            if (addr) { org[aType](addr.id()); }
+        });
+
+        return org;
     }
+
 }
 
index ef8d360..294d62c 100644 (file)
@@ -52,6 +52,7 @@
               hiddenFields="id,billing_address,mailing_address,holds_address,ill_address">
               <eg-fm-record-editor-action label="Add Child" i18n-label 
                 [disabled]="orgChildTypes().length === 0 || currentOrg().isnew()"
+                buttonCss="btn-outline-info"
                 (actionClick)="addChild()"></eg-fm-record-editor-action>
             </eg-fm-record-editor>
           </div>
               </div>
             </div>
             <div class="row d-flex justify-content-end">
+              <div class="alert alert-warning mr-2 p-1" 
+                *ngIf="currentOrg().hours_of_operation().isnew()">
+                Hours of Operation Have Not Yet Been Saved.
+              </div>
+              <div class="mr-2">
+                <button class="btn btn-warning" (click)="deleteHours()" i18n>
+                  Clear Hours of Operation
+                </button>
+              </div>
               <div>
                 <button class="btn btn-info" (click)="saveHours()" i18n>
                   Apply Changes
index 6df6379..714d7b7 100644 (file)
@@ -169,6 +169,14 @@ export class OrgUnitComponent implements OnInit {
         );
     }
 
+    deleteHours() {
+        const hours = this.currentOrg().hours_of_operation();
+        const promise = hours.isnew() ? Promise.resolve() :
+            this.pcrud.remove(hours).toPromise();
+
+        promise.then(_ => this.generateHours(this.currentOrg()));
+    }
+
     currentOrg(): IdlObject {
         return this.selected ? this.selected.callerData.orgUnit : null;
     }