linting
This commit is contained in:
@@ -16,17 +16,17 @@ function normalize(input: string): string {
|
||||
|
||||
export const onlyUnique = <T>(value: T, index: number, array: T[]) => array.indexOf(value) === index;
|
||||
|
||||
export function dynamicSort(property: string) {
|
||||
export function dynamicSort<T extends Record<string, string | number>>(property: keyof T | `-${string & keyof T}`) {
|
||||
let sortOrder = 1;
|
||||
if (property[0] === '-') {
|
||||
let resolvedProperty = property as string;
|
||||
if (resolvedProperty[0] === '-') {
|
||||
sortOrder = -1;
|
||||
property = property.substr(1);
|
||||
resolvedProperty = resolvedProperty.slice(1);
|
||||
}
|
||||
return function (a: unknown, b: unknown) {
|
||||
/* next line works with strings and numbers,
|
||||
* and you may want to customize it to your needs
|
||||
*/
|
||||
const result = a[property] < b[property] ? -1 : a[property] > b[property] ? 1 : 0;
|
||||
return function (a: T, b: T): number {
|
||||
const left = a[resolvedProperty as keyof T];
|
||||
const right = b[resolvedProperty as keyof T];
|
||||
const result = left < right ? -1 : left > right ? 1 : 0;
|
||||
return result * sortOrder;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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