Implement then else for haspackage
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good

This commit is contained in:
Willem Dantuma 2021-03-20 11:06:04 +01:00
parent 00346a963e
commit 997ebb24a7

View File

@ -5,29 +5,56 @@ import { IPackages } from '../../models/package';
import { Observable, Subscription } from 'rxjs'; import { Observable, Subscription } from 'rxjs';
@Directive({ @Directive({
selector: '[fm-haspackage]', selector: '[fmHasPackage]',
}) })
export class HasPackageDirective implements OnInit,OnDestroy{ export class HasPackageDirective implements OnDestroy{
@Input('fm-haspackage') package:string; @Input()
set fmHasPackage(packageIdentifier:string) {
constructor(private templateRef$: TemplateRef<any>,private viewContainerRef$: ViewContainerRef,private store$: Store<appCommonReducer.State>) { } this.packageIdentifier$ = packageIdentifier;
private packages$:Observable<IPackages> = this.store$.select(appCommonReducer.SelectGetUserPackages); this.updateView();
private hasView = false;
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;
} }
@Input()
set fmHasPackageThen(thenTemplate:TemplateRef<any>) {
this.thenTemplate$ = thenTemplate;
this.updateView();
}
@Input()
set fmHasPackageElse(thenTemplate:TemplateRef<any>) {
this.elseTemplate$ = thenTemplate;
this.updateView();
}
private packageIdentifier$:string;
private thenTemplate$:TemplateRef<any>;
private elseTemplate$:TemplateRef<any>;
private packages$:any = {};
private packagesObservable$:Observable<IPackages> = this.store$.select(appCommonReducer.SelectGetUserPackages);
private packSub:Subscription;
constructor(private templateRef$: TemplateRef<any>,private viewContainerRef$: ViewContainerRef,private store$: Store<appCommonReducer.State>) {
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() { ngOnDestroy() {