Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sweet-humans-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Hide CTA for `<PricingTable forOrganization/>` when the user is does not have an active organization selected.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Fix grammar in the changeset entry.

Please drop the extra “is” so the sentence reads naturally.

- Hide CTA for `<PricingTable forOrganization/>` when the user is does not have an active organization selected.
+ Hide CTA for `<PricingTable forOrganization/>` when the user does not have an active organization selected.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Hide CTA for `<PricingTable forOrganization/>` when the user is does not have an active organization selected.
Hide CTA for `<PricingTable forOrganization/>` when the user does not have an active organization selected.
🤖 Prompt for AI Agents
In .changeset/sweet-humans-poke.md around line 5, the sentence currently
contains an extra "is"; remove the extra "is" so the line reads: "Hide CTA for
`<PricingTable forOrganization/>` when the user does not have an active
organization selected."

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useClerk, useSession } from '@clerk/shared/react';
import { useClerk, useOrganization, useSession } from '@clerk/shared/react';
import type { BillingPlanResource, BillingSubscriptionPlanPeriod, PricingTableProps } from '@clerk/types';
import * as React from 'react';

Expand All @@ -24,6 +24,7 @@ import {
import { Check, Plus } from '../../icons';
import { common, InternalThemeProvider } from '../../styledSystem';
import { SubscriptionBadge } from '../Subscriptions/badge';
import { getPricingFooterState } from './utils/pricing-footer-state';

interface PricingTableDefaultProps {
plans?: BillingPlanResource[] | null;
Expand Down Expand Up @@ -103,6 +104,7 @@ function Card(props: CardProps) {
const { isSignedIn } = useSession();
const { mode = 'mounted', ctaPosition: ctxCtaPosition } = usePricingTableContext();
const subscriberType = useSubscriberTypeContext();
const { organization } = useOrganization();

const ctaPosition = pricingTableProps.ctaPosition || ctxCtaPosition || 'bottom';
const collapseFeatures = pricingTableProps.collapseFeatures || false;
Expand All @@ -129,35 +131,14 @@ function Card(props: CardProps) {
);

const hasFeatures = plan.features.length > 0;
const showStatusRow = !!subscription;

let shouldShowFooter = false;
let shouldShowFooterNotice = false;

if (!subscription) {
shouldShowFooter = true;
shouldShowFooterNotice = false;
} else if (subscription.status === 'upcoming') {
shouldShowFooter = true;
shouldShowFooterNotice = true;
} else if (subscription.status === 'active') {
if (subscription.canceledAt) {
shouldShowFooter = true;
shouldShowFooterNotice = false;
} else if (planPeriod !== subscription.planPeriod && plan.annualMonthlyFee.amount > 0) {
shouldShowFooter = true;
shouldShowFooterNotice = false;
} else if (plan.freeTrialEnabled && subscription.isFreeTrial) {
shouldShowFooter = true;
shouldShowFooterNotice = true;
} else {
shouldShowFooter = false;
shouldShowFooterNotice = false;
}
} else {
shouldShowFooter = false;
shouldShowFooterNotice = false;
}
const { shouldShowFooter, shouldShowFooterNotice } = getPricingFooterState({
subscription,
plan,
planPeriod,
forOrganizations: pricingTableProps.forOrganizations,
hasActiveOrganization: !!organization,
});

return (
<Box
Expand Down Expand Up @@ -185,7 +166,7 @@ function Card(props: CardProps) {
planPeriod={planPeriod}
setPlanPeriod={setPlanPeriod}
badge={
showStatusRow ? (
subscription ? (
<SubscriptionBadge subscription={subscription.isFreeTrial ? { status: 'free_trial' } : subscription} />
) : undefined
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import type { BillingPlanResource, BillingSubscriptionItemResource, BillingSubscriptionPlanPeriod } from '@clerk/types';
import { describe, expect, it } from 'vitest';

import { getPricingFooterState } from './pricing-footer-state';

const basePlan: BillingPlanResource = {
id: 'plan_1',
name: 'Pro',
fee: { amount: 1000, amountFormatted: '10.00', currency: 'USD', currencySymbol: '$' },
annualFee: { amount: 10000, amountFormatted: '100.00', currency: 'USD', currencySymbol: '$' },
annualMonthlyFee: { amount: 833, amountFormatted: '8.33', currency: 'USD', currencySymbol: '$' },
description: 'desc',
isDefault: false,
isRecurring: true,
hasBaseFee: true,
forPayerType: 'user',
publiclyVisible: true,
slug: 'pro',
avatarUrl: '',
features: [],
freeTrialDays: 14,
freeTrialEnabled: true,
pathRoot: '',
reload: async () => undefined as any,
};

const makeSub = (overrides: Partial<BillingSubscriptionItemResource>): BillingSubscriptionItemResource => ({
id: 'si_1',
plan: basePlan,
planPeriod: 'month',
status: 'active',
createdAt: new Date('2021-01-01'),
paymentSourceId: 'src_1',
pastDueAt: null,
periodStart: new Date('2021-01-01'),
periodEnd: new Date('2021-01-31'),
canceledAt: null,
isFreeTrial: false,
cancel: async () => undefined as any,
pathRoot: '',
reload: async () => undefined as any,
...overrides,
});

const run = (args: {
subscription?: BillingSubscriptionItemResource;
plan?: BillingPlanResource;
planPeriod?: BillingSubscriptionPlanPeriod;
forOrganizations?: boolean;
hasActiveOrganization?: boolean;
}) =>
getPricingFooterState({
subscription: args.subscription,
plan: args.plan ?? basePlan,
planPeriod: args.planPeriod ?? 'month',
forOrganizations: args.forOrganizations,
hasActiveOrganization: args.hasActiveOrganization ?? false,
});

describe('usePricingFooterState', () => {
it('hides footer when org plans and no active org', () => {
const res = run({ subscription: undefined, forOrganizations: true, hasActiveOrganization: false });
expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false });
});

it('shows footer when no subscription and user plans', () => {
const res = run({ subscription: undefined, forOrganizations: false });
expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false });
});

it('shows notice when subscription is upcoming', () => {
const res = run({ subscription: makeSub({ status: 'upcoming' }) });
expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: true });
});

it('shows footer when active but canceled', () => {
const res = run({ subscription: makeSub({ status: 'active', canceledAt: new Date('2021-02-01') }) });
expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false });
});

it('shows footer when switching period to paid annual', () => {
const res = run({
subscription: makeSub({ status: 'active', planPeriod: 'month' }),
planPeriod: 'annual',
plan: basePlan,
});
expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false });
});

