Add premium and unless-premium directives (#710)

This commit is contained in:
Oscar Hinton 2022-03-08 15:21:49 +01:00 committed by GitHub
parent 744649573a
commit f7375e9064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,27 @@
import { Directive, OnInit, TemplateRef, ViewContainerRef } from "@angular/core";
import { StateService } from "jslib-common/abstractions/state.service";
/**
* Hides the element if the user has premium.
*/
@Directive({
selector: "[appNotPremium]",
})
export class NotPremiumDirective implements OnInit {
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private stateService: StateService
) {}
async ngOnInit(): Promise<void> {
const premium = await this.stateService.getCanAccessPremium();
if (premium) {
this.viewContainer.clear();
} else {
this.viewContainer.createEmbeddedView(this.templateRef);
}
}
}

View File

@ -0,0 +1,27 @@
import { Directive, OnInit, TemplateRef, ViewContainerRef } from "@angular/core";
import { StateService } from "jslib-common/abstractions/state.service";
/**
* Only shows the element if the user has premium.
*/
@Directive({
selector: "[appPremium]",
})
export class PremiumDirective implements OnInit {
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef,
private stateService: StateService
) {}
async ngOnInit(): Promise<void> {
const premium = await this.stateService.getCanAccessPremium();
if (premium) {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
}