import {PaywallShare} from "../types.js" import * as loading from "../toolbox/loading.js" import {star as starIcon} from "../system/icons.js" import {styles} from "./styles/metal-paywall-styles.js" import {mixinStyles} from "../framework/mixin-styles.js" import {MetalshopComponent, html, property} from "../framework/metalshop-component.js" @mixinStyles(styles) export class MetalPaywall extends MetalshopComponent { @property({type: Object}) paywallLoad: loading.Load = loading.ready() render() { const {paywallLoad} = this const {personalLoad, premiumUntil, premium, premiumInfoLoad} = this.share const meta = loading.meta(personalLoad, premiumInfoLoad, paywallLoad) const premiumInfo = loading.payload(premiumInfoLoad) return html` ${premium ? this.renderPanelPremium() : this.renderPanelNoPremium()} ${premiumInfo ? this.renderPanelSubscription() : this.renderPanelNoSubscription()} ` } private renderPanelPremium() { return html`
${starIcon}

Premium

` } private renderPanelNoPremium() { return html`

Not premium

` } private renderPanelSubscription() { const {cardClues} = loading.payload(this.share.premiumInfoLoad) const {brand, last4, expireYear, expireMonth} = cardClues return html`

Billing subscription is active

${brand} ends in ${last4} expires ${expireYear}/${expireMonth}

` } private renderPanelNoSubscription () { const {premiumUntil, premium} = this.share const days = (() => { if (!premiumUntil) return null const duration = premiumUntil - Date.now() return Math.ceil(duration / (1000 * 60 * 60 * 24)) })() return html`
${days ? html`

Remaining: ${days} day${days === 1 ? "": "s"}

` : null}

No active billing subscription

${premium ? null : html`
` }
` } private loadingOp = async

(func: () => Promise

): Promise

=> { this.paywallLoad = loading.loading() try { const result = await func() this.paywallLoad = loading.ready() return result } catch (error) { console.error(error) this.paywallLoad = loading.error() throw error } } private async handleCheckoutPremiumClick() { await this.loadingOp(async() => this.share.checkoutPremium()) } private async handleUpdatePremiumClick() { await this.loadingOp(async () => this.share.updatePremium()) } private async handleCancelPremiumClick() { await this.loadingOp(async () => this.share.cancelPremium()) } }