Broker System for Supercredit
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

124 行
3.0KB

  1. import {Component, OnDestroy, OnInit} from '@angular/core';
  2. import {MenuService} from '../service/menu.service';
  3. import {AuthService} from '../service/auth.service';
  4. import {brokerMenuItems, mainMenuItems, 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. default:
  64. this.showMenu = false;
  65. }
  66. }
  67. // check menuItem has fontawesome
  68. public menuItemHasFontawesome(item: any): boolean {
  69. return item.hasOwnProperty('fa');
  70. }
  71. // menuItem clicked
  72. public onSelect({ item }): void {
  73. if (!item.items) {
  74. this.menuService.itemClicked.emit(item);
  75. // handle logout
  76. if (item.text.toLowerCase() === 'logout'){
  77. this.logout();
  78. }
  79. }
  80. }
  81. ngOnDestroy(): void {
  82. this.loginSub.unsubscribe();
  83. this.logoutSub.unsubscribe();
  84. }
  85. public logout(): void{
  86. this.opened = true;
  87. }
  88. public close(action: string): void {
  89. this.opened = false;
  90. if ( action === 'yes') {
  91. // this.login = false;
  92. this.authService.logout();
  93. }
  94. }
  95. public loggedInUserAvatar(): string {
  96. return this.authService.getUrl('avatar/') + this.ss.loggedIn.User.Id;
  97. }
  98. public profile(): void {
  99. this.menuService.showProfile();
  100. }
  101. }