toward saving
authorJason Etheridge <phasefx@gmail.com>
Mon, 15 May 2023 12:56:19 +0000 (08:56 -0400)
committerJason Etheridge <phasefx@gmail.com>
Thu, 25 May 2023 16:45:32 +0000 (12:45 -0400)
Signed-off-by: Jason Etheridge <phasefx@gmail.com>
Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.html
Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.ts

index 40b8d9b..a614679 100644 (file)
@@ -50,6 +50,7 @@
         <button [disabled]="moveNodeElsewhereDisabled" (click)="moveNodeElsewhere()" i18n>Move Node Elsewhere</button>
         <button *ngIf="!active" (click)="active = !active" i18n>Activate Tree</button>
         <button *ngIf="active" (click)="active = !active" i18n>Deactivate Tree</button>
+        <button (click)="applyChanges()" i18n>Apply Changes</button>
       </eg-tree>
     </div>
   </div>
index 96e7859..e4db7d4 100644 (file)
@@ -1,7 +1,7 @@
 import {Component, ViewChild, OnInit} from '@angular/core';
 import {catchError, firstValueFrom, of, take, defaultIfEmpty} from 'rxjs';
 import {Tree, TreeNode} from '@eg/share/tree/tree';
-//import {IdlService} from '@eg/core/idl.service';
+import {IdlService} from '@eg/core/idl.service';
 import {IdlObject} from '@eg/core/idl.service';
 import {OrgService} from '@eg/core/org.service';
 import {PcrudService} from '@eg/core/pcrud.service';
@@ -20,6 +20,8 @@ export class CustomOrgUnitTreesComponent implements OnInit {
 
     tree: Tree;
     custom_tree: Tree;
+       aouctn_root: IdlObject;
+    tree_type: IdlObject;
     active: boolean = false;
     selected: TreeNode;
     custom_selected: TreeNode;
@@ -36,7 +38,7 @@ export class CustomOrgUnitTreesComponent implements OnInit {
     @ViewChild('moveNodeElsewhereDialog', { static: true }) moveNodeElsewhereDialog: CustomOrgUnitTreesDialogComponent;
 
     constructor(
-        //private idl: IdlService,
+        private idl: IdlService,
         private org: OrgService,
         private pcrud: PcrudService,
         //private strings: StringService,
@@ -78,8 +80,7 @@ export class CustomOrgUnitTreesComponent implements OnInit {
     async loadCustomTree(purpose: string): Promise<any> {
         const flesh = ['children', 'org_unit'];
 
-        let tree_type: IdlObject;
-        tree_type = await firstValueFrom(
+        this.tree_type = await firstValueFrom(
             this.pcrud.search('aouct', { purpose: purpose })
             .pipe(
                 take(1),
@@ -92,16 +93,15 @@ export class CustomOrgUnitTreesComponent implements OnInit {
         );
 
         let tree_id: number;
-        if (tree_type) {
-            tree_id = tree_type.id();
-            this.active = tree_type.active();
+        if (this.tree_type) {
+            tree_id = this.tree_type.id();
+            this.active = this.tree_type.active();
         } else {
             tree_id = null;
         }
 
-        let tree: IdlObject;
         if (tree_id) {
-            tree = await firstValueFrom(
+            this.aouctn_root = await firstValueFrom(
                 this.pcrud.search('aouctn', {tree: tree_id, parent_node: null},
                     {flesh: -1, flesh_fields: {aouctn: flesh}}, {authoritative: true})
                 .pipe(
@@ -113,9 +113,13 @@ export class CustomOrgUnitTreesComponent implements OnInit {
                     })
                 )
             );
-            this.ingestCustomTree(tree); // sets this.custom_tree as a side-effect
+            this.ingestCustomTree(this.aouctn_root); // sets this.custom_tree as a side-effect
         } else {
-            this.custom_tree = this.tree.clone(); // need to remember to create the aouct if needed upon saving
+            console.log('prior to clone: ' + this.tree, this.tree);
+            this.custom_tree = this.tree.clone();
+            this.tree_type = this.idl.create('aouct');
+            this.tree_type.isnew('t');
+            this.tree_type.purpose('opac');
         }
         return this.custom_tree;
     }
@@ -129,7 +133,7 @@ export class CustomOrgUnitTreesComponent implements OnInit {
             const treeNode = new TreeNode({
                 id: orgNode.id(),
                 label: orgNode.name() + '--' + orgNode.shortname(),
-                callerData: {orgUnit: orgNode},
+                callerData: {orgId: orgNode.id()},
                 expanded: expand
             });
 
@@ -157,7 +161,7 @@ export class CustomOrgUnitTreesComponent implements OnInit {
             const treeNode = new TreeNode({
                 id: orgNode.id(),
                 label: orgNode.org_unit().name() + '--' + orgNode.org_unit().shortname(),
-                callerData: {orgUnit: orgNode},
+                callerData: {orgId: orgNode.org_unit().id()},
                 expanded: expand
             });
 
@@ -398,5 +402,43 @@ export class CustomOrgUnitTreesComponent implements OnInit {
             }
         );
     }
+
+       async applyChanges() {
+               if (this.active !== this.tree_type.active()) {
+                       this.tree_type.active(this.active);
+                       this.tree_type.ischanged('t');
+               }
+               try {
+                       if (this.tree_type.isnew()) {
+                               this.tree_type = await firstValueFrom(this.pcrud.create(this.tree_type));
+                       } else if (this.tree_type.ischanged()) {
+                               await firstValueFrom(this.pcrud.update(this.tree_type));
+                       }
+                       await this.createNewNodes(this.custom_tree.rootNode);
+               } catch (error) {
+                       console.error('Error applying changes:', error);
+               }
+       }
+
+       async createNewNodes(node: any, parent_id: number = null, order: number = 0) {
+               let newNode = this.idl.create('aouctn');
+               newNode.parent_node(parent_id);
+               newNode.sibling_order(order);
+               newNode.org_unit = node.callerData.orgId;
+               newNode.tree = this.tree_type.id();
+
+               // Send the new node to the server and get back the updated node
+               newNode = await firstValueFrom(this.pcrud.create(newNode));
+
+               // If the original node has children, create new nodes for each child
+               if (node.children && node.children.length > 0) {
+                       for (let i = 0; i < node.children.length; i++) {
+                               await this.createNewNodes(node.children[i], newNode.id(), i);
+                       }
+               }
+
+               return newNode;
+       }
+
 }