Peter Bastiani 84a1a04b19
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
AW-6046 Angular improvement
2024-04-15 10:29:47 +02:00

27 lines
1.0 KiB
TypeScript

import { Directive, Input, OnInit, TemplateRef, ViewContainerRef } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { IUser } from '../../models/user';
import * as appCommonReducer from '../../reducers/app-common.reducer';
@Directive({
selector: '[fm-hasclaim]',
})
export class HasClaimDirective implements OnInit{
@Input('fm-hasclaim') claim:string;
constructor(private templateRef$: TemplateRef<any>,private viewContainerRef$: ViewContainerRef,private store$: Store<appCommonReducer.State>) { }
private user$:Observable<IUser> = this.store$.select(appCommonReducer.SelectGetUser);
private hasView = false;
ngOnInit() {
this.user$.subscribe((user) => {
if (user && user.claims[this.claim]) {
this.viewContainerRef$.createEmbeddedView(this.templateRef$);
this.hasView=true;
} else if (this.hasView) {
this.viewContainerRef$.clear();
this.hasView = false;
}
});
}
}