linting
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import {Directive, ElementRef, Input, OnInit, TemplateRef, ViewContainerRef, inject} from '@angular/core';
|
||||
import {Directive, DestroyRef, Input, OnInit, TemplateRef, ViewContainerRef, inject} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {UserService} from './user.service';
|
||||
|
||||
@Directive({selector: '[appOwner]'})
|
||||
export class OwnerDirective implements OnInit {
|
||||
private element = inject(ElementRef);
|
||||
private templateRef = inject<TemplateRef<unknown>>(TemplateRef);
|
||||
private viewContainer = inject(ViewContainerRef);
|
||||
private userService = inject(UserService);
|
||||
private destroyRef = inject(DestroyRef);
|
||||
|
||||
private currentUserId: string | null = null;
|
||||
private iAppOwner: string | null = null;
|
||||
@@ -18,14 +19,14 @@ export class OwnerDirective implements OnInit {
|
||||
}
|
||||
|
||||
public ngOnInit(): void {
|
||||
this.userService.userId$.subscribe(user => {
|
||||
this.userService.userId$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(user => {
|
||||
this.currentUserId = user;
|
||||
this.updateView();
|
||||
});
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
private updateView() {
|
||||
private updateView(): void {
|
||||
this.viewContainer.clear();
|
||||
if (this.currentUserId === this.iAppOwner) {
|
||||
this.viewContainer.createEmbeddedView(this.templateRef);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {ChangeDetectorRef, Directive, ElementRef, Input, OnInit, TemplateRef, ViewContainerRef, inject} from '@angular/core';
|
||||
import {ChangeDetectorRef, DestroyRef, Directive, Input, OnInit, TemplateRef, ViewContainerRef, inject} from '@angular/core';
|
||||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
|
||||
import {roles} from './roles';
|
||||
import {UserService} from './user.service';
|
||||
import {User} from './user';
|
||||
@@ -6,11 +7,11 @@ import {combineLatest} from 'rxjs';
|
||||
|
||||
@Directive({selector: '[appRole]'})
|
||||
export class RoleDirective implements OnInit {
|
||||
private element = inject(ElementRef);
|
||||
private templateRef = inject<TemplateRef<unknown>>(TemplateRef);
|
||||
private viewContainer = inject(ViewContainerRef);
|
||||
private userService = inject(UserService);
|
||||
private changeDetection = inject(ChangeDetectorRef);
|
||||
private destroyRef = inject(DestroyRef);
|
||||
|
||||
@Input() public appRole: roles[] = [];
|
||||
private currentUser: User | null = null;
|
||||
@@ -18,14 +19,16 @@ export class RoleDirective implements OnInit {
|
||||
private currentViewState = false;
|
||||
|
||||
public ngOnInit(): void {
|
||||
combineLatest([this.userService.user$, this.userService.loggedIn$()]).subscribe(_ => {
|
||||
this.currentUser = _[0];
|
||||
this.loggedIn = _[1];
|
||||
this.updateView();
|
||||
});
|
||||
combineLatest([this.userService.user$, this.userService.loggedIn$()])
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe(([user, loggedIn]) => {
|
||||
this.currentUser = user;
|
||||
this.loggedIn = loggedIn;
|
||||
this.updateView();
|
||||
});
|
||||
}
|
||||
|
||||
private updateView() {
|
||||
private updateView(): void {
|
||||
const viewState = this.loggedIn && this.checkPermission();
|
||||
if (this.currentViewState !== viewState) {
|
||||
if (!viewState) this.viewContainer.clear();
|
||||
@@ -35,7 +38,7 @@ export class RoleDirective implements OnInit {
|
||||
}
|
||||
}
|
||||
|
||||
private checkPermission() {
|
||||
private checkPermission(): boolean {
|
||||
if (this.currentUser && this.currentUser.role) {
|
||||
if (this.currentUser.role === 'admin') {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user