import {Component, ViewChild, OnInit} from '@angular/core';
-import {catchError, firstValueFrom, of, take, defaultIfEmpty} from 'rxjs';
+import {catchError, firstValueFrom, lastValueFrom, of, take, defaultIfEmpty} from 'rxjs';
import {Tree, TreeNode} from '@eg/share/tree/tree';
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';
-//import {ToastService} from '@eg/share/toast/toast.service';
+import {ToastService} from '@eg/share/toast/toast.service';
import {StringComponent} from '@eg/share/string/string.component';
//import {StringService} from '@eg/share/string/string.service';
import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
@ViewChild('editString', { static: true }) editString: StringComponent;
@ViewChild('errorString', { static: true }) errorString: StringComponent;
+ @ViewChild('successString', { static: true }) successString: StringComponent;
+ @ViewChild('updateFailedString', { static: true }) updateFailedString: StringComponent;
@ViewChild('delConfirm', { static: true }) delConfirm: ConfirmDialogComponent;
@ViewChild('moveNodeElsewhereDialog', { static: true }) moveNodeElsewhereDialog: CustomOrgUnitTreesDialogComponent;
private org: OrgService,
private pcrud: PcrudService,
//private strings: StringService,
- //private toast: ToastService
+ private toast: ToastService
) {}
let tree_id: number;
if (this.tree_type) {
tree_id = this.tree_type.id();
- this.active = this.tree_type.active();
+ this.active = this.tree_type.active() === 't';
} else {
tree_id = null;
}
+ this.aouctn_root = undefined;
if (tree_id) {
this.aouctn_root = await firstValueFrom(
this.pcrud.search('aouctn', {tree: tree_id, parent_node: null},
})
)
);
- this.ingestCustomTree(this.aouctn_root); // sets this.custom_tree as a side-effect
} else {
- 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');
+ this.tree_type.active(this.active ? 't' : 'f');
+ }
+ if (this.aouctn_root) {
+ this.ingestCustomTree(this.aouctn_root); // sets this.custom_tree as a side-effect
+ } else {
+ this.custom_tree = this.tree.clone();
}
return this.custom_tree;
}
}
async applyChanges() {
- if (this.active !== this.tree_type.active()) {
- this.tree_type.active(this.active);
+ if (this.active !== (this.tree_type.active() === 't')) {
+ this.tree_type.active(this.active ? 't' : 'f');
this.tree_type.ischanged('t');
}
try {
await firstValueFrom(this.pcrud.update(this.tree_type));
}
await this.createNewNodes(this.custom_tree.rootNode);
+ this.successString.current().then(str => this.toast.success(str));
+
} catch (error) {
console.error('Error applying changes:', error);
+ this.updateFailedString.current().then(str => this.toast.danger(str));
}
}
async createNewNodes(node: any, parent_id: number = null, order: number = 0) {
+ // delete the existing custom nodes for the custom tree
+ // TODO: this is what the dojo interface did, but do we really need so much churn?
+ // TODO: we may want to move this to an OpenSRF method so we can wrap the entire
+ // delete and create into a single transaction
+ if (this.aouctn_root) {
+ await lastValueFrom(this.pcrud.remove(this.aouctn_root));
+ this.aouctn_root = null;
+ }
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();
+ 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));