]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/login.component.ts
LP1615805 No inputs after submit in patron search (AngularJS)
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / login.component.ts
1 import {Component, OnInit, Renderer2} from '@angular/core';
2 import {Location} from '@angular/common';
3 import {Router, ActivatedRoute} from '@angular/router';
4 import {AuthService, AuthWsState} from '@eg/core/auth.service';
5 import {StoreService} from '@eg/core/store.service';
6
7 @Component({
8   templateUrl : './login.component.html'
9 })
10
11 export class StaffLoginComponent implements OnInit {
12
13     workstations: any[];
14     loginFailed: boolean;
15
16     args = {
17       username : '',
18       password : '',
19       workstation : '',
20       type : 'staff'
21     };
22
23     constructor(
24       private router: Router,
25       private route: ActivatedRoute,
26       private ngLocation: Location,
27       private renderer: Renderer2,
28       private auth: AuthService,
29       private store: StoreService
30     ) {}
31
32     ngOnInit() {
33         // clear out any stale auth data
34         this.auth.logout();
35
36         // Focus username
37         this.renderer.selectRootElement('#username').focus();
38
39         this.workstations = this.store.getLocalItem('eg.workstation.all');
40         this.args.workstation =
41             this.store.getLocalItem('eg.workstation.default');
42         this.applyWorkstation();
43     }
44
45     applyWorkstation() {
46         const wanted = this.route.snapshot.queryParamMap.get('workstation');
47         if (!wanted) { return; } // use the default
48
49         const exists = this.workstations.filter(w => w.name === wanted)[0];
50         if (exists) {
51             this.args.workstation = wanted;
52         } else {
53             console.error(`Unknown workstation requested: ${wanted}`);
54         }
55     }
56
57     handleSubmit() {
58
59         // post-login URL
60         let url: string = this.auth.redirectUrl || '/staff/splash';
61
62         // prevent sending the user back to the login page
63         if (url.startsWith('/staff/login')) {
64             url = '/staff/splash';
65         }
66
67         const workstation: string = this.args.workstation;
68
69         this.loginFailed = false;
70         this.auth.login(this.args).then(
71             ok => {
72                 this.auth.redirectUrl = null;
73
74                 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
75                     // User attempted to login with a workstation that is
76                     // unknown to the server. Redirect to the WS admin page.
77                     // Reset the WS state to avoid looping back to WS removal
78                     // page before the new workstation can be activated.
79                     this.auth.workstationState = AuthWsState.PENDING;
80                     this.router.navigate(
81                         [`/staff/admin/workstation/workstations/remove/${workstation}`]);
82                 } else {
83                     // Force reload of the app after a successful login.
84                     // This allows the route resolver to re-run with a
85                     // valid auth token and workstation.
86                     window.location.href =
87                         this.ngLocation.prepareExternalUrl(url);
88                 }
89             },
90             notOk => {
91                 this.loginFailed = true;
92             }
93         );
94     }
95 }
96
97
98