Removed themeService from templates #70
This commit is contained in:
parent
ba881aefa8
commit
1a3126dfc5
@ -1,11 +1,11 @@
|
|||||||
<main [class]="themeService.themeName">
|
<main [class]="themeName">
|
||||||
<ng-container *ngIf="authService.isLoggedIn; else login">
|
<ng-container *ngIf="authService.isLoggedIn; else login">
|
||||||
<app-header (isSidebarFullWidth)="themeService.setSideWidth($event)"></app-header>
|
<app-header></app-header>
|
||||||
|
|
||||||
<section class="app">
|
<section class="app">
|
||||||
<div>
|
<div>
|
||||||
<section class="sidebar" [style.width]="themeService.sidebarWidth">
|
<section class="sidebar" [style.width]="sidebarWidth">
|
||||||
<app-sidebar [isSidebarOpen]="themeService.isSidebarOpen"></app-sidebar>
|
<app-sidebar></app-sidebar>
|
||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
<div class="component-wrapper">
|
<div class="component-wrapper">
|
||||||
|
@ -10,15 +10,32 @@ import { ThemeService } from './services/theme/theme.service';
|
|||||||
})
|
})
|
||||||
export class AppComponent implements OnInit {
|
export class AppComponent implements OnInit {
|
||||||
|
|
||||||
|
themeName!: string;
|
||||||
|
sidebarWidth!: string;
|
||||||
|
|
||||||
|
isLoggedIn: boolean = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public authService: AuthService,
|
public authService: AuthService,
|
||||||
public themeService: ThemeService,
|
private themeService: ThemeService,
|
||||||
private socket: SocketService
|
private socket: SocketService
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
|
this.themeService.sidebarWidth$.subscribe(value => {
|
||||||
|
this.sidebarWidth = value;
|
||||||
|
});
|
||||||
|
this.themeService.themeName$.subscribe(value => {
|
||||||
|
this.themeName = value;
|
||||||
|
});
|
||||||
|
|
||||||
this.socket.startSocket();
|
this.socket.startSocket();
|
||||||
this.themeService.loadTheme();
|
this.themeService.loadTheme();
|
||||||
this.themeService.loadMenu();
|
this.themeService.loadMenu();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
setSideWidth($event: any): void {
|
||||||
|
this.themeService.setSideWidth($event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
|
import { Component, OnInit } from '@angular/core';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
|
import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
|
||||||
import { MenuItem, PrimeNGConfig } from 'primeng/api';
|
import { MenuItem, PrimeNGConfig } from 'primeng/api';
|
||||||
@ -14,8 +14,6 @@ import { ThemeService } from 'src/app/services/theme/theme.service';
|
|||||||
styleUrls: ['./header.component.scss']
|
styleUrls: ['./header.component.scss']
|
||||||
})
|
})
|
||||||
export class HeaderComponent implements OnInit {
|
export class HeaderComponent implements OnInit {
|
||||||
@Output() isSidebarFullWidth: EventEmitter<boolean> = new EventEmitter<boolean>(this.themeService.isSidebarOpen);
|
|
||||||
|
|
||||||
langList: MenuItem[] = [];
|
langList: MenuItem[] = [];
|
||||||
themeList: MenuItem[] = [];
|
themeList: MenuItem[] = [];
|
||||||
userMenuList!: MenuItem[];
|
userMenuList!: MenuItem[];
|
||||||
@ -112,7 +110,6 @@ export class HeaderComponent implements OnInit {
|
|||||||
|
|
||||||
toggleMenu(): void {
|
toggleMenu(): void {
|
||||||
this.themeService.setIsMenuOpen(!this.themeService.isSidebarOpen);
|
this.themeService.setIsMenuOpen(!this.themeService.isSidebarOpen);
|
||||||
this.isSidebarFullWidth.emit(this.themeService.isSidebarOpen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
changeTheme(name: string): void {
|
changeTheme(name: string): void {
|
||||||
|
@ -3,50 +3,57 @@ import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
|
|||||||
import { MenuItem } from 'primeng/api';
|
import { MenuItem } from 'primeng/api';
|
||||||
import { AuthRoles } from 'src/app/models/auth/auth-roles.enum';
|
import { AuthRoles } from 'src/app/models/auth/auth-roles.enum';
|
||||||
import { AuthService } from 'src/app/services/auth/auth.service';
|
import { AuthService } from 'src/app/services/auth/auth.service';
|
||||||
|
import { ThemeService } from 'src/app/services/theme/theme.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-sidebar',
|
selector: 'app-sidebar',
|
||||||
templateUrl: './sidebar.component.html',
|
templateUrl: './sidebar.component.html',
|
||||||
styleUrls: ['./sidebar.component.scss']
|
styleUrls: ['./sidebar.component.scss']
|
||||||
})
|
})
|
||||||
export class SidebarComponent implements OnInit, OnChanges {
|
export class SidebarComponent implements OnInit {
|
||||||
|
|
||||||
@Input() isSidebarOpen!: boolean;
|
isSidebarOpen: boolean = true;
|
||||||
|
|
||||||
menuItems!: MenuItem[];
|
menuItems!: MenuItem[];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private authService: AuthService,
|
private authService: AuthService,
|
||||||
private translateService: TranslateService
|
private translateService: TranslateService,
|
||||||
|
private themeService: ThemeService
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.translateService.onLangChange.subscribe(async (event: LangChangeEvent) => {
|
this.themeService.isSidebarOpen$.subscribe(value => {
|
||||||
await this.setMenu(this.isSidebarOpen);
|
this.isSidebarOpen = value;
|
||||||
|
this.setMenu();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
|
||||||
|
this.setMenu();
|
||||||
|
});
|
||||||
|
this.setMenu();
|
||||||
|
}
|
||||||
|
|
||||||
|
setMenu() {
|
||||||
|
|
||||||
|
this.authService.hasUserPermission(AuthRoles.Admin).then(hasPermission => {
|
||||||
|
this.menuItems = [];
|
||||||
|
this.menuItems = [
|
||||||
|
{ label: this.isSidebarOpen ? this.translateService.instant('sidebar.dashboard') : '', icon: 'pi pi-th-large', routerLink: 'dashboard' },
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!hasPermission) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.menuItems.push(
|
||||||
|
{ separator: true },
|
||||||
|
{ label: this.isSidebarOpen ? this.translateService.instant('sidebar.config') : '', icon: 'pi pi-cog', routerLink: '/admin/settings' },
|
||||||
|
{ label: this.isSidebarOpen ? this.translateService.instant('sidebar.auth_user_list') : '', icon: 'pi pi-user-edit', routerLink: '/admin/users' },
|
||||||
|
);
|
||||||
|
this.menuItems = this.menuItems.slice();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async setMenu(isSidebarOpen: boolean) {
|
|
||||||
this.menuItems = [];
|
|
||||||
this.menuItems = [
|
|
||||||
{ label: isSidebarOpen ? this.translateService.instant('sidebar.dashboard') : '', icon: 'pi pi-th-large', routerLink: 'dashboard' },
|
|
||||||
];
|
|
||||||
|
|
||||||
if (await this.authService.hasUserPermission(AuthRoles.Admin)) {
|
|
||||||
this.menuItems.push(
|
|
||||||
{ separator: true },
|
|
||||||
{ label: isSidebarOpen ? this.translateService.instant('sidebar.config') : '', icon: 'pi pi-cog', routerLink: '/admin/settings' },
|
|
||||||
{ label: isSidebarOpen ? this.translateService.instant('sidebar.auth_user_list') : '', icon: 'pi pi-user-edit', routerLink: '/admin/users' },
|
|
||||||
);
|
|
||||||
this.menuItems = this.menuItems.slice();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async ngOnChanges(changes: SimpleChanges): Promise<void> {
|
|
||||||
if (!changes)
|
|
||||||
return;
|
|
||||||
|
|
||||||
await this.setMenu(changes['isSidebarOpen'].currentValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -28,10 +28,13 @@ export class AuthGuard implements CanActivate {
|
|||||||
|
|
||||||
const role = route.data['role'];
|
const role = route.data['role'];
|
||||||
if (role) {
|
if (role) {
|
||||||
if (!await this.authService.hasUserPermission(role)) {
|
this.authService.hasUserPermission(role).then(hasPermission => {
|
||||||
this.router.navigate(['/dashboard']);
|
if (!hasPermission) {
|
||||||
return false;
|
this.router.navigate(['/dashboard']);
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -229,12 +229,14 @@ export class AuthService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async hasUserPermission(role: AuthRoles): Promise<boolean> {
|
hasUserPermission(role: AuthRoles): Promise<boolean> {
|
||||||
if (!role || !await this.isUserLoggedInAsync()) {
|
return this.isUserLoggedInAsync().then(isLoggedIn => {
|
||||||
return false;
|
if (!role || !isLoggedIn) {
|
||||||
}
|
return false;
|
||||||
const token = this.getDecodedToken();
|
}
|
||||||
return AuthRoles[token['role']] === AuthRoles[role];
|
const token = this.getDecodedToken();
|
||||||
|
return AuthRoles[token['role']] === AuthRoles[role];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getEMailFromDecodedToken(token: { [key: string]: any }): string | null {
|
getEMailFromDecodedToken(token: { [key: string]: any }): string | null {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
|
||||||
import { Subject } from 'rxjs';
|
import { Subject } from 'rxjs';
|
||||||
import { Themes } from 'src/app/models/view/themes.enum';
|
import { Themes } from 'src/app/models/view/themes.enum';
|
||||||
import { AuthService } from '../auth/auth.service';
|
import { AuthService } from '../auth/auth.service';
|
||||||
@ -8,23 +9,31 @@ import { AuthService } from '../auth/auth.service';
|
|||||||
})
|
})
|
||||||
export class ThemeService {
|
export class ThemeService {
|
||||||
|
|
||||||
themeName: string | null;
|
themeName: string = Themes.Default;
|
||||||
|
|
||||||
sidebarWidth = '150px';
|
sidebarWidth = '150px';
|
||||||
isSidebarOpen = false;
|
isSidebarOpen = false;
|
||||||
hasLangChanged = false;
|
hasLangChanged = false;
|
||||||
|
|
||||||
|
themeName$ = new Subject<string>();
|
||||||
isSidebarOpen$ = new Subject<boolean>();
|
isSidebarOpen$ = new Subject<boolean>();
|
||||||
|
sidebarWidth$ = new Subject<string>();
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private authService: AuthService
|
private authService: AuthService
|
||||||
) {
|
) {
|
||||||
this.isSidebarOpen$.subscribe(isSidebarOpen => {
|
this.themeName$.subscribe(themeName => {
|
||||||
this.isSidebarOpen = isSidebarOpen
|
this.themeName = themeName;
|
||||||
});
|
});
|
||||||
|
this.isSidebarOpen$.subscribe(isSidebarOpen => {
|
||||||
|
this.isSidebarOpen = isSidebarOpen;
|
||||||
|
});
|
||||||
|
this.sidebarWidth$.subscribe(sidebarWidth => {
|
||||||
|
this.sidebarWidth = sidebarWidth;
|
||||||
|
});
|
||||||
|
|
||||||
this.loadTheme();
|
this.loadTheme();
|
||||||
this.loadMenu();
|
this.loadMenu();
|
||||||
this.themeName = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadTheme(): void {
|
loadTheme(): void {
|
||||||
@ -53,7 +62,7 @@ export class ThemeService {
|
|||||||
this.authService.isUserLoggedInAsync().then(result => {
|
this.authService.isUserLoggedInAsync().then(result => {
|
||||||
if (!result) {
|
if (!result) {
|
||||||
localStorage.setItem(`default_themeName`, Themes.Default);
|
localStorage.setItem(`default_themeName`, Themes.Default);
|
||||||
this.themeName = Themes.Default;
|
this.themeName$.next(Themes.Default);
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = this.authService.getDecodedToken();
|
const token = this.authService.getDecodedToken();
|
||||||
@ -62,7 +71,7 @@ export class ThemeService {
|
|||||||
localStorage.setItem(`${mail}_themeName`, name);
|
localStorage.setItem(`${mail}_themeName`, name);
|
||||||
}
|
}
|
||||||
localStorage.setItem(`default_themeName`, name);
|
localStorage.setItem(`default_themeName`, name);
|
||||||
this.themeName = name;
|
this.themeName$.next(name);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,8 +106,8 @@ export class ThemeService {
|
|||||||
this.setSideWidth(isMenuOpen);
|
this.setSideWidth(isMenuOpen);
|
||||||
}
|
}
|
||||||
|
|
||||||
setSideWidth($event: any): void {
|
setSideWidth(isSidebarOpen: boolean): void {
|
||||||
this.sidebarWidth = $event ? '150px' : '50px';
|
this.sidebarWidth$.next(isSidebarOpen ? '150px' : '50px');
|
||||||
this.isSidebarOpen$.next($event)
|
this.isSidebarOpen$.next(isSidebarOpen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user