LP#1626157 op-change, toast; strings cont.
authorBill Erickson <berickxx@gmail.com>
Wed, 25 Apr 2018 20:50:11 +0000 (16:50 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 25 Apr 2018 20:50:11 +0000 (16:50 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/core/auth.service.ts
Open-ILS/src/eg2/src/app/share/string/string.component.ts
Open-ILS/src/eg2/src/app/share/string/string.service.ts
Open-ILS/src/eg2/src/app/staff/nav.component.html
Open-ILS/src/eg2/src/app/staff/nav.component.ts
Open-ILS/src/eg2/src/app/staff/share/op-change/op-change.component.ts

index 9bd05dc..84de51a 100644 (file)
@@ -42,12 +42,7 @@ export class EgAuthService {
 
     private authChannel: any;
 
-    private activeUser: EgAuthUser;
-
-    // opChangeUser refers to the user that has been superseded during
-    // an op-change event.  opChangeUser resumes its status as the 
-    // activeUser once the op-change cycle has completed.
-    private opChangeUser: EgAuthUser = null;
+    private activeUser: EgAuthUser = null;
 
     workstationState: EgAuthWsState = EgAuthWsState.PENDING;
 
@@ -72,7 +67,7 @@ export class EgAuthService {
 
     // Returns true if we are currently in op-change mode.
     opChangeIsActive(): boolean {
-        return this.opChangeUser !== null;
+        return Boolean(this.store.getLoginSessionItem('eg.auth.time.oc'));
     }
 
     // - Accessor functions always refer to the active user.
@@ -95,12 +90,17 @@ export class EgAuthService {
     };
 
     // NOTE: EgNetService emits an event if the auth session has expired.
+    // This only rejects when no authtoken is found.
     testAuthToken(): Promise<any> {
 
-        this.activeUser = new EgAuthUser(
-            this.store.getLoginSessionItem('eg.auth.token'),
-            this.store.getLoginSessionItem('eg.auth.time')
-        );
+        if (!this.activeUser) {
+            // Only necessary on new page loads.  During op-change, 
+            // for example, we already have an activeUser.
+            this.activeUser = new EgAuthUser(
+                this.store.getLoginSessionItem('eg.auth.token'),
+                this.store.getLoginSessionItem('eg.auth.time')
+            );
+        }
 
         if (!this.token()) return Promise.reject('no authtoken');
 
@@ -129,8 +129,7 @@ export class EgAuthService {
 
         switch (evt.textcode) {
             case 'SUCCESS':
-                this.handleLoginOk(args, evt, isOpChange);
-                return Promise.resolve();
+                return this.handleLoginOk(args, evt, isOpChange);
 
             case 'WORKSTATION_NOT_FOUND':
                 console.error(`No such workstation "${args.workstation}"`);
@@ -145,12 +144,11 @@ export class EgAuthService {
     }
 
     // Stash the login data
-    handleLoginOk(args: EgAuthLoginArgs, evt: EgEvent, isOpChange: boolean): void {
+    handleLoginOk(args: EgAuthLoginArgs, evt: EgEvent, isOpChange: boolean): Promise<void> {
 
         if (isOpChange) {
             this.store.setLoginSessionItem('eg.auth.token.oc', this.token());
             this.store.setLoginSessionItem('eg.auth.time.oc', this.authtime());
-            this.opChangeUser = this.activeUser;
         }
 
         this.activeUser = new EgAuthUser(
@@ -161,18 +159,24 @@ export class EgAuthService {
 
         this.store.setLoginSessionItem('eg.auth.token', this.token());
         this.store.setLoginSessionItem('eg.auth.time', this.authtime());
+
+        return Promise.resolve();
     }
 
     undoOpChange(): Promise<any> {
-        if (this.opChangeUser) {
+        if (this.opChangeIsActive()) {
             this.deleteSession();
-            this.activeUser = this.opChangeUser;
-            this.opChangeUser = null;
+            this.activeUser = new EgAuthUser(
+                this.store.getLoginSessionItem('eg.auth.token.oc'),
+                this.store.getLoginSessionItem('eg.auth.time.oc'),
+                this.activeUser.workstation
+            );
             this.store.removeLoginSessionItem('eg.auth.token.oc');                
             this.store.removeLoginSessionItem('eg.auth.time.oc');                 
             this.store.setLoginSessionItem('eg.auth.token', this.token());
             this.store.setLoginSessionItem('eg.auth.time', this.authtime());
         }
+        // Re-fetch the user.
         return this.testAuthToken();
     }
 
@@ -281,7 +285,6 @@ export class EgAuthService {
     // Remove/reset session data
     cleanup(): void {
         this.activeUser = null;
-        this.opChangeUser = null;
         if (this.pollTimeout) {
             clearTimeout(this.pollTimeout);
             this.pollTimeout = null;
index 13e442c..8c81898 100644 (file)
@@ -31,10 +31,14 @@ export class EgStringComponent implements OnInit {
     }
 
     ngOnInit() {
-        this.strings.register({
-            key: this.key, 
-            resolver: (ctx:any) => this.current(ctx)
-        });
+        // No key means it's an unregistered (likely static) string
+        // that does not need interpolation.
+        if (this.key) {
+            this.strings.register({
+                key: this.key, 
+                resolver: (ctx:any) => this.current(ctx)
+            });
+        }
     }
 
 
index 0e3e706..1af8083 100644 (file)
@@ -16,7 +16,7 @@ export class EgStringService {
         this.strings[assn.key] = assn;
     }
 
-    interpolate(key: string, ctx: any): Promise<string> {
+    interpolate(key: string, ctx?: any): Promise<string> {
         if (!this.strings[key]) 
             return Promise.reject('No Such String');
         return this.strings[key].resolver(ctx);
index a56fd16..c5d5fcb 100644 (file)
 
 
     <div class="navbar-nav mr-auto"></div>
-    <div class="navbar-nav" *ngIf="user">
-      <span i18n>{{user}} @ {{workstation}}</span>
+    <div class="navbar-nav" *ngIf="user()">
+      <span i18n>{{user()}} @ {{workstation()}}</span>
     </div>
-    <div class="navbar-nav" *ngIf="user">
+    <div class="navbar-nav" *ngIf="user()">
       <div ngbDropdown class="nav-item dropdown" placement="bottom-right">
         <a ngbDropdownToggle i18n 
           i18n-title
           <i class="material-icons">list</i>
         </a>
         <div class="dropdown-menu" ngbDropdownMenu>
-          <eg-op-change #navOpChange></eg-op-change>
+          <eg-op-change #navOpChange
+            i18n-failMessage
+            i18n-successMessage
+            failMessage="Operator Change Failed"
+            successMessage="Operator Change Succeeded">
+          </eg-op-change>
           <a class="dropdown-item" *ngIf="!opChangeActive()" 
             (click)="navOpChange.open()">
             <span class="material-icons">transform</span>
             <span i18n>Change Operator</span>
           </a>
           <a *ngIf="opChangeActive()" class="dropdown-item" 
-            (click)="restoreOperator()">
+            (click)="navOpChange.restore()">
             <span class="material-icons">transform</span>
             <span i18n>Restore Operator</span>
           </a>
index 1e4053f..14db1d8 100644 (file)
@@ -1,4 +1,4 @@
-import {Component, OnInit} from '@angular/core';
+import {Component, OnInit, ViewChild} from '@angular/core';
 import {ActivatedRoute, Router} from '@angular/router';
 import {EgAuthService} from '@eg/core/auth.service';
 
@@ -10,36 +10,24 @@ import {EgAuthService} from '@eg/core/auth.service';
 
 export class EgStaffNavComponent implements OnInit {
 
-    user: string;
-    workstation: string;
-
     constructor(
         private router: Router,
         private auth: EgAuthService
     ) {}
 
     ngOnInit() {
-        if (this.auth.user()) {
-            this.user = this.auth.user().usrname();
-            this.workstation = this.auth.workstation();
-        }
     }
 
-    opChangeActive(): boolean {
-        return this.auth.opChangeIsActive();
+    user() {
+        return this.auth.user() ? this.auth.user().usrname() : '';
     }
 
-    restoreOperator() { 
-        this.auth.undoOpChange().then(
-            ok => {
-                console.log('OP change undo succeeded');
-            },
-
-            err => {
-                console.warn('OP change undo auth expired');
-                this.router.navigate(['/staff/login']);
-            }
-        );
+    workstation() {
+        return this.auth.user() ? this.auth.workstation() : '';
+    }
+
+    opChangeActive(): boolean {
+        return this.auth.opChangeIsActive();
     }
 
     // Broadcast to all tabs that we're logging out.
index 2451f21..39444f6 100644 (file)
@@ -1,4 +1,5 @@
 import {Component, OnInit, Input, Renderer} from '@angular/core';
+import {EgToastService} from '@eg/share/toast/toast.service';               
 import {EgAuthService} from '@eg/core/auth.service';
 import {EgDialogComponent} from '@eg/share/dialog/dialog.component';
 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
@@ -15,9 +16,13 @@ export class EgOpChangeComponent
     @Input() password: string;
     @Input() loginType: string = 'temp';
 
+    @Input() successMessage: string;
+    @Input() failMessage: string;
+
        constructor(
         private modal: NgbModal, // required for passing to parent
         private renderer: Renderer,
+        private toast: EgToastService,
         private auth: EgAuthService) {
         super(modal);
     }
@@ -35,10 +40,11 @@ export class EgOpChangeComponent
             this.login();
     }
 
-    login() {
-        if (!(this.username && this.password)) return;
+    login(): Promise<any> {
+        if (!(this.username && this.password)) 
+            return Promise.reject('Missing Params');
         
-        this.auth.login(
+        return this.auth.login(
             {   username    : this.username, 
                 password    : this.password, 
                 workstation : this.auth.workstation(),
@@ -46,14 +52,30 @@ export class EgOpChangeComponent
             },  true        // isOpChange
         ).then(
             ok => {
-                console.log('OP change succeeded'),
-                this.close('login OK');
+                this.password = '';
+                this.username = '';
+
+                // Fetch the user object
+                this.auth.testAuthToken().then(
+                    ok => {
+                        this.close();
+                        this.toast.success(this.successMessage);
+                    }
+                );
             },
             notOk => {
-                console.warn('OP change failed')
+                this.password = '';
+                this.toast.danger(this.failMessage);
             }
         );
     }
+
+    restore(): Promise<any> { 
+        return this.auth.undoOpChange().then(
+            ok => this.toast.success(this.successMessage),
+            err => this.toast.danger(this.failMessage)
+        );
+    }
 }