all files / app/ app.component.ts

74.07% Statements 40/54
50% Branches 1/2
36.36% Functions 8/22
72.55% Lines 37/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511×   1× 1× 1× 1×   1×                                                                     1×         1×   1× 1×     1× 1× 1× 1×     1× 1× 1×           1×   1× 1×           1×       1×           1× 1×   1× 1×           1×               1×                 1×               1×               1× 1×   1×         1×               1× 1×       1×
import { BsModalService } from 'ngx-bootstrap/ng2-bootstrap';
import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';
import { ToastyService, ToastOptions } from 'ng2-toasty';
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { MENU_OPTIONS } from './menu-options.enum';
 
import {
  IRailincSSOUser,
  AccountSearchTypes,
  RailincUserService,
  NoAccountSelectedModalComponent,
  PermissionService,
  UnauthorizedAccountModalComponent,
  AccountListErrorModalComponent,
  LoginErrorComponent
} from '@railinc/rl-ngx-core';
 
@Component({
  selector: 'app',
  encapsulation: ViewEncapsulation.None,
  styleUrls: [
    './app.component.css'
  ],
  template: `
    <rl-block-ui></rl-block-ui>
    
    <ng2-toasty [position]="'top-right'"></ng2-toasty>
    
    <railinc-application
        applicationName="My Application Name"
        [railincUser]="user"
        [applicationList]="applicationList"
        [menuOptions]="menuOptions"
        [accountList]="accountList"
        [showAccountList]="showAccountList"
        [accountListType]="accountListType"
      >
    
    </railinc-application>
  `
})
export class AppComponent implements OnInit {
  public user: IRailincSSOUser;
  public applicationList: any[];
  public accountList: string[];
 
  public menuOptions: any[] = MENU_OPTIONS;
  private bsModalRef: BsModalRef;
  public showAccountList: boolean = true;
  public accountListType: AccountSearchTypes = AccountSearchTypes.DRODOWN;
 
  constructor(
    private userService: RailincUserService,
    public toastyService: ToastyService,
    private modalService: BsModalService,
    private permissionService: PermissionService
  ) { }
 
  public ngOnInit() {
    setTimeout(() => {
      this.userService.getSSOUser()
        .subscribe((user) => {
          this.user = <IRailincSSOUser> user;
          this.permissionService.setRoleArray(user['userRoles']['userRoles']);
        });
 
      this.userService.getApplications()
        .catch((error) => {
          this.showAppListToast();
          return Observable.throw([]);
        })
        .subscribe((applicationList) => {
          this.applicationList = applicationList;
        });
 
      this.userService.onLoginFailure.debounceTime(250).subscribe(failure => {
        this.showSSOUserError();
      });
 
      this.permissionService.onUnauthorizedRoute
        .debounceTime(250)
        .subscribe(error => {
          this.showUnauthorizedRouteModal();
        });
 
      Eif (this.showAccountList) {
        this.userService.getAccountList()
          .catch(e => {
            this.showAccountListError();
            return Observable.throw([]);
          })
          .subscribe((accountList) => {
            this.accountList = accountList;
          });
 
        this.userService.onAccountChage
          .debounceTime(250)
          .subscribe(account => {
            this.toastyService.info(<ToastOptions> {
              msg: 'New Account Selected: ' + this.userService.SELECTED_ACCOUNT
            });
          });
 
        this.userService.onNoAccountSelected
          .debounceTime(10)
          .subscribe(error => {
            this.showNoAccountSelectedModal();
          });
      }
    }, 0);
  }
 
  private showUnauthorizedRouteModal() {
    this.bsModalRef = this.modalService.show(UnauthorizedAccountModalComponent, { class: 'dialog-sm  dialog-error' });
 
    this.modalService.onHidden.subscribe(data => {
      // Handle exit error modal click
    });
  }
 
  private showNoAccountSelectedModal() {
    this.bsModalRef = this.modalService.show(NoAccountSelectedModalComponent, { class: 'dialog-sm  dialog-error' });
 
    this.modalService.onHidden.subscribe(data => {
      // Handle exit error modal click
    });
  }
 
  private showAccountListError() {
    this.bsModalRef = this.modalService.show(AccountListErrorModalComponent, { class: 'dialog-sm  dialog-error' });
 
    this.modalService.onHidden.subscribe(data => {
      // Handle exit error modal click
    });
  }
 
  private showSSOUserError() {
    this.bsModalRef = this.modalService.show(LoginErrorComponent, { class: 'dialog-sm dialog-error' });
 
    this.modalService.onHide.subscribe(data => {
      //window.location.href = '/sso/logout.do';
    });
  }
 
  private showAppListToast() {
    this.toastyService.error(<ToastOptions> {
      msg: 'Unable to load User Applications'
    });
  }
}