lp1847519 Port of Circulation Limit Set UI
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / circ_limit_set / circ_limit_set.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
12 @Component({
13     templateUrl: './circ_limit_set.component.html'
14 })
15
16 export class CircLimitSetComponent implements OnInit {
17
18     recId: number;
19     gridDataSource: GridDataSource;
20     initDone = false;
21     cspSource: GridDataSource = new GridDataSource();
22
23     @ViewChild('editDialog', {static: true}) editDialog: FmRecordEditorComponent;
24     @ViewChild('grid', {static: true}) grid: GridComponent;
25     @ViewChild('updateSuccessString', {static: true}) updateSuccessString: StringComponent;
26     @ViewChild('updateFailedString', {static: true}) updateFailedString: StringComponent;
27     @ViewChild('deleteFailedString', {static: true}) deleteFailedString: StringComponent;
28     @ViewChild('deleteSuccessString', {static: true}) deleteSuccessString: StringComponent;
29     @ViewChild('createSuccessString', {static: true}) createSuccessString: StringComponent;
30     @ViewChild('createErrString', {static: true}) createErrString: StringComponent;
31
32     @Input() dialogSize: 'sm' | 'lg' = 'lg';
33
34     constructor(
35         private pcrud: PcrudService,
36         private toast: ToastService,
37         private router: Router
38     ) {
39         this.gridDataSource = new GridDataSource();
40     }
41
42     ngOnInit() {
43         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
44             const orderBy: any = {};
45             const searchOps = {
46                 offset: pager.offset,
47                 limit: pager.limit,
48                 order_by: orderBy
49             };
50             return this.pcrud.retrieveAll('ccls', searchOps, {fleshSelectors: true});
51         };
52
53         this.grid.onRowActivate.subscribe(
54             (set: IdlObject) => {
55                 const idToEdit = set.id();
56                 this.navigateToEditPage(idToEdit);
57             }
58         );
59     }
60
61     deleteSelected = (idlThings: IdlObject[]) => {
62         idlThings.forEach(idlThing => idlThing.isdeleted(true));
63         this.pcrud.autoApply(idlThings).subscribe(
64             val => {
65                 this.deleteSuccessString.current()
66                     .then(str => this.toast.success(str));
67             },
68             err => {
69                 this.deleteFailedString.current()
70                     .then(str => this.toast.danger(str));
71             },
72             ()  => this.grid.reload()
73         );
74     }
75
76     editSelected(sets: IdlObject[]) {
77         const idToEdit = sets[0].id();
78         this.navigateToEditPage(idToEdit);
79     }
80
81     navigateToEditPage(id: any) {
82         this.router.navigate(['/staff/admin/local/config/circ_limit_set/' + id]);
83     }
84
85     createNew() {
86         this.editDialog.mode = 'create';
87         this.editDialog.recordId = null;
88         this.editDialog.record = null;
89         this.editDialog.open({size: this.dialogSize}).subscribe(
90             ok => {
91                 this.createSuccessString.current()
92                     .then(str => this.toast.success(str));
93                 this.grid.reload();
94             },
95             rejection => {
96                 if (!rejection.dismissed) {
97                     this.createErrString.current()
98                         .then(str => this.toast.danger(str));
99                 }
100             }
101         );
102     }
103
104     showEditDialog(standingPenalty: IdlObject): Promise<any> {
105         this.editDialog.mode = 'update';
106         this.editDialog.recordId = standingPenalty['id']();
107         return new Promise((resolve, reject) => {
108             this.editDialog.open({size: this.dialogSize}).subscribe(
109                 result => {
110                     this.updateSuccessString.current()
111                         .then(str => this.toast.success(str));
112                     this.grid.reload();
113                     resolve(result);
114                 },
115                 error => {
116                     this.updateFailedString.current()
117                         .then(str => this.toast.danger(str));
118                     reject(error);
119                 }
120             );
121         });
122     }
123 }