]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/survey/survey.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / survey / survey.component.ts
1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, Input, ViewChild} from '@angular/core';
3 import {GridComponent} from '@eg/share/grid/grid.component';
4 import {GridDataSource} from '@eg/share/grid/grid';
5 import {Router} from '@angular/router';
6 import {IdlObject, IdlService} from '@eg/core/idl.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
9 import {StringComponent} from '@eg/share/string/string.component';
10 import {ToastService} from '@eg/share/toast/toast.service';
11 import {NetService} from '@eg/core/net.service';
12 import {AuthService} from '@eg/core/auth.service';
13
14 @Component({
15     templateUrl: './survey.component.html'
16 })
17
18 export class SurveyComponent implements OnInit {
19
20     defaultNewRecord: IdlObject;
21     gridDataSource: GridDataSource;
22
23     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
24     @ViewChild('grid', { static: true }) grid: GridComponent;
25     @ViewChild('successString', { static: true }) successString: StringComponent;
26     @ViewChild('createString', { static: true }) createString: StringComponent;
27     @ViewChild('createErrString', { static: true }) createErrString: StringComponent;
28     @ViewChild('updateFailedString', { static: true }) updateFailedString: StringComponent;
29     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
30     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
31     @ViewChild('endSurveyFailedString', { static: true }) endSurveyFailedString: StringComponent;
32     @ViewChild('endSurveySuccessString', { static: true }) endSurveySuccessString: StringComponent;
33
34     @Input() sortField: string;
35     @Input() idlClass = 'asv';
36     @Input() dialogSize: 'sm' | 'lg' = 'lg';
37
38     constructor(
39         private auth: AuthService,
40         private idl: IdlService,
41         private net: NetService,
42         private pcrud: PcrudService,
43         private toast: ToastService,
44         private router: Router
45     ) {
46         this.gridDataSource = new GridDataSource();
47     }
48
49     ngOnInit() {
50         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
51             const orderBy: any = {};
52             if (sort.length) {
53                 // Sort specified from grid
54                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
55             } else if (this.sortField) {
56                 // Default sort field
57                 orderBy[this.idlClass] = this.sortField;
58             }
59
60             const searchOps = {
61                 offset: pager.offset,
62                 limit: pager.limit,
63                 order_by: orderBy
64             };
65             return this.pcrud.retrieveAll('asv', searchOps, {});
66         };
67
68         this.grid.onRowActivate.subscribe(
69             (idlThing: IdlObject) => {
70                 const idToEdit = idlThing.id();
71                 this.navigateToEditPage(idToEdit);
72             }
73         );
74
75         this.defaultNewRecord = this.idl.create('asv');
76         const nextWeek = new Date();
77         // eslint-disable-next-line no-magic-numbers
78         nextWeek.setDate(nextWeek.getDate() + 7);
79         this.defaultNewRecord.end_date(nextWeek.toISOString());
80     }
81
82     showEditDialog(idlThing: IdlObject): Promise<any> {
83         return;
84     }
85
86     editSelected = (surveys: IdlObject[]) => {
87         const idToEdit = surveys[0].id();
88         this.navigateToEditPage(idToEdit);
89     };
90
91     endSurvey = (surveys: IdlObject[]) => {
92         const today = new Date().toISOString();
93         for (let i = 0; i < surveys.length; i++) {
94             surveys[i].end_date(today);
95             this.pcrud.update(surveys[i]).toPromise().then(
96                 async (ok) => {
97                     this.toast.success(await this.endSurveySuccessString.current());
98                 },
99                 async (err) => {
100                     this.toast.warning(await this.endSurveyFailedString.current());
101                 }
102             );
103         }
104     };
105
106     deleteSelected = (surveys: IdlObject[]) => {
107         for (let i = 0; i < surveys.length; i++) {
108             const idToDelete = surveys[i].id();
109             this.net.request(
110                 'open-ils.circ',
111                 'open-ils.circ.survey.delete.cascade.override',
112                 this.auth.token(), idToDelete
113             ).subscribe(res => {
114                 this.deleteSuccessString.current()
115                     .then(str => this.toast.success(str));
116                 this.grid.reload();
117                 return res;
118             }, (err: unknown) => {
119                 this.deleteFailedString.current()
120                     .then(str => this.toast.success(str));
121             });
122         }
123     };
124
125     navigateToEditPage(id: any) {
126         this.router.navigate(['/staff/admin/local/action/survey/' + id]);
127     }
128
129     createNew = () => {
130         this.editDialog.mode = 'create';
131         this.editDialog.datetimeFields = 'start_date,end_date';
132         this.editDialog.open({size: this.dialogSize}).subscribe(
133             ok => {
134                 this.createString.current()
135                     .then(str => this.toast.success(str));
136                 this.grid.reload();
137             },
138             // eslint-disable-next-line rxjs/no-implicit-any-catch
139             (rejection: any) => {
140                 if (!rejection.dismissed) {
141                     this.createErrString.current()
142                         .then(str => this.toast.danger(str));
143                 }
144             }
145         );
146     };
147 }