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() dialogSize: 'sm' | 'lg' = 'lg';
36 private auth: AuthService,
37 private net: NetService,
38 private pcrud: PcrudService,
39 private toast: ToastService,
40 private router: Router
42 this.gridDataSource = new GridDataSource();
46 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
47 return this.pcrud.retrieveAll('asv', {});
50 this.grid.onRowActivate.subscribe(
51 (idlThing: IdlObject) => {
52 const idToEdit = idlThing.id();
53 this.navigateToEditPage(idToEdit);
58 showEditDialog(idlThing: IdlObject): Promise<any> {
62 editSelected = (surveys: IdlObject[]) => {
63 const idToEdit = surveys[0].id();
64 this.navigateToEditPage(idToEdit);
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(
73 this.toast.success(await this.endSurveySuccessString.current());
76 this.toast.warning(await this.endSurveyFailedString.current());
82 deleteSelected = (surveys: IdlObject[]) => {
83 for (let i = 0; i < surveys.length; i++) {
84 const idToDelete = surveys[i].id();
87 'open-ils.circ.survey.delete.cascade.override',
88 this.auth.token(), idToDelete
90 this.deleteSuccessString.current()
91 .then(str => this.toast.success(str));
95 this.deleteFailedString.current()
96 .then(str => this.toast.success(str));
101 navigateToEditPage(id: any) {
102 this.router.navigate(['/staff/admin/local/action/survey/' + id]);
106 this.editDialog.mode = 'create';
107 this.editDialog.datetimeFields = 'start_date,end_date';
108 this.editDialog.open({size: this.dialogSize}).subscribe(
110 this.createString.current()
111 .then(str => this.toast.success(str));
115 if (!rejection.dismissed) {
116 this.createErrString.current()
117 .then(str => this.toast.danger(str));