|
- import {Component, OnDestroy, OnInit} from '@angular/core';
- import {MenuService} from '../service/menu.service';
- import {AuthService} from '../service/auth.service';
- import {mainMenuItems} from '../main-menu-items';
- import {apiV1LoginResponse} from '../models/api-v1-login-response';
- import {Subscription} from 'rxjs';
-
- @Component({
- selector: 'app-top-bar',
- templateUrl: './top-bar.component.html',
- styleUrls: ['./top-bar.component.scss']
- })
- export class TopBarComponent implements OnInit , OnDestroy {
- login = false;
- Avatar = './assets/img/avatar.png';
- public items: any[] = mainMenuItems;
- private loginSub: Subscription;
-
- constructor(private menuService: MenuService, private authService: AuthService,) { }
-
- ngOnInit(): void {
- this.initAndSubLogin();
- }
-
- public initAndSubLogin(): void{
- this.login = this.authService.loggedIn.login;
- console.log('subscribe auto login');
- this.loginSub = this.authService.loginSuccess.subscribe(
- (rsp: apiV1LoginResponse) => {
- this.login = rsp.login;
- console.log ('topbar received auth events', rsp);
- }
- );
- }
-
- // check menuItem has fontawesome
- public menuItemHasFontawesome(item: any): boolean {
- return item.hasOwnProperty('fa');
- }
-
- // menuItem clicked
- public onSelect({ item }): void {
- if (!item.items) {
- this.menuService.itemClicked.emit(item);
-
- // handle logout
- if (item.text === 'Logout'){
- this.authService.logout();
- this.login = false;
- // this.authService.loginSuccess.emit("loggedout");
- }
- }
- }
-
- // tslint:disable-next-line:typedef
- ngOnDestroy() {
- this.loginSub.unsubscribe();
- }
-
- }
|