7bec134b894237029c73fdbab6f1771ca991e963
[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() sortField: string;
34     @Input() idlClass='asv';
35     @Input() dialogSize: 'sm' | 'lg' = 'lg';
36
37     constructor(
38         private auth: AuthService,
39         private net: NetService,
40         private pcrud: PcrudService,
41         private toast: ToastService,
42         private router: Router
43     ) {
44         this.gridDataSource = new GridDataSource();
45     }
46
47     ngOnInit() {
48         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
49             const orderBy: any = {};
50             if (sort.length) {
51                 // Sort specified from grid
52                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
53             } else if (this.sortField) {
54                 // Default sort field
55                 orderBy[this.idlClass] = this.sortField;
56             }
57
58             const searchOps = {
59                 offset: pager.offset,
60                 limit: pager.limit,
61                 order_by: orderBy
62             };
63             return this.pcrud.retrieveAll('asv', searchOps, {});
64         };
65
66         this.grid.onRowActivate.subscribe(
67             (idlThing: IdlObject) => {
68                 const idToEdit = idlThing.id();
69                 this.navigateToEditPage(idToEdit);
70             }
71         );
72     }
73
74     showEditDialog(idlThing: IdlObject): Promise<any> {
75         return;
76     }
77
78     editSelected = (surveys: IdlObject[]) => {
79         const idToEdit = surveys[0].id();
80         this.navigateToEditPage(idToEdit);
81     }
82
83     endSurvey = (surveys: IdlObject[]) => {
84         const today = new Date().toISOString();
85         for (let i = 0; i < surveys.length; i++) {
86             surveys[i].end_date(today);
87             this.pcrud.update(surveys[i]).toPromise().then(
88                 async (ok) => {
89                     this.toast.success(await this.endSurveySuccessString.current());
90                 },
91                 async (err) => {
92                     this.toast.warning(await this.endSurveyFailedString.current());
93                 }
94             );
95         }
96     }
97
98     deleteSelected = (surveys: IdlObject[]) => {
99         for (let i = 0; i < surveys.length; i++) {
100             const idToDelete = surveys[i].id();
101             this.net.request(
102                 'open-ils.circ',
103                 'open-ils.circ.survey.delete.cascade.override',
104                 this.auth.token(), idToDelete
105             ).subscribe(res => {
106                 this.deleteSuccessString.current()
107                     .then(str => this.toast.success(str));
108                 this.grid.reload();
109                 return res;
110             }, (err) => {
111                 this.deleteFailedString.current()
112                     .then(str => this.toast.success(str));
113             });
114         }
115     }
116
117     navigateToEditPage(id: any) {
118         this.router.navigate(['/staff/admin/local/action/survey/' + id]);
119     }
120
121     createNew = () => {
122         this.editDialog.mode = 'create';
123         this.editDialog.datetimeFields = 'start_date,end_date';
124         this.editDialog.open({size: this.dialogSize}).subscribe(
125             ok => {
126                 this.createString.current()
127                     .then(str => this.toast.success(str));
128                 this.grid.reload();
129             },
130             rejection => {
131                 if (!rejection.dismissed) {
132                     this.createErrString.current()
133                         .then(str => this.toast.danger(str));
134                 }
135             }
136         );
137     }
138 }