From 049d8a336f9521691f8850022dbdb7ef176c4ede Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Mon, 15 May 2023 08:56:19 -0400 Subject: [PATCH] toward saving Signed-off-by: Jason Etheridge --- .../server/custom-org-unit-trees.component.html | 1 + .../server/custom-org-unit-trees.component.ts | 68 +++++++++++++++++----- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.html b/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.html index 40b8d9b1c6..a614679035 100644 --- a/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.html +++ b/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.html @@ -50,6 +50,7 @@ + diff --git a/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.ts b/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.ts index 96e78595d2..e4db7d4185 100644 --- a/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/admin/server/custom-org-unit-trees.component.ts @@ -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 { 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; + } + } -- 2.11.0