]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/triggers/trigger-edit.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[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, Router} 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 {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     evtReactor: string;
24     evtAltEligible: Boolean = false;
25
26     testErr1: String = '';
27     testErr2: String = '';
28     testResult: String = '';
29     testDone: Boolean = false;
30
31     altTemplateDataSource: GridDataSource = new GridDataSource();
32     envDataSource: GridDataSource = new GridDataSource();
33     paramDataSource: GridDataSource = new GridDataSource();
34
35     editTab: 'def' | 'alt' | 'env' | 'param' | 'test' = 'def';
36
37     @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
38     @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
39     @ViewChild('altTemplateDialog') altTemplateDialog: FmRecordEditorComponent;
40
41     @ViewChild('envGrid') envGrid: GridComponent;
42     @ViewChild('paramGrid') paramGrid: GridComponent;
43     @ViewChild('altTemplateGrid') altTemplateGrid: GridComponent;
44
45     @ViewChild('updateSuccessString') updateSuccessString: StringComponent;
46     @ViewChild('updateFailedString') updateFailedString: StringComponent;
47     @ViewChild('cloneSuccessString') cloneSuccessString: StringComponent;
48     @ViewChild('cloneFailedString') cloneFailedString: StringComponent;
49     @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
50     @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
51     @ViewChild('createSuccessString') createSuccessString: StringComponent;
52     @ViewChild('createErrString') createErrString: StringComponent;
53     @ViewChild('eventDuringTestString') eventDuringTestString: StringComponent;
54     @ViewChild('errorDuringTestString') errorDuringTestString: StringComponent;
55
56     constructor(
57         private router: Router,
58         private idl: IdlService,
59         private pcrud: PcrudService,
60         private toast: ToastService,
61         private route: ActivatedRoute,
62         private net: NetService,
63         private auth: AuthService,
64     ) {
65     }
66
67     ngOnInit() {
68         this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
69
70         // get current event def name to display on the banner
71         this.pcrud.search('atevdef',
72             {id: this.evtDefId}, {}).toPromise().then(rec => {
73             this.evtDefName = rec.name();
74         });
75
76         // get current event def reactor to decide if the alt template tab should show
77         this.pcrud.search('atevdef',
78             {id: this.evtDefId}, {}).toPromise().then(rec => {
79             this.evtReactor = rec.reactor();
80             if ('ProcessTemplate SendEmail SendSMS'.indexOf(this.evtReactor) > -1) { this.evtAltEligible = true; }
81         });
82
83         this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
84             return this.pcrud.search('atenv',
85                 {event_def: this.evtDefId}, {});
86         };
87
88         this.altTemplateDataSource.getRows = (pager: Pager, sort: any[]) => {
89             return this.pcrud.search('atevalt',
90                 {event_def: this.evtDefId}, {});
91         };
92
93         this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
94             return this.pcrud.search('atevparam',
95                 {event_def: this.evtDefId}, {});
96         };
97     }
98
99     onTabChange(event: NgbNavChangeEvent) {
100         this.editTab = event.nextId;
101     }
102
103     createNewEnv = () => {
104         this.createNewThing(this.envDialog, this.envGrid, 'atenv');
105     };
106
107     createNewAltTemplate = () => {
108         this.createNewThing(this.altTemplateDialog, this.altTemplateGrid, 'atevalt');
109     };
110
111     createNewParam = () => {
112         this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
113     };
114
115     createNewThing = (currentDialog: any, currentGrid: any, idl: any) => {
116         currentDialog.mode = 'create';
117         currentDialog.recordId = null;
118         const newRecord = this.idl.create(idl);
119         newRecord.event_def(this.evtDefId);
120         currentDialog.mode = 'create';
121         currentDialog.record = newRecord;
122         currentDialog.open({size: 'lg'}).subscribe(
123             ok => {
124                 this.createSuccessString.current()
125                     .then(str => this.toast.success(str));
126                 currentGrid.reload();
127             },
128             rejection => {
129                 if (!rejection.dismissed) {
130                     this.createErrString.current()
131                         .then(str => this.toast.danger(str));
132                 }
133             }
134         );
135     };
136
137     deleteSelected = (idlThings: IdlObject[]) => {
138         let currentGrid;
139         if (idlThings[0].classname === 'atenv') {
140             currentGrid = this.envGrid;
141         } else if (idlThings[0].classname === 'atevalt') {
142             currentGrid = this.altTemplateGrid;
143         } else {
144             currentGrid = this.paramGrid;
145         }
146         idlThings.forEach(idlThing => idlThing.isdeleted(true));
147         let _deleted = 0;
148         this.pcrud.autoApply(idlThings).subscribe(
149             val => {
150                 console.debug('deleted: ' + val);
151                 this.deleteSuccessString.current()
152                     .then(str => this.toast.success(str));
153                 _deleted++;
154             },
155             (err: unknown) => {
156                 this.deleteFailedString.current()
157                     .then(str => this.toast.danger(str));
158             },
159             () => {
160                 if (_deleted > 0) {
161                     currentGrid.reload();
162                 }
163             }
164         );
165     };
166
167     editSelected = (selectedRecords: IdlObject[]) => {
168         const editOneThing = (record: IdlObject) => {
169             if (!record) { return; }
170             this.showEditDialog(record).then(
171                 () => editOneThing(selectedRecords.shift()));
172         };
173         editOneThing(selectedRecords.shift());
174     };
175
176     showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
177         let currentDialog;
178         let currentGrid;
179         if (selectedRecord.classname === 'atenv') {
180             currentDialog = this.envDialog;
181             currentGrid = this.envGrid;
182         } else if (selectedRecord.classname === 'atevalt') {
183             currentDialog = this.altTemplateDialog;
184             currentGrid = this.altTemplateGrid;
185         } else {
186             currentDialog = this.paramDialog;
187             currentGrid = this.paramGrid;
188         }
189         currentDialog.mode = 'update';
190         const clone = this.idl.clone(selectedRecord);
191         currentDialog.record = clone;
192         return new Promise((resolve, reject) => {
193             currentDialog.open({size: 'lg'}).subscribe(
194                 result => {
195                     this.updateSuccessString.current()
196                         .then(str => this.toast.success(str));
197                     currentGrid.reload();
198                     resolve(result);
199                 },
200                 error => {
201                     this.updateFailedString.current()
202                         .then(str => this.toast.danger(str));
203                     reject(error);
204                 }
205             );
206         });
207     };
208
209     runTest = (barcode) => {
210         if (!barcode) {
211             return;
212         }
213         this.clearTestResults();
214         this.net.request(
215             'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
216             this.auth.token(), this.evtDefId, barcode
217         ).subscribe(res => {
218             this.testDone = true;
219             if (res.ilsevent) {
220                 this.eventDuringTestString.current({ ilsevent: res.ilsevent, textcode : res.textcode})
221                     .then(str => this.testErr1 = str);
222                 this.testErr2 = res.desc;
223             } else {
224                 this.testResult = res.template_output().data();
225             }
226         // eslint-disable-next-line rxjs/no-implicit-any-catch
227         }, (err: any) => {
228             this.testDone = true;
229             this.errorDuringTestString.current().then(str => this.testErr1 = str);
230             this.testErr2 = err;
231         });
232     };
233
234     clearTestResults = () => {
235         this.testDone = false;
236         this.testErr1 = '';
237         this.testErr2 = '';
238         this.testResult = '';
239     };
240     back = () => {
241         this.router.navigate(['/staff/admin/local/action_trigger/event_definition/']);
242     };
243 }