1 import {Component, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {switchMap, map, tap} from 'rxjs/operators';
4 import {AuthService} from '@eg/core/auth.service';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {EventService} from '@eg/core/event.service';
7 import {NetService} from '@eg/core/net.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {ToastService} from '@eg/share/toast/toast.service';
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
11 import {DialogComponent} from '@eg/share/dialog/dialog.component';
12 import {StringComponent} from '@eg/share/string/string.component';
16 * Dialog for changing an item's barcode
20 selector: 'eg-replace-barcode-dialog',
21 templateUrl: 'replace-barcode-dialog.component.html'
24 export class ReplaceBarcodeDialogComponent
25 extends DialogComponent {
27 @Input() copyIds: number[];
28 ids: number[]; // copy of list so we can pop()
32 barcodeExists: boolean;
37 @ViewChild('successMsg', { static: true })
38 private successMsg: StringComponent;
40 @ViewChild('errorMsg', { static: true })
41 private errorMsg: StringComponent;
44 private modal: NgbModal, // required for passing to parent
45 private toast: ToastService,
46 private auth: AuthService,
47 private net: NetService,
48 private evt: EventService,
49 private pcrud: PcrudService,
50 private renderer: Renderer2) {
51 super(modal); // required for subclassing
54 open(args: NgbModalOptions): Observable<boolean> {
55 this.ids = [].concat(this.copyIds);
56 this.numSucceeded = 0;
59 return this.getNextCopy()
60 .pipe(switchMap(() => super.open(args)),
62 this.renderer.selectRootElement('#new-barcode-input').focus())
66 getNextCopy(): Observable<any> {
68 if (this.auth.opChangeIsActive()) {
69 // FIXME: kludge for now, opChange has been reverting mid-dialog with batch use when handling permission elevation
70 this.auth.undoOpChange();
73 if (this.ids.length === 0) {
74 this.close(this.numSucceeded > 0);
79 const id = this.ids.pop();
81 return this.pcrud.retrieve('acp', id)
82 .pipe(map(c => this.copy = c));
86 this.barcodeExists = false;
88 // First see if the barcode is in use
89 return this.pcrud.search('acp', {deleted: 'f', barcode: this.newBarcode})
90 .toPromise().then(async (existing) => {
92 this.barcodeExists = true;
98 'open-ils.cat.update_copy_barcode',
99 this.auth.token(), this.copy.id(), this.newBarcode
102 if (this.evt.parse(res)) {
103 console.error('parsed error response', res);
105 console.log('success', res);
107 this.successMsg.current().then(m => this.toast.success(m));
108 this.getNextCopy().toPromise();
112 console.error('error', err);
114 console.error('Replace barcode failed: ', err);
115 this.errorMsg.current().then(m => this.toast.warning(m));
118 console.log('finis');