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