]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/booking/return.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / booking / return.component.ts
1 import {Component, OnInit, OnDestroy, QueryList, ViewChildren, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {FormGroup, FormControl, Validators} from '@angular/forms';
4 import {NgbTabChangeEvent, NgbTabset} from '@ng-bootstrap/ng-bootstrap';
5 import {Observable, from, of, Subscription} from 'rxjs';
6 import { single, switchMap, tap, debounceTime } from 'rxjs/operators';
7 import {PatronService} from '@eg/staff/share/patron/patron.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {IdlObject} from '@eg/core/idl.service';
10 import {ReservationsGridComponent} from './reservations-grid.component';
11 import {ServerStoreService} from '@eg/core/server-store.service';
12 import {ToastService} from '@eg/share/toast/toast.service';
13 import {PatronBarcodeValidator} from '@eg/share/validators/patron_barcode_validator.directive';
14
15
16 @Component({
17   templateUrl: './return.component.html'
18 })
19
20 export class ReturnComponent implements OnInit, OnDestroy {
21     patronId: number;
22     findPatron: FormGroup;
23     subscriptions: Subscription[] = [];
24
25     noSelectedRows: (rows: IdlObject[]) => boolean;
26     handleTabChange: ($event: NgbTabChangeEvent) => void;
27     @ViewChild('tabs', { static: true }) tabs: NgbTabset;
28     @ViewChildren(ReservationsGridComponent) grids: QueryList<ReservationsGridComponent>;
29
30     constructor(
31         private pcrud: PcrudService,
32         private patron: PatronService,
33         private pbv: PatronBarcodeValidator,
34         private route: ActivatedRoute,
35         private router: Router,
36         private store: ServerStoreService,
37         private toast: ToastService
38     ) {
39     }
40
41
42     ngOnInit() {
43         this.route.paramMap.pipe(switchMap((params: ParamMap) => {
44             return this.handleParams$(params);
45         })).subscribe();
46
47         this.findPatron = new FormGroup({
48             'patronBarcode': new FormControl(null,
49                 [Validators.required],
50                 [this.pbv.validate]),
51             'resourceBarcode': new FormControl(null,
52                 [Validators.required])
53         });
54
55         const debouncing = 1500;
56         this.subscriptions.push(
57             this.patronBarcode.valueChanges.pipe(
58                 debounceTime(debouncing),
59                 switchMap((val) => {
60                     if ('INVALID' === this.patronBarcode.status) {
61                         this.toast.danger('No patron found with this barcode');
62                         return of();
63                     } else {
64                         return this.patron.bcSearch(val).pipe(
65                             single(),
66                             tap((resp) => { this.router.navigate(['/staff', 'booking', 'return', 'by_patron', resp[0].id]); })
67                         );
68                     }
69                 })
70             )
71             .subscribe());
72
73         this.subscriptions.push(
74             this.resourceBarcode.valueChanges.pipe(
75                 debounceTime(debouncing),
76                 switchMap((val) => {
77                     if ('INVALID' !== this.resourceBarcode.status) {
78                         return this.pcrud.search('brsrc', {'barcode': val}, {
79                             order_by: {'curr_rsrcs': 'pickup_time DESC'},
80                             limit: 1,
81                             flesh: 1,
82                             flesh_fields: {'brsrc': ['curr_rsrcs']},
83                             select: {'curr_rsrcs': {'return_time': null, 'pickup_time': {'!=': null}}}
84                         }).pipe(tap((resp) => {
85                             if (resp.curr_rsrcs()[0].usr()) {
86                                 this.patronId = resp.curr_rsrcs()[0].usr();
87                                 this.refreshGrids();
88                             }
89                         }));
90                     } else {
91                         return of();
92                     }
93                 })
94             ).subscribe()
95         );
96         this.noSelectedRows = (rows: IdlObject[]) => (rows.length === 0);
97
98         this.handleTabChange = ($event) => {
99             this.store.setItem('eg.booking.return.tab', $event.nextId)
100             .then(() => {
101                 this.router.navigate(['/staff', 'booking', 'return']);
102                 this.findPatron.patchValue({resourceBarcode: ''});
103                 this.patronId = null;
104             });
105         };
106     }
107
108     handleParams$ = (params: ParamMap): Observable<any> => {
109       this.patronId = +params.get('patron_id');
110       if (this.patronId) {
111           return this.pcrud.search('au', {
112               'id': this.patronId,
113           }, {
114               limit: 1,
115               flesh: 1,
116               flesh_fields: {'au': ['card']}
117           }).pipe(tap(
118               (resp) => {
119                   this.findPatron.patchValue({patronBarcode: resp.card().barcode()});
120                   this.refreshGrids();
121               }, (err) => { console.debug(err); }
122           ));
123       } else {
124           return from(this.store.getItem('eg.booking.return.tab'))
125               .pipe(tap(tab => {
126                   if (tab) { this.tabs.select(tab); }
127           }));
128       }
129     }
130     refreshGrids = (): void => {
131         this.grids.forEach (grid => grid.reloadGrid());
132     }
133     get patronBarcode() {
134       return this.findPatron.get('patronBarcode');
135     }
136     get resourceBarcode() {
137       return this.findPatron.get('resourceBarcode');
138     }
139
140     ngOnDestroy(): void {
141       this.subscriptions.forEach((subscription) => {
142           subscription.unsubscribe();
143       });
144     }
145 }