LPXXX Angular Volcopy
authorBill Erickson <berickxx@gmail.com>
Mon, 8 Jun 2020 16:23:50 +0000 (12:23 -0400)
committerBill Erickson <berickxx@gmail.com>
Mon, 8 Jun 2020 16:23:50 +0000 (12:23 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.html
Open-ILS/src/eg2/src/app/staff/cat/volcopy/vol-edit.component.ts
Open-ILS/src/eg2/src/app/staff/cat/volcopy/volcopy.component.ts

index a8a74c7..0360b00 100644 (file)
         <div class="p-1" [ngStyle]="{flex: flexAt(2)}">
           <ng-container *ngIf="copyIdx == 0 && volIdx == 0">
             <input type="number" class="form-control form-control-sm"
-              [required]="true" min="0"
+              [required]="true" [min]="existingVolCount(orgNode)"
               [ngModel]="orgNode.children.length"
               (ngModelChange)="volCountChanged(orgNode, $event)"/>
           </ng-container>
         <div class="p-1" [ngStyle]="{flex: flexAt(7)}">
           <ng-container *ngIf="copyIdx == 0">
             <input type="number" class="form-control form-control-sm"
-              [required]="true" min="0"
+              [required]="true" [min]="existingCopyCount(volNode)"
               [ngModel]="volNode.children.length"
               (ngModelChange)="copyCountChanged(volNode, $event)"/>
           </ng-container>
index 29692eb..46d3dff 100644 (file)
@@ -145,7 +145,7 @@ export class VolEditComponent implements OnInit {
         if (diff > 0) {
             this.createVols(orgNode, diff);
         } else if (diff < 0) {
-            this.deleteVols(orgNode, diff);
+            this.deleteVols(orgNode, -diff);
         }
     }
 
@@ -164,6 +164,16 @@ export class VolEditComponent implements OnInit {
         return vol;
     }
 
+    existingVolCount(orgNode: HoldingsTreeNode): number {
+        return orgNode.children.filter(volNode => !volNode.target.isnew()).length;
+    }
+
+    existingCopyCount(volNode: HoldingsTreeNode): number {
+        return volNode.children.filter(copyNode => !copyNode.target.isnew()).length;
+    }
+
+
+
 
     createStubCopy(vol: IdlObject): IdlObject {
 
@@ -190,9 +200,39 @@ export class VolEditComponent implements OnInit {
     }
 
     copyCountChanged(volNode: HoldingsTreeNode, count: number) {
-        console.log('vol set set to ', count);
+        const diff = count - volNode.children.length;
+        if (diff > 0) {
+            this.createCopies(volNode, diff);
+        } else if (diff < 0) {
+            this.deleteCopies(volNode, -diff);
+        }
+    }
+
+    // This only removes copies that were created during the
+    // current editing session and have not yet been saved in the DB.
+    deleteCopies(volNode: HoldingsTreeNode, count: number) {
+        for (let i = 0;  i < count; i++) {
+            const copyNode = volNode.children[volNode.children.length - 1];
+            if (copyNode && copyNode.target.isnew()) {
+                volNode.children.pop();
+            } else {
+                break;
+            }
+        }
+    }
+
+    createCopies(volNode: HoldingsTreeNode, count: number) {
+        for (let i = 0; i < count; i++) {
+
+            // Our context assumes copies are fleshed with volumes
+            const vol = volNode.target;
+            const copy = this.createStubCopy(vol);
+            copy.call_number(vol);
+            this.context.findOrCreateCopyNode(copy);
+        }
     }
 
+
     createVols(orgNode: HoldingsTreeNode, count: number) {
         for (let i = 0; i < count; i++) {
 
@@ -206,15 +246,22 @@ export class VolEditComponent implements OnInit {
         }
     }
 
-
+    // This only removes vols that were created during the
+    // current editing session and have not yet been saved in the DB.
     deleteVols(orgNode: HoldingsTreeNode, count: number) {
+        for (let i = 0;  i < count; i++) {
+            const volNode = orgNode.children[orgNode.children.length - 1];
+            if (volNode && volNode.target.isnew()) {
+                orgNode.children.pop();
+            } else {
+                break;
+            }
+        }
     }
 
-
     // Empty volumes get a stub copy
     addStubCopies() {
 
-        // TODO: timing issue -- all vols getting stubs
         this.context.volNodes().forEach(volNode => {
             if (volNode.children.length == 0) {
                 const vol = volNode.target;
index dd9173f..a080c3f 100644 (file)
@@ -102,12 +102,12 @@ export class VolCopyComponent implements OnInit {
 
         return this.pcrud.search('acn', {id: ids})
         .pipe(tap(vol => this.context.findOrCreateVolNode(vol)))
-        .pipe(tap(vol => {
+        .toPromise().then(_ => {
              return this.pcrud.search('acp',
                 {call_number: ids, deleted: 'f'}, COPY_FLESH
             ).pipe(tap(copy => this.context.findOrCreateCopyNode(copy))
             ).toPromise();
-        })).toPromise();
+        });
     }
 
     // Fetch call numbers and copies by record ids.