]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/triggers/trigger-edit.component.ts
LP#1855780 Angular Notification/Action Triggers port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / triggers / trigger-edit.component.ts
1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, ViewChild} from '@angular/core';
3 import {GridComponent} from '@eg/share/grid/grid.component';
4 import {GridDataSource} from '@eg/share/grid/grid';
5 import {ActivatedRoute} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {NetService} from '@eg/core/net.service';
9 import {AuthService} from '@eg/core/auth.service';
10 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {ToastService} from '@eg/share/toast/toast.service';
13 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
14
15 @Component({
16     templateUrl: './trigger-edit.component.html'
17 })
18
19 export class EditEventDefinitionComponent implements OnInit {
20
21     evtDefId: number;
22     evtDefName: String;
23
24     testErr1: String = '';
25     testErr2: String = '';
26     testResult: String = '';
27     testDone: Boolean = false;
28
29     envDataSource: GridDataSource = new GridDataSource();
30     paramDataSource: GridDataSource = new GridDataSource();
31
32     editTab: 'def' | 'env' | 'param' | 'test' = 'def';
33
34     @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
35     @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
36
37     @ViewChild('envGrid') envGrid: GridComponent;
38     @ViewChild('paramGrid') paramGrid: GridComponent;
39
40     @ViewChild('updateSuccessString') updateSuccessString: StringComponent;
41     @ViewChild('updateFailedString') updateFailedString: StringComponent;
42     @ViewChild('cloneSuccessString') cloneSuccessString: StringComponent;
43     @ViewChild('cloneFailedString') cloneFailedString: StringComponent;
44     @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
45     @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
46     @ViewChild('createSuccessString') createSuccessString: StringComponent;
47     @ViewChild('createErrString') createErrString: StringComponent;
48
49     constructor(
50         private idl: IdlService,
51         private pcrud: PcrudService,
52         private toast: ToastService,
53         private route: ActivatedRoute,
54         private net: NetService,
55         private auth: AuthService,
56     ) {
57     }
58
59     ngOnInit() {
60         this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
61
62         // get current event def name to display on the banner
63         this.pcrud.search('atevdef',
64             {id: this.evtDefId}, {}).toPromise().then(rec => {
65             this.evtDefName = rec.name();
66         });
67
68         this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
69             return this.pcrud.search('atenv',
70                 {event_def: this.evtDefId}, {});
71         };
72
73         this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
74             return this.pcrud.search('atevparam',
75                 {event_def: this.evtDefId}, {});
76         };
77     }
78
79     onTabChange(event: NgbNavChangeEvent) {
80         this.editTab = event.nextId;
81     }
82
83     createNewEnv = () => {
84         this.createNewThing(this.envDialog, this.envGrid, 'atenv');
85     }
86
87     createNewParam = () => {
88         this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
89     }
90
91     createNewThing = (currentDialog: any, currentGrid: any, idl: any) => {
92         currentDialog.mode = 'create';
93         currentDialog.recordId = null;
94         const newRecord = this.idl.create(idl);
95         newRecord.event_def(this.evtDefId);
96         currentDialog.mode = 'create';
97         currentDialog.record = newRecord;
98         currentDialog.open({size: 'lg'}).subscribe(
99             ok => {
100                 this.createSuccessString.current()
101                     .then(str => this.toast.success(str));
102                 currentGrid.reload();
103             },
104             rejection => {
105                 if (!rejection.dismissed) {
106                     this.createErrString.current()
107                         .then(str => this.toast.danger(str));
108                 }
109             }
110         );
111     }
112
113     deleteSelected = (idlThings: IdlObject[]) => {
114         let currentGrid;
115         if (idlThings[0].classname === 'atenv') {
116             currentGrid = this.envGrid;
117         } else {
118             currentGrid = this.paramGrid;
119         }
120         idlThings.forEach(idlThing => idlThing.isdeleted(true));
121         this.pcrud.autoApply(idlThings).subscribe(
122             val => {
123                 console.debug('deleted: ' + val);
124                 this.deleteSuccessString.current()
125                     .then(str => this.toast.success(str));
126                 currentGrid.reload();
127             },
128             err => {
129                 this.deleteFailedString.current()
130                     .then(str => this.toast.danger(str));
131             }
132         );
133     }
134
135     editSelected = (selectedRecords: IdlObject[]) => {
136         const editOneThing = (record: IdlObject) => {
137             if (!record) { return; }
138             this.showEditDialog(record).then(
139                 () => editOneThing(selectedRecords.shift()));
140         };
141         editOneThing(selectedRecords.shift());
142     }
143
144     showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
145         let currentDialog;
146         let currentGrid;
147         if (selectedRecord.classname === 'atenv') {
148             currentDialog = this.envDialog;
149             currentGrid = this.envGrid;
150         } else {
151             currentDialog = this.paramDialog;
152             currentGrid = this.paramGrid;
153         }
154         currentDialog.mode = 'update';
155         const clone = this.idl.clone(selectedRecord);
156         currentDialog.record = clone;
157         return new Promise((resolve, reject) => {
158             currentDialog.open({size: 'lg'}).subscribe(
159                 result => {
160                     this.updateSuccessString.current()
161                         .then(str => this.toast.success(str));
162                     currentGrid.reload();
163                     resolve(result);
164                 },
165                 error => {
166                     this.updateFailedString.current()
167                         .then(str => this.toast.danger(str));
168                     reject(error);
169                }
170            );
171        });
172     }
173
174     runTest = (barcode) => {
175         if (!barcode) {
176             return;
177         }
178         this.clearTestResults();
179         this.net.request(
180             'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
181             this.auth.token(), this.evtDefId, barcode
182         ).subscribe(res => {
183             this.testDone = true;
184             if (res.ilsevent) {
185                 this.testErr1 = 'Event:  ' + res.ilsevent + ':  ' + res.textcode + ' ->';
186                 this.testErr2 = res.desc;
187             } else {
188                 this.testResult = res.template_output().data();
189             }
190         });
191     }
192
193     clearTestResults = () => {
194         this.testDone = false;
195         this.testErr1 = '';
196         this.testErr2 = '';
197         this.testResult = '';
198     }
199 }