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'
16 export class OrgUnitTypeComponent implements OnInit {
20 @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
21 @ViewChild('editString') editString: StringComponent;
22 @ViewChild('createString') createString: StringComponent;
23 @ViewChild('errorString') errorString: StringComponent;
24 @ViewChild('delConfirm') delConfirm: ConfirmDialogComponent;
27 private idl: IdlService,
28 private org: OrgService,
29 private auth: AuthService,
30 private pcrud: PcrudService,
31 private toast: ToastService
40 this.pcrud.search('aout', {depth: 0},
41 {flesh: -1, flesh_fields: {aout: ['children', 'org_units']}},
43 ).subscribe(aoutTree => this.ingestAoutTree(aoutTree));
46 // Translate the org unt type tree into a structure EgTree can use.
47 ingestAoutTree(aoutTree) {
49 const handleNode = (aoutNode: IdlObject): TreeNode => {
50 if (!aoutNode) { return; }
52 // grab number of associated org units, then
53 // clear it so that FmRecordEditor doesn't try
55 const orgCount = aoutNode.org_units().length;
56 aoutNode.org_units(null);
58 const treeNode = new TreeNode({
60 label: aoutNode.name(),
61 callerData: { aout: aoutNode, orgCount: orgCount },
64 aoutNode.children().forEach(childNode =>
65 treeNode.children.push(handleNode(childNode))
71 const rootNode = handleNode(aoutTree);
72 this.tree = new Tree(rootNode);
75 nodeClicked($event: any) {
76 this.selected = $event;
79 postUpdate(message: StringComponent) {
80 // Modifying org unit types means refetching the org unit
81 // data normally fetched on page load, since it includes
82 // org unit type data.
83 this.org.fetchOrgs().then(
85 message.current().then(str => this.toast.success(str));
91 this.editDialog.mode = 'update';
92 this.editDialog.setRecord(this.selected.callerData.aout);
94 this.editDialog.open().then(
96 this.postUpdate(this.editString);
97 this.loadAoutTree(); // since the tree is never going to
98 // be large, just reload the whole
102 if (rejected && rejected.dismissed) {
105 this.errorString.current()
106 .then(str => this.toast.danger(str));
112 this.delConfirm.open().then(
114 this.pcrud.remove(this.selected.callerData.aout)
118 this.errorString.current()
119 .then(str => this.toast.danger(str));
122 // Avoid updating until we know the entire
123 // pcrud action/transaction completed.
124 this.loadAoutTree(); // since the tree is never going to
125 // be large, just reload the whole
127 this.selected = null;
128 this.postUpdate(this.editString);
137 const parentTreeNode = this.selected;
138 const parentType = parentTreeNode.callerData.aout;
140 const newType = this.idl.create('aout');
141 newType.parent(parentType.id());
142 newType.depth(Number(parentType.depth()) + 1);
144 this.editDialog.setRecord(newType);
145 this.editDialog.mode = 'create';
147 this.editDialog.open().then(
148 result => { // aout object
150 // Add our new node to the tree
151 const newNode = new TreeNode({
153 label: result.name(),
154 callerData: { aout: result, orgCount: 0 }
156 this.loadAoutTree(); // since the tree is never going to
157 // be large, just reload the whole
159 this.postUpdate(this.createString);
163 if (rejected && rejected.dismissed) {
166 this.errorString.current()
167 .then(str => this.toast.danger(str));