Broker System for Supercredit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.1KB

  1. import {Component, OnDestroy, OnInit} from '@angular/core';
  2. import {MenuService} from '../service/menu.service';
  3. import {AuthService} from '../service/auth.service';
  4. import {accountantMenuItems, brokerMenuItems, mainMenuItems, managerMenuItems, peopleMenuItems, userMenuItems} from '../main-menu-items';
  5. import {ApiV1LoginResponse} from '../models/api-v1-login-response';
  6. import {Subscription} from 'rxjs';
  7. import {PeopleModel} from '../models/people.model';
  8. import {SessionService} from '../service/session.service';
  9. @Component({
  10. selector: 'app-top-bar',
  11. templateUrl: './top-bar.component.html',
  12. styleUrls: ['./top-bar.component.scss']
  13. })
  14. export class TopBarComponent implements OnInit , OnDestroy {
  15. login = false;
  16. public LoggedInUser: PeopleModel;
  17. Avatar = './assets/img/logout.png';
  18. public items: any[] = mainMenuItems;
  19. private loginSub: Subscription;
  20. private logoutSub: Subscription;
  21. public showMenu = true;
  22. public opened = false;
  23. constructor(private menuService: MenuService,
  24. private ss: SessionService,
  25. private authService: AuthService) {
  26. }
  27. ngOnInit(): void {
  28. this.initAndSubLogin();
  29. }
  30. public initAndSubLogin(): void{
  31. this.login = this.ss.loggedIn.login;
  32. this.LoggedInUser = this.ss.loggedIn.User;
  33. this.selectMenuShow(this.ss.loggedIn.role);
  34. this.loginSub = this.ss.loginResult.subscribe(
  35. (rsp: ApiV1LoginResponse) => {
  36. this.login = rsp.login;
  37. this.LoggedInUser = this.ss.CurrentUser;
  38. this.selectMenuShow(rsp.role);
  39. }
  40. );
  41. this.logoutSub = this.ss.logoutEvent.subscribe(
  42. resp => {
  43. this.login = false;
  44. this.LoggedInUser = this.ss.CurrentUser; // always empty user
  45. }
  46. );
  47. }
  48. private selectMenuShow(role: string): void {
  49. this.showMenu = true;
  50. switch ( role ){
  51. case 'admin':
  52. this.items = mainMenuItems;
  53. break;
  54. case 'broker':
  55. this.items = brokerMenuItems;
  56. break;
  57. case 'user':
  58. this.items = userMenuItems;
  59. break;
  60. case 'people':
  61. this.items = peopleMenuItems;
  62. break;
  63. case 'manager':
  64. this.items = managerMenuItems;
  65. break;
  66. case 'accountant':
  67. this.items = accountantMenuItems;
  68. break;
  69. default:
  70. this.showMenu = false;
  71. }
  72. }
  73. // check menuItem has fontawesome
  74. public menuItemHasFontawesome(item: any): boolean {
  75. return item.hasOwnProperty('fa');
  76. }
  77. // menuItem clicked
  78. public onSelect({ item }): void {
  79. if (!item.items) {
  80. this.menuService.itemClicked.emit(item);
  81. // handle logout
  82. if (item.text.toLowerCase() === 'logout'){
  83. this.logout();
  84. }
  85. }
  86. }
  87. ngOnDestroy(): void {
  88. this.loginSub.unsubscribe();
  89. this.logoutSub.unsubscribe();
  90. }
  91. public logout(): void{
  92. this.opened = true;
  93. }
  94. public close(action: string): void {
  95. this.opened = false;
  96. if ( action === 'yes') {
  97. // this.login = false;
  98. this.authService.logout();
  99. }
  100. }
  101. public loggedInUserAvatar(): string {
  102. return this.authService.getUrl('avatar/') + this.ss.loggedIn.User.Id;
  103. }
  104. public profile(): void {
  105. this.menuService.showProfile();
  106. }
  107. }