]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/reporter/simple/sr-my-reports.component.ts
LP2042879 Shelving Location Groups Admin accessibility
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / reporter / simple / sr-my-reports.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {map, concatMap} from 'rxjs/operators';
4 import {from} from 'rxjs';
5 import {AuthService} from '@eg/core/auth.service';
6 import {IdlObject, IdlService} from '@eg/core/idl.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {Pager} from '@eg/share/util/pager';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {GridDataSource, GridCellTextGenerator} from '@eg/share/grid/grid';
11 import {SimpleReporterService, SRTemplate} from './simple-reporter.service';
12 import {StringComponent} from '@eg/share/string/string.component';
13 import {ToastService} from '@eg/share/toast/toast.service';
14 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
15 import {PromptDialogComponent} from '@eg/share/dialog/prompt.component';
16 import {NetService} from '@eg/core/net.service';
17
18 @Component({
19     selector: 'eg-sr-reports',
20     templateUrl: 'sr-my-reports.component.html',
21 })
22
23 export class SRReportsComponent implements OnInit {
24
25     gridSource: GridDataSource;
26     editSelected: ($event: any) => void;
27     newReport: ($event: any) => void;
28     @ViewChild('srReportsGrid', { static: true }) reportsGrid: GridComponent;
29     @ViewChild('confirmDelete', { static: true }) deleteDialog: ConfirmDialogComponent;
30     @ViewChild('promptClone', { static: true }) cloneDialog: PromptDialogComponent;
31     @ViewChild('delete', { static: true} ) deleteString: StringComponent;
32     @ViewChild('clone', { static: true} ) cloneString: StringComponent;
33     @ViewChild('deleteSuccess', { static: true} ) deleteSuccessString: StringComponent;
34     @ViewChild('deleteFailure', { static: true} ) deleteFailureString: StringComponent;
35     @ViewChild('mixedResults', { static: true} ) mixedResultsString: StringComponent;
36     @ViewChild('templateSaved', { static: true }) templateSavedString: StringComponent;
37     @ViewChild('templateSaveError', { static: true }) templateSaveErrorString: StringComponent;
38
39
40     cellTextGenerator: GridCellTextGenerator;
41
42     constructor(
43         private router: Router,
44         private route: ActivatedRoute,
45         private auth: AuthService,
46         private pcrud: PcrudService,
47         private idl: IdlService,
48         private srSvc: SimpleReporterService,
49         private toast: ToastService,
50         private net: NetService
51     ) {
52     }
53
54     ngOnInit() {
55         this.gridSource = this.srSvc.getReportsDatasource();
56
57         this.editSelected = ($event) => {
58                 this.router.navigate(['edit', $event[0].rt_id], { relativeTo: this.route });
59         };
60
61         this.newReport = ($event) => {
62                 this.router.navigate(['new'], { relativeTo: this.route });
63         };
64     }
65
66     zeroSelectedRows(rows: any) {
67         return rows.length === 0;
68     }
69
70     notOneSelectedRow(rows: any) {
71         return rows.length !== 1;
72     }
73
74     deleteSelected(rows: any) {
75         if ( rows.length <= 0 ) { return; }
76
77         let successes = 0;
78         let failures = 0;
79
80         this.deleteString.current({ct: rows.length})
81         .then(str => {
82             this.deleteDialog.dialogBody = str;
83             this.deleteDialog.open()
84             .subscribe(confirmed => {
85                 if ( confirmed ) {
86                     from(rows.map(x => x.rt_id)).pipe(concatMap(rt_id =>
87                         this.net.request(
88                             'open-ils.reporter',
89                             'open-ils.reporter.template.delete.cascade',
90                             this.auth.token(),
91                             rt_id
92                         ).pipe(map(res => ({
93                             result: res,
94                             rt_id: rt_id
95                         })))
96                     )).subscribe(
97                         (res) => {
98                             if (Number(res.result) === 2) {
99                                 successes++;
100                             } else {
101                                 failures++;
102                             }
103                         },
104                         (err) => {},
105                         () => {
106                             if (successes === rows.length) {
107                                 this.deleteSuccessString.current({ct: successes}).then(str2 => { this.toast.success(str2); });
108                             } else if (failures && !successes) {
109                                 this.deleteFailureString.current({ct: failures}).then(str2 => { this.toast.danger(str2); });
110                             } else {
111                                 this.mixedResultsString.current({fail: failures, success: successes})
112                                     .then(str2 => { this.toast.warning(str2); });
113                             }
114                             this.reportsGrid.reload();
115                          }
116                     );
117                 }
118             });
119         });
120     }
121
122     cloneSelected(row: any) {
123         if ( row.length <= 0 ) { return; }
124         if ( row.length > 1 ) { return; }
125
126         const rt_row = row[0];
127
128         this.cloneString.current({old: rt_row.name})
129         .then(str => {
130             this.cloneDialog.dialogBody = str;
131             this.cloneDialog.promptValue = rt_row.name + ' (Clone)';
132             this.cloneDialog.open()
133             .subscribe(new_name => {
134                 if ( new_name ) {
135                     this.srSvc.loadTemplate(rt_row.rt_id)
136                     .then(idl => {
137                         // build a new clone
138                         const new_templ = new SRTemplate(idl);
139                         new_templ.name = new_name;
140                         new_templ.id = -1;
141                         new_templ.isNew = true;
142                         new_templ.create_time = null;
143                         new_templ.runNow = 'now';
144                         new_templ.runTime = null;
145
146                         // and save it
147                         this.srSvc.saveTemplate(new_templ, false)
148                         .then(rt => {
149                             this.router.navigate(['edit', rt.id()], { relativeTo: this.route });
150                         },
151                         err => {
152                             this.templateSaveErrorString.current()
153                             .then(errstr => {
154                                 this.toast.danger(errstr + err);
155                                 console.error('Error saving template: %o', err);
156                             });
157                         });
158
159                     });
160                 }
161             });
162         });
163     }
164
165 }