LP1816475: Booking module refresh
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / booking / cancel-reservation-dialog.component.ts
1 import {Component, EventEmitter, Output, ViewChild} from '@angular/core';
2 import {switchMap} from 'rxjs/operators';
3 import {AuthService} from '@eg/core/auth.service';
4 import {NetService} from '@eg/core/net.service';
5 import {ToastService} from '@eg/share/toast/toast.service';
6 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
7
8 @Component({
9   selector: 'eg-cancel-reservation-dialog',
10   template: `
11   <eg-confirm-dialog #confirmCancelReservationDialog
12     i18n-dialogTitle i18n-dialogBody
13     dialogTitle="Confirm Cancelation"
14     [dialogBodyTemplate]="confirmMessage">
15   </eg-confirm-dialog>
16   <ng-template #confirmMessage>
17     <span i18n>
18       Are you sure you want to cancel
19       {reservations.length, plural, =1 {this reservation} other {these {{reservations.length}} reservations}}?
20     </span>
21   </ng-template>
22   `
23 })
24
25 export class CancelReservationDialogComponent {
26
27     constructor(
28         private auth: AuthService,
29         private net: NetService,
30         private toast: ToastService
31     ) {
32     }
33
34     reservations: number[];
35
36     @ViewChild('confirmCancelReservationDialog')
37         private cancelReservationDialog: ConfirmDialogComponent;
38
39     @Output() onSuccessfulCancel = new EventEmitter();
40
41     open(reservations: number[]) {
42         this.reservations = reservations;
43         this.cancelReservationDialog.open()
44             .pipe(
45                 switchMap(() => this.net.request(
46                     'open-ils.booking',
47                     'open-ils.booking.reservations.cancel',
48                     this.auth.token(), reservations))
49             )
50             .subscribe(
51                 (res) => {
52                     if (res.textcode) {
53                         this.toast.danger('Could not cancel reservation'); // TODO: needs i18n, pluralization
54                     } else {
55                         this.toast.success('Reservation successfully canceled'); // TODO: needs i18n, pluralization
56                         this.onSuccessfulCancel.emit();
57                     }
58                 }
59             );
60     }
61
62 }
63