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.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';
17 templateUrl: './return.component.html'
20 export class ReturnComponent implements OnInit, OnDestroy {
22 findPatron: FormGroup;
23 subscriptions: Subscription[] = [];
25 noSelectedRows: (rows: IdlObject[]) => boolean;
26 handleTabChange: ($event: NgbTabChangeEvent) => void;
27 @ViewChild('tabs', { static: true }) tabs: NgbTabset;
28 @ViewChildren(ReservationsGridComponent) grids: QueryList<ReservationsGridComponent>;
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
43 this.route.paramMap.pipe(switchMap((params: ParamMap) => {
44 return this.handleParams$(params);
47 this.findPatron = new FormGroup({
48 'patronBarcode': new FormControl(null,
49 [Validators.required],
51 'resourceBarcode': new FormControl(null,
52 [Validators.required])
55 const debouncing = 1500;
56 this.subscriptions.push(
57 this.patronBarcode.valueChanges.pipe(
58 debounceTime(debouncing),
60 if ('INVALID' === this.patronBarcode.status) {
61 this.toast.danger('No patron found with this barcode');
64 return this.patron.bcSearch(val).pipe(
66 tap((resp) => { this.router.navigate(['/staff', 'booking', 'return', 'by_patron', resp[0].id]); })
73 this.subscriptions.push(
74 this.resourceBarcode.valueChanges.pipe(
75 debounceTime(debouncing),
77 if ('INVALID' !== this.resourceBarcode.status) {
78 return this.pcrud.search('brsrc', {'barcode': val}, {
79 order_by: {'curr_rsrcs': 'pickup_time DESC'},
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();
96 this.noSelectedRows = (rows: IdlObject[]) => (rows.length === 0);
98 this.handleTabChange = ($event) => {
99 this.store.setItem('eg.booking.return.tab', $event.nextId)
101 this.router.navigate(['/staff', 'booking', 'return']);
102 this.findPatron.patchValue({resourceBarcode: ''});
103 this.patronId = null;
108 handleParams$ = (params: ParamMap): Observable<any> => {
109 this.patronId = +params.get('patron_id');
111 return this.pcrud.search('au', {
116 flesh_fields: {'au': ['card']}
119 this.findPatron.patchValue({patronBarcode: resp.card().barcode()});
121 }, (err) => { console.debug(err); }
124 return from(this.store.getItem('eg.booking.return.tab'))
126 if (tab) { this.tabs.select(tab); }
130 refreshGrids = (): void => {
131 this.grids.forEach (grid => grid.reloadGrid());
133 get patronBarcode() {
134 return this.findPatron.get('patronBarcode');
136 get resourceBarcode() {
137 return this.findPatron.get('resourceBarcode');
140 ngOnDestroy(): void {
141 this.subscriptions.forEach((subscription) => {
142 subscription.unsubscribe();