it('shows notice when active free trial', () => {
const res = run({ subscription: makeSub({ status: 'active', isFreeTrial: true }) });
expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: true });
});

it('hides footer when active and matching period without trial', () => {
const res = run({ subscription: makeSub({ status: 'active', planPeriod: 'month', isFreeTrial: false }) });
expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false });
});

it('shows footer when switching period to paid monthly', () => {
const res = run({
subscription: makeSub({ status: 'active', planPeriod: 'annual' }),
planPeriod: 'month',
plan: basePlan,
});
expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false });
});

it('does not show footer when switching period if annualMonthlyFee is 0', () => {
const freeAnnualPlan: BillingPlanResource = {
...basePlan,
annualMonthlyFee: { ...basePlan.annualMonthlyFee, amount: 0, amountFormatted: '0.00' },
};
const res = run({
subscription: makeSub({ status: 'active', planPeriod: 'month' }),
planPeriod: 'annual',
plan: freeAnnualPlan,
});
expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false });
});

it('hides footer when subscription is past_due', () => {
const res = run({ subscription: makeSub({ status: 'past_due' }) });
expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { BillingPlanResource, BillingSubscriptionItemResource, BillingSubscriptionPlanPeriod } from '@clerk/types';

type UsePricingFooterStateParams = {
subscription: BillingSubscriptionItemResource | undefined;
plan: BillingPlanResource;
planPeriod: BillingSubscriptionPlanPeriod;
forOrganizations?: boolean;
hasActiveOrganization: boolean;
};

/**
* Calculates the correct show/hide state for the footer of a card in the `<PricingTableDefault/>` component.
* @returns [shouldShowFooter, shouldShowFooterNotice]
*/
const valueResolution = (params: UsePricingFooterStateParams): [boolean, boolean] => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙃 Can we document the return type as JSDoc? So it's easier to read the tuples down in the code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any specific reason to return [bool, bool] instead of an object? It's probably a pattern but asking as I'm not familliar with the SDK codebase

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saving on bytes. No other reason.

const { subscription, plan, planPeriod, forOrganizations, hasActiveOrganization } = params;
const show_with_notice: [boolean, boolean] = [true, true];
const show_without_notice: [boolean, boolean] = [true, false];
const hide: [boolean, boolean] = [false, false];

// No subscription
if (!subscription) {
if (forOrganizations && !hasActiveOrganization) {
return hide;
}
return show_without_notice;
}

// Upcoming subscription
if (subscription.status === 'upcoming') {
return show_with_notice;
}

// Active subscription
if (subscription.status === 'active') {
const isCanceled = !!subscription.canceledAt;
const isSwitchingPaidPeriod = planPeriod !== subscription.planPeriod && plan.annualMonthlyFee.amount > 0;
const isActiveFreeTrial = plan.freeTrialEnabled && subscription.isFreeTrial;

if (isCanceled || isSwitchingPaidPeriod) {
return show_without_notice;
}

if (isActiveFreeTrial) {
return show_with_notice;
}

return hide;
}
return hide;
};

export const getPricingFooterState = (
params: UsePricingFooterStateParams,
): { shouldShowFooter: boolean; shouldShowFooterNotice: boolean } => {
const [shouldShowFooter, shouldShowFooterNotice] = valueResolution(params);
return { shouldShowFooter, shouldShowFooterNotice };
};
Loading