9865a4b3e63578b044037650100093ea2159d921
[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} 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     gridDataSource: GridDataSource;
21
22     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
23     @ViewChild('grid', { static: true }) grid: GridComponent;
24     @ViewChild('successString', { static: true }) successString: StringComponent;
25     @ViewChild('createString', { static: true }) createString: StringComponent;
26     @ViewChild('createErrString', { static: true }) createErrString: StringComponent;
27     @ViewChild('updateFailedString', { static: true }) updateFailedString: StringComponent;
28     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
29     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
30     @ViewChild('endSurveyFailedString', { static: true }) endSurveyFailedString: StringComponent;
31     @ViewChild('endSurveySuccessString', { static: true }) endSurveySuccessString: StringComponent;
32
33     @Input() dialogSize: 'sm' | 'lg' = 'lg';
34
35     constructor(
36         private auth: AuthService,
37         private net: NetService,
38         private pcrud: PcrudService,
39         private toast: ToastService,
40         private router: Router
41     ) {
42         this.gridDataSource = new GridDataSource();
43     }
44
45     ngOnInit() {
46         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
47             return this.pcrud.retrieveAll('asv', {});
48         };
49
50         this.grid.onRowActivate.subscribe(
51             (idlThing: IdlObject) => {
52                 const idToEdit = idlThing.id();
53                 this.navigateToEditPage(idToEdit);
54             }
55         );
56     }
57
58     showEditDialog(idlThing: IdlObject): Promise<any> {
59         return;
60     }
61
62     editSelected = (surveys: IdlObject[]) => {
63         const idToEdit = surveys[0].id();
64         this.navigateToEditPage(idToEdit);
65     }
66
67     endSurvey = (surveys: IdlObject[]) => {
68         const today = new Date().toISOString();
69         for (let i = 0; i < surveys.length; i++) {
70             surveys[i].end_date(today);
71             this.pcrud.update(surveys[i]).toPromise().then(
72                 async (ok) => {
73                     this.toast.success(await this.endSurveySuccessString.current());
74                 },
75                 async (err) => {
76                     this.toast.warning(await this.endSurveyFailedString.current());
77                 }
78             );
79         }
80     }
81
82     deleteSelected = (surveys: IdlObject[]) => {
83         for (let i = 0; i < surveys.length; i++) {
84             const idToDelete = surveys[i].id();
85             this.net.request(
86                 'open-ils.circ',
87                 'open-ils.circ.survey.delete.cascade.override',
88                 this.auth.token(), idToDelete
89             ).subscribe(res => {
90                 this.deleteSuccessString.current()
91                     .then(str => this.toast.success(str));
92                 this.grid.reload();
93                 return res;
94             }, (err) => {
95                 this.deleteFailedString.current()
96                     .then(str => this.toast.success(str));
97             });
98         }
99     }
100
101     navigateToEditPage(id: any) {
102         this.router.navigate(['/staff/admin/local/action/survey/' + id]);
103     }
104
105     createNew = () => {
106         this.editDialog.mode = 'create';
107         this.editDialog.datetimeFields = 'start_date,end_date';
108         this.editDialog.open({size: this.dialogSize}).subscribe(
109             ok => {
110                 this.createString.current()
111                     .then(str => this.toast.success(str));
112                 this.grid.reload();
113             },
114             rejection => {
115                 if (!rejection.dismissed) {
116                     this.createErrString.current()
117                         .then(str => this.toast.danger(str));
118                 }
119             }
120         );
121     }
122 }