]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/record/conjoined.component.ts
1ba570d407187734f8add47e462eda124e78e964
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / record / conjoined.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {IdlService, IdlObject} from '@eg/core/idl.service';
3 import {PcrudService} from '@eg/core/pcrud.service';
4 import {Pager} from '@eg/share/util/pager';
5 import {OrgService} from '@eg/core/org.service';
6 import {PermService} from '@eg/core/perm.service';
7 import {GridDataSource} from '@eg/share/grid/grid';
8 import {GridComponent} from '@eg/share/grid/grid.component';
9 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
10 import {ConjoinedItemsDialogComponent
11     } from '@eg/staff/share/holdings/conjoined-items-dialog.component';
12
13 /** Conjoined items per record grid */
14
15 @Component({
16   selector: 'eg-catalog-record-conjoined',
17   templateUrl: 'conjoined.component.html'
18 })
19 export class ConjoinedComponent implements OnInit {
20
21     @Input() recordId: number;
22
23     hasPerm: boolean;
24     gridDataSource: GridDataSource;
25     idsToUnlink: number[];
26
27     @ViewChild('conjoinedGrid') private grid: GridComponent;
28
29     @ViewChild('conjoinedDialog')
30         private conjoinedDialog: ConjoinedItemsDialogComponent;
31
32     @ViewChild('confirmUnlink')
33         private confirmUnlink: ConfirmDialogComponent;
34
35     constructor(
36         private idl: IdlService,
37         private org: OrgService,
38         private pcrud: PcrudService,
39         private perm: PermService
40     ) {
41         this.gridDataSource = new GridDataSource();
42         this.idsToUnlink = [];
43     }
44
45     ngOnInit() {
46         // Load edit perms
47         this.perm.hasWorkPermHere(['UPDATE_COPY'])
48             .then(perms => this.hasPerm = perms.UPDATE_COPY);
49
50         this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
51             const orderBy: any = {};
52
53             if (sort.length) { // Sort provided by grid.
54                 orderBy.bmp = sort[0].name + ' ' + sort[0].dir;
55             } else {
56                 orderBy.bmp = 'id';
57             }
58
59             const searchOps = {
60                 offset: pager.offset,
61                 limit: pager.limit,
62                 order_by: orderBy
63             };
64
65             return this.pcrud.search('bpbcm',
66                 {peer_record: this.recordId}, searchOps, {fleshSelectors: true});
67         };
68     }
69
70     async unlink(rows: any) {
71
72         this.idsToUnlink = rows.map(r => r.target_copy().id());
73         if (this.idsToUnlink.length === 0) { return; }
74
75         try { // rejects on dismiss, which results in an Error
76             // TODO this will change with LP #1823041
77             await this.confirmUnlink.open({size: 'sm'});
78         } catch (dismissed) { return; }
79
80         const maps = [];
81         this.pcrud.search('bpbcm',
82             {target_copy: this.idsToUnlink, peer_record: this.recordId})
83         .subscribe(
84             map => maps.push(map),
85             err => {},
86             () => {
87                 this.pcrud.remove(maps).subscribe(
88                     ok => console.debug('deleted map ', ok),
89                     err => console.error(err),
90                     ()  => {
91                         this.idsToUnlink = [];
92                         this.grid.reload();
93                     }
94                 );
95             }
96         );
97     }
98
99     openConjoinedDialog() {
100         this.conjoinedDialog.open({size: 'sm'}).subscribe(
101             modified => {
102                 if (modified) {
103                     this.grid.reload();
104                 }
105             }
106         );
107     }
108 }
109