]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/op-change/op-change.component.ts
95d4db87e7bc079432fcf99d3c0a2b251305a225
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / op-change / op-change.component.ts
1 import {Component, OnInit, Input, Renderer2} from '@angular/core';
2 import {ToastService} from '@eg/share/toast/toast.service';
3 import {AuthService} from '@eg/core/auth.service';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
6
7 @Component({
8   selector: 'eg-op-change',
9   templateUrl: 'op-change.component.html'
10 })
11
12 export class OpChangeComponent
13     extends DialogComponent implements OnInit {
14
15     @Input() username: string;
16     @Input() password: string;
17     @Input() loginType = 'temp';
18
19     @Input() successMessage: string;
20     @Input() failMessage: string;
21
22     constructor(
23         private modal: NgbModal, // required for passing to parent
24         private renderer: Renderer2,
25         private toast: ToastService,
26         private auth: AuthService) {
27         super(modal);
28     }
29
30     ngOnInit() {
31
32         // Focus the username any time the dialog is opened.
33         this.onOpen$.subscribe(
34             val => this.renderer.selectRootElement('#username').focus()
35         );
36     }
37
38     login(): Promise<any> {
39         if (!(this.username && this.password)) {
40             return Promise.reject('Missing Params');
41         }
42
43         return this.auth.login(
44             {   username    : this.username,
45                 password    : this.password,
46                 workstation : this.auth.workstation(),
47                 type        : this.loginType
48             },  true        // isOpChange
49         ).then(
50             ok => {
51                 this.password = '';
52                 this.username = '';
53
54                 // Fetch the user object
55                 this.auth.testAuthToken().then(
56                     ok2 => {
57                         this.close();
58                         this.toast.success(this.successMessage);
59                     }
60                 );
61             },
62             notOk => {
63                 this.password = '';
64                 this.toast.danger(this.failMessage);
65             }
66         );
67     }
68
69     restore(): Promise<any> {
70         return this.auth.undoOpChange().then(
71             ok => this.toast.success(this.successMessage),
72             err => this.toast.danger(this.failMessage)
73         );
74     }
75 }
76
77