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';
tree: Tree;
custom_tree: Tree;
+ aouctn_root: IdlObject;
+ tree_type: IdlObject;
active: boolean = false;
selected: TreeNode;
custom_selected: TreeNode;
@ViewChild('moveNodeElsewhereDialog', { static: true }) moveNodeElsewhereDialog: CustomOrgUnitTreesDialogComponent;
constructor(
- //private idl: IdlService,
+ private idl: IdlService,
private org: OrgService,
private pcrud: PcrudService,
//private strings: StringService,
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),
);
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(
})
)
);
- 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;
}
const treeNode = new TreeNode({
id: orgNode.id(),
label: orgNode.name() + '--' + orgNode.shortname(),
- callerData: {orgUnit: orgNode},
+ callerData: {orgId: orgNode.id()},
expanded: expand
});
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
});
}
);
}
+
+ 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;
+ }
+
}