1 import {Component, OnInit, Input, ViewChild, Renderer2} from '@angular/core';
2 import {Observable, throwError} from 'rxjs';
3 import {switchMap, map, tap} from 'rxjs/operators';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {EventService} from '@eg/core/event.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {AuthService} from '@eg/core/auth.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 marking items missing.
20 selector: 'eg-replace-barcode-dialog',
21 templateUrl: 'replace-barcode-dialog.component.html'
24 export class ReplaceBarcodeDialogComponent
25 extends DialogComponent implements OnInit {
27 @Input() copyIds: number[];
28 ids: number[]; // copy of list so we can pop()
32 barcodeExists: boolean;
37 @ViewChild('successMsg')
38 private successMsg: StringComponent;
40 @ViewChild('errorMsg')
41 private errorMsg: StringComponent;
44 private modal: NgbModal, // required for passing to parent
45 private toast: ToastService,
46 private net: NetService,
47 private pcrud: PcrudService,
48 private evt: EventService,
49 private renderer: Renderer2,
50 private auth: AuthService) {
51 super(modal); // required for subclassing
56 open(args: NgbModalOptions): Observable<boolean> {
57 this.ids = [].concat(this.copyIds);
58 this.numSucceeded = 0;
61 return this.getNextCopy()
62 .pipe(switchMap(() => super.open(args)))
64 this.renderer.selectRootElement('#new-barcode-input').focus())
68 getNextCopy(): Observable<any> {
70 if (this.ids.length === 0) {
71 this.close(this.numSucceeded > 0);
72 return throwError(false);
77 const id = this.ids.pop();
79 return this.pcrud.retrieve('acp', id)
80 .pipe(map(c => this.copy = c));
83 async replaceOneBarcode(): Promise<any> {
84 this.barcodeExists = false;
86 // First see if the barcode is in use
87 return this.pcrud.search('acp', {deleted: 'f', barcode: this.newBarcode})
88 .toPromise().then(async (existing) => {
90 this.barcodeExists = true;
94 this.copy.barcode(this.newBarcode);
95 this.pcrud.update(this.copy).toPromise().then(
98 this.toast.success(await this.successMsg.current());
103 console.error('Replace barcode failed: ', err);
104 this.toast.warning(await this.errorMsg.current());