]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/server/coded-value-maps/coded-value-maps.component.ts
LP1843969 Composite Attribute Entry Defs
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / coded-value-maps / coded-value-maps.component.ts
1 import {Pager} from '@eg/share/util/pager';
2 import {Component, ViewChild, OnInit} from '@angular/core';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {GridDataSource} from '@eg/share/grid/grid';
5 import {GridComponent} from '@eg/share/grid/grid.component';
6 import {ToastService} from '@eg/share/toast/toast.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
11 @Component({
12     templateUrl: './coded-value-maps.component.html'
13 })
14
15 export class CodedValueMapsComponent implements OnInit {
16
17     gridDataSource: GridDataSource = new GridDataSource();
18     @ViewChild('createString', { static: true }) createString: StringComponent;
19     @ViewChild('createErrString', { static: true }) createErrString: StringComponent;
20     @ViewChild('updateSuccessString', { static: true }) updateSuccessString: StringComponent;
21     @ViewChild('updateFailedString', { static: true }) updateFailedString: StringComponent;
22     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
23     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
24
25     @ViewChild('grid', {static: true}) grid: GridComponent;
26     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
27
28     constructor(
29         private pcrud: PcrudService,
30         private toast: ToastService,
31     ) {
32     }
33
34     ngOnInit() {
35         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
36             return this.pcrud.retrieveAll('ccvm', {order_by: {ccvm: 'id'}}, {fleshSelectors: true});
37         };
38         this.grid.onRowActivate.subscribe(
39             (idlThing: IdlObject) => this.showEditDialog(idlThing)
40         );
41     }
42
43     showEditDialog(standingPenalty: IdlObject): Promise<any> {
44         this.editDialog.mode = 'update';
45         this.editDialog.recordId = standingPenalty['id']();
46         return new Promise((resolve, reject) => {
47             this.editDialog.open({size: 'lg'}).subscribe(
48                 result => {
49                     this.updateSuccessString.current()
50                         .then(str => this.toast.success(str));
51                     this.grid.reload();
52                     resolve(result);
53                 },
54                 error => {
55                     this.updateFailedString.current()
56                         .then(str => this.toast.danger(str));
57                     reject(error);
58                 }
59             );
60         });
61     }
62
63     editSelected = (maps: IdlObject[]) => {
64         const editOneThing = (map: IdlObject) => {
65             this.showEditDialog(map).then(
66                 () => editOneThing(maps.shift()));
67         };
68         editOneThing(maps.shift());
69     }
70
71     deleteSelected = (idlThings: IdlObject[]) => {
72         idlThings.forEach(idlThing => idlThing.isdeleted(true));
73         this.pcrud.autoApply(idlThings).subscribe(
74             val => {
75                 console.debug('deleted: ' + val);
76                 this.deleteSuccessString.current()
77                     .then(str => this.toast.success(str));
78             },
79             err => {
80                 this.deleteFailedString.current()
81                     .then(str => this.toast.danger(str));
82             },
83             ()  => this.grid.reload()
84         );
85     }
86
87     createNew = () => {
88         this.editDialog.mode = 'create';
89         this.editDialog.recordId = null;
90         this.editDialog.record = null;
91         this.editDialog.open({size: 'lg'}).subscribe(
92             ok => {
93                 this.createString.current()
94                     .then(str => this.toast.success(str));
95                 this.grid.reload();
96             },
97             rejection => {
98                 if (!rejection.dismissed) {
99                     this.createErrString.current()
100                         .then(str => this.toast.danger(str));
101                 }
102             }
103         );
104     }
105
106  }