diff --git a/projects/common/src/fm/components/has-package/has-package.directive.ts b/projects/common/src/fm/components/has-package/has-package.directive.ts index 6072785..e96d7c7 100644 --- a/projects/common/src/fm/components/has-package/has-package.directive.ts +++ b/projects/common/src/fm/components/has-package/has-package.directive.ts @@ -5,29 +5,56 @@ import { IPackages } from '../../models/package'; import { Observable, Subscription } from 'rxjs'; @Directive({ - selector: '[fm-haspackage]', + selector: '[fmHasPackage]', }) -export class HasPackageDirective implements OnInit,OnDestroy{ - @Input('fm-haspackage') package:string; +export class HasPackageDirective implements OnDestroy{ + @Input() + set fmHasPackage(packageIdentifier:string) { + this.packageIdentifier$ = packageIdentifier; + this.updateView(); + } - constructor(private templateRef$: TemplateRef,private viewContainerRef$: ViewContainerRef,private store$: Store) { } - private packages$:Observable = this.store$.select(appCommonReducer.SelectGetUserPackages); - private hasView = false; + @Input() + set fmHasPackageThen(thenTemplate:TemplateRef) { + this.thenTemplate$ = thenTemplate; + this.updateView(); + } + + @Input() + set fmHasPackageElse(thenTemplate:TemplateRef) { + this.elseTemplate$ = thenTemplate; + this.updateView(); + } + + private packageIdentifier$:string; + private thenTemplate$:TemplateRef; + private elseTemplate$:TemplateRef; + private packages$:any = {}; + private packagesObservable$:Observable = this.store$.select(appCommonReducer.SelectGetUserPackages); private packSub:Subscription; - ngOnInit() { - this.packages$.subscribe((packages) => { - const today = new Date(new Date(Date.now()).toUTCString()).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; - } + + constructor(private templateRef$: TemplateRef,private viewContainerRef$: ViewContainerRef,private store$: Store) { + this.thenTemplate$=templateRef$; + this.packSub = this.store$.select(appCommonReducer.SelectGetUserPackages).subscribe((packages) => { + this.packages$ = packages; + this.updateView(); }); + this.updateView(); + } + + + updateView() { + const today = new Date(new Date(Date.now()).toUTCString()).setHours(0, 0, 0, 0); + if (this.packages$[this.packageIdentifier$] && + this.packages$[this.packageIdentifier$].enabled && + (this.packages$[this.packageIdentifier$].dataDate && new Date(this.packages$[this.packageIdentifier$].dataDate).setHours(0, 0, 0, 0) <= today) && + (this.packages$[this.packageIdentifier$].dataEndDate == null || new Date(this.packages$[this.packageIdentifier$].dataEndDate).setHours(0, 0, 0, 0) >= today)) { + this.viewContainerRef$.createEmbeddedView(this.thenTemplate$); + } else if (this.elseTemplate$) { + this.viewContainerRef$.createEmbeddedView(this.elseTemplate$); + } else { + this.viewContainerRef$.clear(); + } } ngOnDestroy() {