]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/booking/capture.component.ts
LP1851306: Port Capture Booking Resource to Angular
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / booking / capture.component.ts
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';
12
13 @Component({
14   templateUrl: './capture.component.html'
15 })
16
17 export class CaptureComponent implements OnInit, OnDestroy {
18
19     findResource: FormGroup;
20     subscriptions: Subscription[] = [];
21
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;
26
27     constructor(
28         private auth: AuthService,
29         private net: NetService,
30         private resourceValidator: BookingResourceBarcodeValidator,
31         private toast: ToastService,
32         private actions: ReservationActionsService
33     ) {
34     }
35
36     ngOnInit() {
37         this.findResource = new FormGroup({
38             'resourceBarcode': new FormControl(null, [], this.resourceValidator.validate)
39         });
40
41         const debouncing = 1500;
42         this.subscriptions.push(
43             this.resourceBarcode.valueChanges.pipe(
44                 debounceTime(debouncing),
45                 switchMap((val) => {
46                     if ('INVALID' === this.resourceBarcode.status) {
47                         this.noResourceString.current()
48                             .then(str => this.toast.danger(str));
49                         return of();
50                     } else {
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();
61                                 } else {
62                                     this.captureFailureString.current()
63                                         .then(str => this.toast.danger(str));
64                                 }
65                             } else {
66                                 this.captureFailureString.current()
67                                     .then(str => this.toast.danger(str));
68                             }
69                             return of();
70                         }));
71                     }
72                 })
73             )
74             .subscribe());
75
76     }
77
78     get resourceBarcode() {
79         return this.findResource.get('resourceBarcode');
80     }
81
82
83     ngOnDestroy(): void {
84         this.subscriptions.forEach((subscription) => {
85             subscription.unsubscribe();
86         });
87     }
88
89 }