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: `
`
})
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 = 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();
});
if (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( {
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( {
msg: 'Unable to load User Applications'
});
}
}