ngx-admin/src/app/@theme/components/header/header.component.ts
2020-06-03 14:21:26 +07:00

111 lines
2.7 KiB
TypeScript

import { Component, OnDestroy, OnInit } from '@angular/core';
import { NbMediaBreakpointsService, NbMenuService, NbSidebarService, NbThemeService } from '@nebular/theme';
import { UserData } from '../../../@core/data/users';
import { LayoutService } from '../../../@core/utils';
import { map, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { User } from '../../../@core/data/user';
import { Router } from '@angular/router';
import { LoginService } from '../../../auth/login.service';
@Component({
selector: 'ngx-header',
styleUrls: ['./header.component.scss'],
templateUrl: './header.component.html',
})
export class HeaderComponent implements OnInit, OnDestroy {
private destroy$: Subject<void> = new Subject<void>();
userPictureOnly: boolean = false;
username: any;
roles: any;
userpicture: any;
themes = [
{
value: 'default',
name: 'Light',
},
{
value: 'dark',
name: 'Dark',
},
{
value: 'cosmic',
name: 'Cosmic',
},
{
value: 'corporate',
name: 'Corporate',
},
];
currentTheme = 'default';
userMenu = [{ title: 'User Role(s)' }];
constructor(private sidebarService: NbSidebarService,
private router: Router,
private menuService: NbMenuService,
private themeService: NbThemeService,
private layoutService: LayoutService,
private loginService: LoginService,
private breakpointService: NbMediaBreakpointsService) {
}
ngOnInit() {
this.username = localStorage.getItem('username');
this.roles = localStorage.getItem('roles');
this.userpicture = 'assets/images/eva.png';
if (this.username == null) {
this.router.navigate(['/auth/login']);
}
this.username = this.username.replace(/['"]+/g, '');
this.currentTheme = this.themeService.currentTheme;
const { xl } = this.breakpointService.getBreakpointsMap();
this.themeService.onMediaQueryChange()
.pipe(
map(([, currentBreakpoint]) => currentBreakpoint.width < xl),
takeUntil(this.destroy$),
)
.subscribe((isLessThanXl: boolean) => this.userPictureOnly = isLessThanXl);
this.themeService.onThemeChange()
.pipe(
map(({ name }) => name),
takeUntil(this.destroy$),
)
.subscribe(themeName => this.currentTheme = themeName);
}
logout(){
this.loginService.logout();
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
changeTheme(themeName: string) {
this.themeService.changeTheme(themeName);
}
toggleSidebar(): boolean {
this.sidebarService.toggle(true, 'menu-sidebar');
this.layoutService.changeLayoutSize();
return false;
}
navigateHome() {
this.menuService.navigateHome();
return false;
}
}