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.

61 lines
1.6KB

  1. import {Component, OnDestroy, OnInit} from '@angular/core';
  2. import {MenuService} from '../service/menu.service';
  3. import {AuthService} from '../service/auth.service';
  4. import {mainMenuItems} from '../main-menu-items';
  5. import {apiV1LoginResponse} from '../models/api-v1-login-response';
  6. import {Subscription} from 'rxjs';
  7. @Component({
  8. selector: 'app-top-bar',
  9. templateUrl: './top-bar.component.html',
  10. styleUrls: ['./top-bar.component.scss']
  11. })
  12. export class TopBarComponent implements OnInit , OnDestroy {
  13. login = false;
  14. Avatar = './assets/img/avatar.png';
  15. public items: any[] = mainMenuItems;
  16. private loginSub: Subscription;
  17. constructor(private menuService: MenuService, private authService: AuthService,) { }
  18. ngOnInit(): void {
  19. this.initAndSubLogin();
  20. }
  21. public initAndSubLogin(): void{
  22. this.login = this.authService.loggedIn.login;
  23. console.log('subscribe auto login');
  24. this.loginSub = this.authService.loginSuccess.subscribe(
  25. (rsp: apiV1LoginResponse) => {
  26. this.login = rsp.login;
  27. console.log ('topbar received auth events', rsp);
  28. }
  29. );
  30. }
  31. // check menuItem has fontawesome
  32. public menuItemHasFontawesome(item: any): boolean {
  33. return item.hasOwnProperty('fa');
  34. }
  35. // menuItem clicked
  36. public onSelect({ item }): void {
  37. if (!item.items) {
  38. this.menuService.itemClicked.emit(item);
  39. // handle logout
  40. if (item.text === 'Logout'){
  41. this.authService.logout();
  42. this.login = false;
  43. // this.authService.loginSuccess.emit("loggedout");
  44. }
  45. }
  46. }
  47. // tslint:disable-next-line:typedef
  48. ngOnDestroy() {
  49. this.loginSub.unsubscribe();
  50. }
  51. }