ngx-admin/src/app/@theme/components/header/header.component.ts

111 lines
3 KiB
TypeScript
Raw Normal View History

import { Component, OnDestroy, OnInit } from '@angular/core';
2024-03-21 15:32:11 -07:00
import { NbMediaBreakpointsService, NbMenuService, NbSidebarService, NbThemeService, NbMenuItem } from '@nebular/theme';
import { UserData } from '../../../@core/data/users';
import { LayoutService } from '../../../@core/utils';
import { map, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
2024-03-21 15:32:11 -07:00
import { AuthService } from '../../../service/auth.service';
2024-03-21 18:13:58 -07:00
import { Router } from '@angular/router';
@Component({
selector: 'ngx-header',
2017-04-28 15:14:05 +03:00
styleUrls: ['./header.component.scss'],
templateUrl: './header.component.html',
})
export class HeaderComponent implements OnInit, OnDestroy {
private destroy$: Subject<void> = new Subject<void>();
userPictureOnly: boolean = false;
user: any;
themes = [
{
value: 'default',
name: 'Light',
},
{
value: 'dark',
name: 'Dark',
},
2024-03-21 15:32:11 -07:00
// {
// value: 'cosmic',
// name: 'Cosmic',
// },
// {
// value: 'corporate',
// name: 'Corporate',
// },
];
currentTheme = 'default';
userMenu = [ { title: 'Profile' }, { title: 'Log out' } ];
constructor(private sidebarService: NbSidebarService,
private menuService: NbMenuService,
private themeService: NbThemeService,
private userService: UserData,
private layoutService: LayoutService,
2024-03-21 15:32:11 -07:00
private breakpointService: NbMediaBreakpointsService,
2024-03-21 18:13:58 -07:00
private authService: AuthService,
private router: Router) {
}
ngOnInit() {
this.currentTheme = this.themeService.currentTheme;
this.userService.getUsers()
.pipe(takeUntil(this.destroy$))
.subscribe((users: any) => this.user = users.nick);
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);
2024-03-21 15:32:11 -07:00
this.menuService.onItemClick().subscribe((event: { item: NbMenuItem }) => {
if (event.item.title === 'Log out') {
this.logout();
2024-03-21 18:13:58 -07:00
} else if (event.item.title === 'Profile') {
this.router.navigate(['/pages/layout/stepper/profile']); // Redirect to profile page
2024-03-21 15:32:11 -07:00
}
});
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
changeTheme(themeName: string) {
this.themeService.changeTheme(themeName);
}
2017-06-21 17:34:10 +03:00
toggleSidebar(): boolean {
this.sidebarService.toggle(true, 'menu-sidebar');
this.layoutService.changeLayoutSize();
return false;
}
navigateHome() {
this.menuService.navigateHome();
return false;
}
2024-03-21 15:32:11 -07:00
logout(): void {
this.authService.logout();
}
}