import { Directive, ViewContainerRef,TemplateRef,OnInit,Input,OnDestroy } from '@angular/core'; import { Store} from '@ngrx/store'; import * as appCommonReducer from '../../reducers/app-common.reducer' import { IPackages } from '../../models/package'; import { Observable, Subscription } from 'rxjs'; @Directive({ selector: '[fm-haspackage]', }) export class HasPackageDirective implements OnInit,OnDestroy{ @Input('fm-haspackage') package:string; constructor(private templateRef$: TemplateRef,private viewContainerRef$: ViewContainerRef,private store$: Store) { } private packages$:Observable = this.store$.select(appCommonReducer.SelectGetUserPackages); private hasView = false; private packSub:Subscription; ngOnInit() { this.packages$.subscribe((packages) => { const today = new Date(Date.prototype.getUTCDate()).setHours(0,0,0,0); if (packages[this.package] && packages[this.package].enabled && (packages[this.package].DataDate && new Date(packages[this.package].DataDate).setHours(0, 0, 0, 0) <= today) && (packages[this.package].DataEndDate == null || new Date(packages[this.package].DataEndDate).setHours(0, 0, 0, 0) >= today)) { this.viewContainerRef$.createEmbeddedView(this.templateRef$); this.hasView=true; } else if (this.hasView) { this.viewContainerRef$.clear(); this.hasView = false; } }); } ngOnDestroy() { if(this.packSub) this.packSub.unsubscribe(); } }