LP1806087 Angular catalog Ang7 & lint repairs
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / basket-actions.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {BasketService} from '@eg/share/catalog/basket.service';
3 import {Subscription} from 'rxjs';
4 import {Router} from '@angular/router';
5 import {NetService} from '@eg/core/net.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {PrintService} from '@eg/share/print/print.service';
8 import {RecordBucketDialogComponent
9     } from '@eg/staff/share/buckets/record-bucket-dialog.component';
10
11 @Component({
12   selector: 'eg-catalog-basket-actions',
13   templateUrl: 'basket-actions.component.html'
14 })
15 export class BasketActionsComponent implements OnInit {
16
17     basketAction: string;
18
19     @ViewChild('addBasketToBucketDialog')
20         addToBucketDialog: RecordBucketDialogComponent;
21
22     constructor(
23         private router: Router,
24         private net: NetService,
25         private auth: AuthService,
26         private printer: PrintService,
27         private basket: BasketService
28     ) {
29         this.basketAction = '';
30     }
31
32     ngOnInit() {
33     }
34
35     basketCount(): number {
36         return this.basket.recordCount();
37     }
38
39     // TODO: confirmation dialogs?
40
41     applyAction() {
42         console.debug('Performing basket action', this.basketAction);
43
44         switch (this.basketAction) {
45             case 'view':
46                 // This does not propagate search params -- unclear if needed.
47                 this.router.navigate(['/staff/catalog/search'],
48                     {queryParams: {showBasket: true}});
49                 break;
50
51             case 'clear':
52                 this.basket.removeAllRecordIds();
53                 break;
54
55             case 'hold':
56                 this.basket.getRecordIds().then(ids => {
57                     this.router.navigate(['/staff/catalog/hold/T'],
58                         {queryParams: {target: ids}});
59                 });
60                 break;
61
62             case 'print':
63                 this.basket.getRecordIds().then(ids => {
64                     this.net.request(
65                         'open-ils.search',
66                         'open-ils.search.biblio.record.print', ids
67                     ).subscribe(
68                         at_event => {
69                             // check for event..
70                             const html = at_event.template_output().data();
71                             this.printer.print({
72                                 text: html,
73                                 printContext: 'default'
74                             });
75                         }
76                     );
77                 });
78                 break;
79
80             case 'email':
81                 this.basket.getRecordIds().then(ids => {
82                     this.net.request(
83                         'open-ils.search',
84                         'open-ils.search.biblio.record.email',
85                         this.auth.token(), ids
86                     ).toPromise(); // fire-and-forget
87                 });
88                 break;
89
90             case 'bucket':
91                 this.basket.getRecordIds().then(ids => {
92                     this.addToBucketDialog.recordId = ids;
93                     this.addToBucketDialog.open({size: 'lg'});
94                 });
95                 break;
96
97         }
98
99         // Resetting basketAction inside its onchange handler
100         // prevents the new value from propagating to Angular
101         // Reset after the current thread.
102         setTimeout(() => this.basketAction = ''); // reset
103     }
104 }
105
106