]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/holdings/conjoined-items-dialog.component.ts
LP1830973 Angular 8 updates
[working/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, IdlObject} 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
27     // If true, ignore the provided copyIds array and fetch all of
28     // the linked copies to work on.
29     @Input() modifyAll: boolean;
30
31     // If peerRecord is not set, the localStorage value will be used.
32     @Input() peerRecord: number;
33
34     peerType: number;
35     numSucceeded: number;
36     numFailed: number;
37     peerTypes: ComboboxEntry[];
38     existingMaps: any;
39     onOpenSub: Subscription;
40
41     @ViewChild('successMsg', { static: true }) private successMsg: StringComponent;
42     @ViewChild('errorMsg', { static: true }) private errorMsg: StringComponent;
43
44     constructor(
45         private modal: NgbModal, // required for passing to parent
46         private toast: ToastService,
47         private idl: IdlService,
48         private pcrud: PcrudService,
49         private localStore: StoreService) {
50         super(modal); // required for subclassing
51         this.peerTypes = [];
52         this.copyIds = [];
53     }
54
55     ngOnInit() {
56         this.onOpenSub = this.onOpen$.subscribe(() => {
57             if (this.modifyAll) {
58                 // This will be set once the list of copies to
59                 // modify has been fetched.
60                 this.copyIds = [];
61             }
62             this.numSucceeded = 0;
63             this.numFailed = 0;
64
65             if (!this.peerRecord) {
66                 this.peerRecord =
67                     this.localStore.getLocalItem('eg.cat.marked_conjoined_record');
68
69                     if (!this.peerRecord) {
70                     this.close(false);
71                 }
72             }
73
74             if (this.peerTypes.length === 0) {
75                 this.getPeerTypes();
76             }
77
78             this.fetchExistingMaps();
79         });
80     }
81
82     ngOnDestroy() {
83         this.onOpenSub.unsubscribe();
84     }
85
86     fetchExistingMaps() {
87         this.existingMaps = {};
88         const search: any = {
89             peer_record: this.peerRecord
90         };
91
92         if (!this.modifyAll) {
93             search.target_copy = this.copyIds;
94         }
95
96         this.pcrud.search('bpbcm', search)
97         .subscribe(map => {
98             this.existingMaps[map.target_copy()] = map;
99             if (this.modifyAll) {
100                 this.copyIds.push(map.target_copy());
101             }
102         });
103     }
104
105     // Fetch and map peer types to combobox entries
106     getPeerTypes(): Promise<any> {
107         return this.pcrud.retrieveAll('bpt', {}, {atomic: true}).toPromise()
108         .then(types =>
109             this.peerTypes = types.map(t => ({id: t.id(), label: t.name()}))
110         );
111     }
112
113     peerTypeChanged(entry: ComboboxEntry) {
114         if (entry) {
115             this.peerType = entry.id;
116         } else {
117             this.peerType = null;
118         }
119     }
120
121     // Create or update peer copy links.
122     linkCopies() {
123
124         const maps: IdlObject[] = [];
125         this.copyIds.forEach(id => {
126             let map: IdlObject;
127             if (this.existingMaps[id]) {
128                 map = this.existingMaps[id];
129                 map.ischanged(true);
130             } else {
131                 map = this.idl.create('bpbcm');
132                 map.isnew(true);
133             }
134
135             map.peer_record(this.peerRecord);
136             map.target_copy(id);
137             map.peer_type(this.peerType);
138             maps.push(map);
139         });
140
141         return this.pcrud.autoApply(maps).subscribe(
142             ok => this.numSucceeded++,
143             err => {
144                 this.numFailed++;
145                 console.error(err);
146                 this.errorMsg.current().then(msg => this.toast.warning(msg));
147             },
148             () => {
149                 this.successMsg.current().then(msg => this.toast.success(msg));
150                 this.close(this.numSucceeded > 0);
151             }
152         );
153     }
154 }
155
156
157