import {html} from "lit" import {makeStoreModel} from "../../../model/model.js" import {select} from "../../../../../../toolbox/selects.js" import {dollarsToCents, centsToDollars} from "./price-utils.js" import {makePlanningComponentSnap} from "./planning-component-snap.js" import {formatDate} from "../../../../../../toolbox/goodtimes/format-date.js" import {renderOp} from "../../../../../../framework/op-rendering/render-op.js" import {XioTextInput} from "../../../../../xio-components/inputs/xio-text-input.js" import {XioPriceInput} from "../../../../../xio-components/inputs/xio-price-input.js" import {SubscriptionPricing, SubscriptionPlan, SubscriptionTier} from "../../../../isomorphic/concepts.js" import {validateNewPlanDraft, validateNewTierDraft, validateEditPlanDraft, validateEditTierDraft, validateLabel, validatePriceNumber} from "../../../../isomorphic/validators.js" import {SubscriptionPricingDraft, SubscriptionPlanDraft, SubscriptionTierDraft, EditPlanDraft, EditTierDraft} from "../../../../backend/services/subscriptions/types/drafts.js" function preparePricing(dollars: string): SubscriptionPricingDraft { return { currency: "usd", interval: "month", price: dollarsToCents(dollars), } } function isPricingChanged(pricing: SubscriptionPricing, draft: SubscriptionPricingDraft) { for (const changed of [ draft.price !== pricing.price, draft.currency !== pricing.currency, draft.interval !== pricing.interval, ]) { if (changed) return true } return false } export function planningUi({storeModel, componentSnap, getShadowRoot}: { storeModel: ReturnType componentSnap: ReturnType getShadowRoot: () => ShadowRoot }) { const states = { store: storeModel.snap.readable, component: componentSnap.state, } const formGathering = { newPlan() { const shadow = getShadowRoot() const elements = { planlabel: select( `.plandraft xio-text-input[data-field="planlabel"]`, shadow, ), tierlabel: select( `.plandraft xio-text-input[data-field="tierlabel"]`, shadow, ), tierprice: select( `.plandraft xio-price-input[data-field="tierprice"]`, shadow, ), } const draft: SubscriptionPlanDraft = { planLabel: elements.planlabel.value, tier: { label: elements.tierlabel.value, pricing: preparePricing(elements.tierprice.value), }, } const problems = validateNewPlanDraft(draft) states.component.draftNewPlan.problems = problems return {draft, problems} }, newTier() { const shadow = getShadowRoot() const elements = { label: select( `.tierdraft xio-text-input[data-field="tierlabel"]`, shadow, ), price: select( `.tierdraft xio-price-input[data-field="tierprice"]`, shadow, ), } const draft: SubscriptionTierDraft = { label: elements.label.value, pricing: preparePricing(elements.price.value), } const problems = validateNewTierDraft(draft) states.component.draftNewTier.problems = problems return {draft, problems} }, editedPlan(plan: SubscriptionPlan) { const shadow = getShadowRoot() const elements = { label: select( `.planediting [data-field="label"]`, shadow, ), archived: select( `.planediting [data-field="archived"]`, shadow, ), } const draft: EditPlanDraft = { planId: plan.planId, label: elements.label.value, archived: elements.archived.checked, } const problems = validateEditPlanDraft(draft) const isChanged = draft.label !== plan.label || draft.archived !== plan.archived states.component.editingPlanDraft.isChanged = isChanged states.component.editingPlanDraft.problems = problems return {draft, problems, isChanged} }, editedTier(tier: SubscriptionTier) { const initialPricing = tier.pricing ? tier.pricing[0] : { stripePriceId: "", currency: "usd" as SubscriptionPricing["currency"], interval: "month" as SubscriptionPricing["interval"], price: dollarsToCents(""), } const shadow = getShadowRoot() const elements = { label: select( `.tierediting [data-field="label"]`, shadow, ), active: select( `.tierediting [data-field="active"]`, shadow, ), price: select( `.tierediting [data-field="price"]`, shadow, ), } const draft: EditTierDraft = { tierId: tier.tierId, label: elements.label.value, active: elements.active.checked, pricing: preparePricing(elements.price.value) } const problems = validateEditTierDraft(draft) const isChanged = draft.label !== tier.label || draft.active !== tier.active || isPricingChanged(initialPricing, draft.pricing) states.component.editingTierDraft.isChanged = isChanged states.component.editingTierDraft.problems = problems return {draft, problems, isChanged} }, } const actions = { newPlan: { toggleDraftPanel() { states.component.draftNewPlan = states.component.draftNewPlan ? undefined : {loading: false, problems: ["cannot be empty"]} }, async submit() { const {draft, problems} = formGathering.newPlan() if (problems.length === 0) { try { states.component.draftNewPlan.loading = true await storeModel.subscriptions.addPlan(draft) } finally { states.component.draftNewPlan = undefined } } }, }, newTier: { toggleDraftPanel(planId: string) { states.component.draftNewTier = states.component.draftNewTier ? undefined : {planId, loading: false, problems: []} }, async submit(planId: string) { const {draft, problems} = formGathering.newTier() if (problems.length === 0) { try { states.component.draftNewTier.loading = true await storeModel.subscriptions.addTier({ ...draft, planId, }) } finally { states.component.draftNewTier = undefined } } }, }, planEditing: { handleEditButtonPress(plan: SubscriptionPlan) { const isEditing = states.component.editingPlanDraft?.planId === plan.planId states.component.editingPlanDraft = isEditing ? undefined : { isChanged: false, loading: false, planId: plan.planId, problems: [], } }, async submit(plan: SubscriptionPlan) { const {draft, problems} = formGathering.editedPlan(plan) if (problems.length === 0) { try { states.component.editingPlanDraft.loading = true await storeModel.subscriptions.editPlan(draft) } finally { states.component.editingPlanDraft = undefined } } }, }, tierEditing: { handleEditButtonPress(plan: SubscriptionPlan, tier: SubscriptionTier) { const isEditing = states.component.editingTierDraft?.tierId === tier.tierId states.component.editingTierDraft = isEditing ? undefined : { isChanged: false, loading: false, planId: plan.planId, tierId: tier.tierId, problems: [], } }, async submit(plan: SubscriptionPlan, tier: SubscriptionTier) { const {draft, problems} = formGathering.editedTier(tier) if (problems.length === 0) { try { states.component.editingTierDraft.loading = true await storeModel.subscriptions.editTier({ planId: plan.planId, tierId: tier.tierId, label: draft.label, active: draft.active, pricing: draft.pricing, }) } finally { states.component.editingTierDraft = undefined } } } }, } const handlers = { planDraft: { valuechange: formGathering.newPlan, }, tierDraft: { valuechange: formGathering.newTier, }, } function renderNewPlanPanel() { const {loading, problems} = states.component.draftNewPlan return html`
plan label tier label tier price Submit new plan
` } function renderNewTierPanel(planId: string) { const {loading, problems} = states.component.draftNewTier return html`
tier label tier price actions.newTier.submit(planId)}> submit new tier
` } function renderTier(plan: SubscriptionPlan, tier: SubscriptionTier) { const isEditing = states.component.editingTierDraft?.tierId === tier.tierId const loading = states.component.editingTierDraft?.loading const hasPricing = !!tier.pricing const initialPrice = hasPricing ? centsToDollars(tier.pricing[0].price) : "" return html`
  • actions.tierEditing.handleEditButtonPress(plan, tier)}> edit tier
    ${isEditing ?html`
    formGathering.editedTier(tier)} @change=${() => formGathering.editedTier(tier)}> tier label Price ${states.component.editingTierDraft.isChanged ? html` actions.tierEditing.submit(plan, tier)}> save tier ` : null}
    `: html`

    tier label: ${tier.label}

    ${hasPricing ? html`

    price: $${centsToDollars(tier.pricing[0].price)}

    ` : html`

    price not set

    ` }

    active: ${tier.active ?"true" :"false"}

    `}

    tier id:

    role id:

    created: ${formatDate(tier.time).full}

  • ` } function renderPlan(plan: SubscriptionPlan, index: number) { const isEditing = states.component.editingPlanDraft?.planId === plan.planId const loading = states.component.editingPlanDraft?.loading const activeTiers: SubscriptionTier[] = [] const inactiveTiers: SubscriptionTier[] = [] for (const tier of plan.tiers) { if (tier.active) activeTiers.push(tier) else inactiveTiers.push(tier) } return html`
  • actions.planEditing.handleEditButtonPress(plan)}> edit plan
    ${isEditing ?html`
    formGathering.editedPlan(plan)} @valuechange=${() => formGathering.editedPlan(plan)}> plan label ${states.component.editingPlanDraft.isChanged ? html` actions.planEditing.submit(plan)}> save plan ` : null}
    `: html`

    plan label: ${plan.label}

    archived: ${plan.archived ?"true" :"false"}

    `}

    plan id:

    created: ${formatDate(plan.time).full}

    tiers

    actions.newTier.toggleDraftPanel(plan.planId)}> + add tier ${states.component.draftNewTier?.planId === plan.planId ? renderNewTierPanel(plan.planId) : null}
      ${activeTiers.map(tier => renderTier(plan, tier))}
    ${inactiveTiers.length ? html`
    inactive tiers
      ${inactiveTiers.map(tier => renderTier(plan, tier))}
    ` : null}
  • ` } function renderPlanning() { const {subscriptionPlansOp} = states.store.subscriptions return renderOp(subscriptionPlansOp, plans => { const availablePlans: SubscriptionPlan[] = [] const archivedPlans: SubscriptionPlan[] = [] for (const plan of plans) { if (plan.archived) archivedPlans.push(plan) else availablePlans.push(plan) } return html` + add plan ${states.component.draftNewPlan ? renderNewPlanPanel() : null}
      ${availablePlans.map(renderPlan)}
    ${archivedPlans.length ? html`
    archived plans
      ${archivedPlans.map(renderPlan)}
    ` : null} ` }) } return { renderPlanning, } }