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';
15 templateUrl: './survey.component.html'
18 export class SurveyComponent implements OnInit {
20 gridDataSource: GridDataSource;
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;
33 @Input() sortField: string;
34 @Input() idlClass = 'asv';
35 @Input() dialogSize: 'sm' | 'lg' = 'lg';
38 private auth: AuthService,
39 private net: NetService,
40 private pcrud: PcrudService,
41 private toast: ToastService,
42 private router: Router
44 this.gridDataSource = new GridDataSource();
48 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
49 const orderBy: any = {};
51 // Sort specified from grid
52 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
53 } else if (this.sortField) {
55 orderBy[this.idlClass] = this.sortField;
63 return this.pcrud.retrieveAll('asv', searchOps, {});
66 this.grid.onRowActivate.subscribe(
67 (idlThing: IdlObject) => {
68 const idToEdit = idlThing.id();
69 this.navigateToEditPage(idToEdit);
74 showEditDialog(idlThing: IdlObject): Promise<any> {
78 editSelected = (surveys: IdlObject[]) => {
79 const idToEdit = surveys[0].id();
80 this.navigateToEditPage(idToEdit);
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(
89 this.toast.success(await this.endSurveySuccessString.current());
92 this.toast.warning(await this.endSurveyFailedString.current());
98 deleteSelected = (surveys: IdlObject[]) => {
99 for (let i = 0; i < surveys.length; i++) {
100 const idToDelete = surveys[i].id();
103 'open-ils.circ.survey.delete.cascade.override',
104 this.auth.token(), idToDelete
106 this.deleteSuccessString.current()
107 .then(str => this.toast.success(str));
111 this.deleteFailedString.current()
112 .then(str => this.toast.success(str));
117 navigateToEditPage(id: any) {
118 this.router.navigate(['/staff/admin/local/action/survey/' + id]);
122 this.editDialog.mode = 'create';
123 this.editDialog.datetimeFields = 'start_date,end_date';
124 this.editDialog.open({size: this.dialogSize}).subscribe(
126 this.createString.current()
127 .then(str => this.toast.success(str));
131 if (!rejection.dismissed) {
132 this.createErrString.current()
133 .then(str => this.toast.danger(str));