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