]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/server/org-unit-type.component.ts
LP2061136 - Stamping 1405 DB upgrade script
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / org-unit-type.component.ts
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';
11
12 @Component({
13     templateUrl: './org-unit-type.component.html',
14     styleUrls: [ './org-unit.component.css' ],
15 })
16
17 export class OrgUnitTypeComponent implements OnInit {
18
19     tree: Tree;
20     selected: TreeNode;
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;
26
27     constructor(
28         private idl: IdlService,
29         private org: OrgService,
30         private auth: AuthService,
31         private pcrud: PcrudService,
32         private toast: ToastService
33     ) {}
34
35
36     ngOnInit() {
37         this.loadAoutTree();
38     }
39
40     loadAoutTree() {
41         this.pcrud.search('aout', {depth: 0},
42             {flesh: -1, flesh_fields: {aout: ['children', 'org_units']}},
43             {anonymous: true}
44         ).subscribe(aoutTree => this.ingestAoutTree(aoutTree));
45     }
46
47     // Translate the org unt type tree into a structure EgTree can use.
48     ingestAoutTree(aoutTree) {
49
50         const handleNode = (aoutNode: IdlObject): TreeNode => {
51             if (!aoutNode) { return; }
52
53             // grab number of associated org units, then
54             // clear it so that FmRecordEditor doesn't try
55             // to render the list
56             const orgCount = aoutNode.org_units().length;
57             aoutNode.org_units(null);
58
59             const treeNode = new TreeNode({
60                 id: aoutNode.id(),
61                 label: aoutNode.name(),
62                 callerData: { aout: aoutNode, orgCount: orgCount },
63             });
64
65             aoutNode.children().forEach(childNode =>
66                 treeNode.children.push(handleNode(childNode))
67             );
68
69             return treeNode;
70         };
71
72         const rootNode = handleNode(aoutTree);
73         this.tree = new Tree(rootNode);
74     }
75
76     nodeClicked($event: any) {
77         this.selected = $event;
78     }
79
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(
85             ok => {
86                 message.current().then(str => this.toast.success(str));
87             }
88         );
89     }
90
91     edit() {
92         this.editDialog.mode = 'update';
93         this.editDialog.setRecord(this.selected.callerData.aout);
94
95         this.editDialog.open().subscribe(
96             success => {
97                 this.postUpdate(this.editString);
98                 this.loadAoutTree(); // since the tree is never going to
99                 // be large, just reload the whole
100                 // thing
101             },
102             // eslint-disable-next-line rxjs/no-implicit-any-catch
103             (rejected: any) => {
104                 if (rejected && rejected.dismissed) {
105                     return;
106                 }
107                 this.errorString.current()
108                     .then(str => this.toast.danger(str));
109             }
110         );
111     }
112
113     remove() {
114         this.delConfirm.open().subscribe(confirmed => {
115             if (!confirmed) { return; }
116
117             this.pcrud.remove(this.selected.callerData.aout)
118                 // eslint-disable-next-line rxjs/no-nested-subscribe
119                 .subscribe(
120                     ok2 => {},
121                     (err: unknown) => {
122                         this.errorString.current()
123                             .then(str => this.toast.danger(str));
124                     },
125                     ()  => {
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
130                         // thing
131                         this.selected = null;
132                         this.postUpdate(this.editString);
133                     }
134                 );
135         });
136     }
137
138     addChild() {
139         const parentTreeNode = this.selected;
140         const parentType = parentTreeNode.callerData.aout;
141
142         const newType = this.idl.create('aout');
143         newType.parent(parentType.id());
144         newType.depth(Number(parentType.depth()) + 1);
145
146         this.editDialog.setRecord(newType);
147         this.editDialog.mode = 'create';
148
149         this.editDialog.open().subscribe(
150             result => { // aout object
151
152                 // Add our new node to the tree
153                 const newNode = new TreeNode({
154                     id: result.id(),
155                     label: result.name(),
156                     callerData: { aout: result, orgCount: 0 }
157                 });
158                 this.loadAoutTree(); // since the tree is never going to
159                 // be large, just reload the whole
160                 // thing
161                 this.postUpdate(this.createString);
162             },
163
164             // eslint-disable-next-line rxjs/no-implicit-any-catch
165             (rejected: any) => {
166                 if (rejected && rejected.dismissed) {
167                     return;
168                 }
169                 this.errorString.current()
170                     .then(str => this.toast.danger(str));
171             }
172         );
173     }
174 }
175