LP#1977554: (follow-up) tag new strings for translations
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / login.component.ts
1 import {
2     Component,
3     ElementRef,
4     OnInit,
5     Renderer2,
6     ViewChild
7 } from '@angular/core';
8 import {Location} from '@angular/common';
9 import {Router, ActivatedRoute} from '@angular/router';
10 import {AuthService, AuthWsState} from '@eg/core/auth.service';
11 import {StoreService} from '@eg/core/store.service';
12 import {OrgService} from '@eg/core/org.service';
13 import {OfflineService} from '@eg/staff/share/offline.service';
14
15 @Component({
16   styleUrls: ['./login.component.css'],
17   templateUrl : './login.component.html'
18 })
19 export class StaffLoginComponent implements OnInit {
20
21     @ViewChild('password')
22     passwordInput: ElementRef;
23     workstations: any[];
24     loginFailed: boolean;
25     routeTo: string;
26     pendingXactsDate: Date;
27     passwordVisible: boolean;
28     ariaDescription: string = $localize`Your password is not visible.`;
29
30     args = {
31       username : '',
32       password : '',
33       workstation : '',
34       type : 'staff'
35     };
36
37     constructor(
38       private router: Router,
39       private route: ActivatedRoute,
40       private ngLocation: Location,
41       private renderer: Renderer2,
42       private auth: AuthService,
43       private org: OrgService,
44       private store: StoreService,
45       private offline: OfflineService
46     ) {}
47
48     ngOnInit() {
49         this.routeTo = this.route.snapshot.queryParamMap.get('routeTo');
50
51         if (this.routeTo) {
52             if (this.routeTo.match(/^[a-z]+:\/\//i)) {
53                 console.warn(
54                     'routeTo must contain only path information: ', this.routeTo);
55                 this.routeTo = null;
56             }
57         }
58
59         // clear out any stale auth data
60         this.auth.logout();
61
62         // Focus username
63         this.renderer.selectRootElement('#username').focus();
64
65         this.store.getWorkstations()
66         .then(wsList => {
67             this.workstations = wsList;
68             return this.store.getDefaultWorkstation();
69         }).then(def => {
70             this.args.workstation = def;
71             this.applyWorkstation();
72         });
73
74         this.offline.pendingXactsDate().then(d => this.pendingXactsDate = d);
75     }
76
77     applyWorkstation() {
78         const wanted = this.route.snapshot.queryParamMap.get('workstation');
79         if (!wanted) { return; } // use the default
80
81         const exists = this.workstations.filter(w => w.name === wanted)[0];
82         if (exists) {
83             this.args.workstation = wanted;
84         } else {
85             console.error(`Unknown workstation requested: ${wanted}`);
86         }
87     }
88
89     handleSubmit() {
90
91         this.passwordVisible = false;
92
93         // post-login URL
94         let url: string = this.routeTo || '/staff/splash';
95
96         // prevent sending the user back to the login page
97         if (url.match('/staff/login')) { url = '/staff/splash'; }
98
99         const workstation: string = this.args.workstation;
100
101         this.loginFailed = false;
102         this.auth.login(this.args).then(
103             ok => {
104
105                 if (this.auth.workstationState === AuthWsState.NOT_FOUND_SERVER) {
106                     // User attempted to login with a workstation that is
107                     // unknown to the server. Redirect to the WS admin page.
108                     // Reset the WS state to avoid looping back to WS removal
109                     // page before the new workstation can be activated.
110                     this.auth.workstationState = AuthWsState.PENDING;
111                     this.router.navigate(
112                         [`/staff/admin/workstation/workstations/remove/${workstation}`]);
113
114                 } else {
115
116                     this.offline.refreshOfflineData()
117                     // Initial login clears cached org unit settings.
118                     .then(_ => this.org.clearCachedSettings())
119                     .then(_ => {
120
121                         // Force reload of the app after a successful login.
122                         // This allows the route resolver to re-run with a
123                         // valid auth token and workstation.
124                         window.location.href =
125                             this.ngLocation.prepareExternalUrl(url);
126                     });
127                 }
128             },
129             notOk => {
130                 this.loginFailed = true;
131             }
132         );
133     }
134
135     togglePasswordVisibility() {
136         this.passwordVisible = !this.passwordVisible;
137         if(this.passwordVisible) this.ariaDescription = $localize`Your password is visible!`;
138         else this.ariaDescription = $localize`Your password is not visible.`;
139         this.passwordInput.nativeElement.focus();
140     }
141
142 }
143
144
145