1 import {Component, OnInit, OnDestroy, ViewChild} from '@angular/core';
2 import {FormGroup, FormControl} from '@angular/forms';
3 import {of, Subscription} from 'rxjs';
4 import {debounceTime, single, switchMap, tap} from 'rxjs/operators';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {StringComponent} from '@eg/share/string/string.component';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {BookingResourceBarcodeValidator} from './booking_resource_validator.directive';
10 import {ReservationActionsService} from './reservation-actions.service';
11 import {ReservationsGridComponent} from './reservations-grid.component';
14 templateUrl: './capture.component.html'
17 export class CaptureComponent implements OnInit, OnDestroy {
19 findResource: FormGroup;
20 subscriptions: Subscription[] = [];
22 @ViewChild('capturedTodayGrid', { static: false }) capturedTodayGrid: ReservationsGridComponent;
23 @ViewChild('noResourceString', { static: true }) noResourceString: StringComponent;
24 @ViewChild('captureSuccessString', { static: true }) captureSuccessString: StringComponent;
25 @ViewChild('captureFailureString', { static: true }) captureFailureString: StringComponent;
28 private auth: AuthService,
29 private net: NetService,
30 private resourceValidator: BookingResourceBarcodeValidator,
31 private toast: ToastService,
32 private actions: ReservationActionsService
37 this.findResource = new FormGroup({
38 'resourceBarcode': new FormControl(null, [], this.resourceValidator.validate)
41 const debouncing = 1500;
42 this.subscriptions.push(
43 this.resourceBarcode.valueChanges.pipe(
44 debounceTime(debouncing),
46 if ('INVALID' === this.resourceBarcode.status) {
47 this.noResourceString.current()
48 .then(str => this.toast.danger(str));
51 return this.net.request( 'open-ils.booking',
52 'open-ils.booking.resources.capture_for_reservation',
53 this.auth.token(), this.resourceBarcode.value )
54 .pipe(switchMap((result: any) => {
55 if (result && result.ilsevent !== undefined) {
56 if (result.payload && result.payload.captured > 0) {
57 this.captureSuccessString.current()
58 .then(str => this.toast.success(str));
59 this.actions.printCaptureSlip(result.payload);
60 this.capturedTodayGrid.reloadGrid();
62 this.captureFailureString.current()
63 .then(str => this.toast.danger(str));
66 this.captureFailureString.current()
67 .then(str => this.toast.danger(str));
78 get resourceBarcode() {
79 return this.findResource.get('resourceBarcode');
84 this.subscriptions.forEach((subscription) => {
85 subscription.unsubscribe();