1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {Tree, TreeNode} from '@eg/share/tree/tree';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {OrgService} from '@eg/core/org.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
10 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
13 templateUrl: './org-unit-type.component.html',
14 styleUrls: [ './org-unit.component.css' ],
17 export class OrgUnitTypeComponent implements OnInit {
21 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
22 @ViewChild('editString', { static: true }) editString: StringComponent;
23 @ViewChild('createString', { static: true }) createString: StringComponent;
24 @ViewChild('errorString', { static: true }) errorString: StringComponent;
25 @ViewChild('delConfirm', { static: true }) delConfirm: ConfirmDialogComponent;
28 private idl: IdlService,
29 private org: OrgService,
30 private auth: AuthService,
31 private pcrud: PcrudService,
32 private toast: ToastService
41 this.pcrud.search('aout', {depth: 0},
42 {flesh: -1, flesh_fields: {aout: ['children', 'org_units']}},
44 ).subscribe(aoutTree => this.ingestAoutTree(aoutTree));
47 // Translate the org unt type tree into a structure EgTree can use.
48 ingestAoutTree(aoutTree) {
50 const handleNode = (aoutNode: IdlObject): TreeNode => {
51 if (!aoutNode) { return; }
53 // grab number of associated org units, then
54 // clear it so that FmRecordEditor doesn't try
56 const orgCount = aoutNode.org_units().length;
57 aoutNode.org_units(null);
59 const treeNode = new TreeNode({
61 label: aoutNode.name(),
62 callerData: { aout: aoutNode, orgCount: orgCount },
65 aoutNode.children().forEach(childNode =>
66 treeNode.children.push(handleNode(childNode))
72 const rootNode = handleNode(aoutTree);
73 this.tree = new Tree(rootNode);
76 nodeClicked($event: any) {
77 this.selected = $event;
80 postUpdate(message: StringComponent) {
81 // Modifying org unit types means refetching the org unit
82 // data normally fetched on page load, since it includes
83 // org unit type data.
84 this.org.fetchOrgs().then(
86 message.current().then(str => this.toast.success(str));
92 this.editDialog.mode = 'update';
93 this.editDialog.setRecord(this.selected.callerData.aout);
95 this.editDialog.open().subscribe(
97 this.postUpdate(this.editString);
98 this.loadAoutTree(); // since the tree is never going to
99 // be large, just reload the whole
102 // eslint-disable-next-line rxjs/no-implicit-any-catch
104 if (rejected && rejected.dismissed) {
107 this.errorString.current()
108 .then(str => this.toast.danger(str));
114 this.delConfirm.open().subscribe(confirmed => {
115 if (!confirmed) { return; }
117 this.pcrud.remove(this.selected.callerData.aout)
118 // eslint-disable-next-line rxjs/no-nested-subscribe
122 this.errorString.current()
123 .then(str => this.toast.danger(str));
126 // Avoid updating until we know the entire
127 // pcrud action/transaction completed.
128 this.loadAoutTree(); // since the tree is never going to
129 // be large, just reload the whole
131 this.selected = null;
132 this.postUpdate(this.editString);
139 const parentTreeNode = this.selected;
140 const parentType = parentTreeNode.callerData.aout;
142 const newType = this.idl.create('aout');
143 newType.parent(parentType.id());
144 newType.depth(Number(parentType.depth()) + 1);
146 this.editDialog.setRecord(newType);
147 this.editDialog.mode = 'create';
149 this.editDialog.open().subscribe(
150 result => { // aout object
152 // Add our new node to the tree
153 const newNode = new TreeNode({
155 label: result.name(),
156 callerData: { aout: result, orgCount: 0 }
158 this.loadAoutTree(); // since the tree is never going to
159 // be large, just reload the whole
161 this.postUpdate(this.createString);
164 // eslint-disable-next-line rxjs/no-implicit-any-catch
166 if (rejected && rejected.dismissed) {
169 this.errorString.current()
170 .then(str => this.toast.danger(str));