]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/conjoined-items-dialog.component.ts
51000a77cd065cb8c69379560897a79ad0f55621
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / holdings / conjoined-items-dialog.component.ts
1 import {Component, OnInit, OnDestroy, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {IdlService} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {StoreService} from '@eg/core/store.service';
7 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
8 import {DialogComponent} from '@eg/share/dialog/dialog.component';
9 import {StringComponent} from '@eg/share/string/string.component';
10 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
11
12
13 /**
14  * Dialog for linking conjoined items.
15  */
16
17 @Component({
18   selector: 'eg-conjoined-items-dialog',
19   templateUrl: 'conjoined-items-dialog.component.html'
20 })
21
22 export class ConjoinedItemsDialogComponent
23     extends DialogComponent implements OnInit, OnDestroy {
24
25     @Input() copyIds: number[];
26     ids: number[]; // copy of list so we can pop()
27
28     peerType: number;
29     numSucceeded: number;
30     numFailed: number;
31     peerTypes: ComboboxEntry[];
32     peerRecord: number;
33
34     onOpenSub: Subscription;
35
36     @ViewChild('successMsg')
37         private successMsg: StringComponent;
38
39     @ViewChild('errorMsg')
40         private errorMsg: StringComponent;
41
42     constructor(
43         private modal: NgbModal, // required for passing to parent
44         private toast: ToastService,
45         private idl: IdlService,
46         private pcrud: PcrudService,
47         private localStore: StoreService) {
48         super(modal); // required for subclassing
49         this.peerTypes = [];
50     }
51
52     ngOnInit() {
53         this.onOpenSub = this.onOpen$.subscribe(() => {
54             this.ids = [].concat(this.copyIds);
55             this.numSucceeded = 0;
56             this.numFailed = 0;
57             this.peerRecord =
58                 this.localStore.getLocalItem('eg.cat.marked_conjoined_record');
59
60             if (!this.peerRecord) {
61                 this.close(false);
62             }
63
64             if (this.peerTypes.length === 0) {
65                 this.getPeerTypes();
66             }
67         });
68     }
69
70     ngOnDestroy() {
71         this.onOpenSub.unsubscribe();
72     }
73
74     getPeerTypes(): Promise<any> {
75         return this.pcrud.retrieveAll('bpt', {}, {atomic: true}).toPromise()
76         .then(types =>
77             // Map types to ComboboxEntry's
78             this.peerTypes = types.map(t => ({id: t.id(), label: t.name()}))
79         );
80     }
81
82     peerTypeChanged(entry: ComboboxEntry) {
83         if (entry) {
84             this.peerType = entry.id;
85         } else {
86             this.peerType = null;
87         }
88     }
89
90     linkCopies(): Promise<any> {
91
92         if (this.ids.length === 0) {
93             this.close(this.numSucceeded > 0);
94             return Promise.resolve();
95         }
96
97         const id = this.ids.pop();
98         const map = this.idl.create('bpbcm');
99         map.peer_record(this.peerRecord);
100         map.target_copy(id);
101         map.peer_type(this.peerType);
102
103         return this.pcrud.create(map).toPromise().then(
104             ok => {
105                 this.successMsg.current().then(msg => this.toast.success(msg));
106                 this.numSucceeded++;
107                 return this.linkCopies();
108             },
109             err => {
110                 this.numFailed++;
111                 console.error(err);
112                 this.errorMsg.current().then(msg => this.toast.warning(msg));
113                 return this.linkCopies();
114             }
115         );
116     }
117 }
118
119
120