Modify layout and improve subscription fetching

The layout for responsive designs has been updated in plan-picker component. Subscription type has been removed from billing/page.tsx and subscription fetching has been improved with additional query parameters in both billing/page.tsx and current-plan-card.tsx. Errors were also corrected in i18n translation keys in team-account-checkout-form.tsx. CurrentPlanCard's component props now includes line items in the subscription object. Changes in billing/page.tsx include adaptation of container width to be fully responsive.
This commit is contained in:
giancarlo
2024-03-31 15:24:31 +08:00
parent aa12ecd5a2
commit 57c8f89804
5 changed files with 36 additions and 13 deletions

View File

@@ -18,8 +18,6 @@ import { withI18n } from '~/lib/i18n/with-i18n';
import { PersonalAccountCheckoutForm } from './_components/personal-account-checkout-form'; import { PersonalAccountCheckoutForm } from './_components/personal-account-checkout-form';
type Subscription = Database['public']['Tables']['subscriptions']['Row'];
export const generateMetadata = async () => { export const generateMetadata = async () => {
const i18n = await createI18nServerInstance(); const i18n = await createI18nServerInstance();
const title = i18n.t('account:billingTab'); const title = i18n.t('account:billingTab');
@@ -78,7 +76,7 @@ async function loadData(client: SupabaseClient<Database>) {
const subscription = client const subscription = client
.from('subscriptions') .from('subscriptions')
.select<string, Subscription>('*') .select('*, items: subscription_items !inner (*)')
.eq('account_id', user.id) .eq('account_id', user.id)
.maybeSingle() .maybeSingle()
.then(({ data }) => data); .then(({ data }) => data);

View File

@@ -38,11 +38,11 @@ export function TeamAccountCheckoutForm(params: { accountId: string }) {
<Card> <Card>
<CardHeader> <CardHeader>
<CardTitle> <CardTitle>
<Trans i18nKey={'billing.manageTeamPlan'} /> <Trans i18nKey={'billing:manageTeamPlan'} />
</CardTitle> </CardTitle>
<CardDescription> <CardDescription>
<Trans i18nKey={'billing.manageTeamPlanDescription'} /> <Trans i18nKey={'billing:manageTeamPlanDescription'} />
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>

View File

@@ -35,6 +35,7 @@ async function TeamAccountBillingPage({ params }: Params) {
const workspace = await loadTeamWorkspace(params.account); const workspace = await loadTeamWorkspace(params.account);
const accountId = workspace.account.id; const accountId = workspace.account.id;
const [subscription, customerId] = await loadAccountData(accountId); const [subscription, customerId] = await loadAccountData(accountId);
const canManageBilling = const canManageBilling =
workspace.account.permissions.includes('billing.manage'); workspace.account.permissions.includes('billing.manage');
@@ -46,7 +47,7 @@ async function TeamAccountBillingPage({ params }: Params) {
/> />
<PageBody> <PageBody>
<div className={'mx-auto w-full max-w-2xl'}> <div className={'mx-auto w-full'}>
<If condition={!canManageBilling}> <If condition={!canManageBilling}>
<CannotManageBillingAlert /> <CannotManageBillingAlert />
</If> </If>
@@ -100,7 +101,7 @@ async function loadAccountData(accountId: string) {
const subscription = client const subscription = client
.from('subscriptions') .from('subscriptions')
.select('*') .select('*, items: subscription_items !inner (*)')
.eq('account_id', accountId) .eq('account_id', accountId)
.maybeSingle() .maybeSingle()
.then(({ data }) => data); .then(({ data }) => data);

View File

@@ -27,14 +27,33 @@ import { Trans } from '@kit/ui/trans';
import { CurrentPlanAlert } from './current-plan-alert'; import { CurrentPlanAlert } from './current-plan-alert';
import { CurrentPlanBadge } from './current-plan-badge'; import { CurrentPlanBadge } from './current-plan-badge';
type Subscription = Database['public']['Tables']['subscriptions']['Row'];
type LineItem = Database['public']['Tables']['subscription_items']['Row'];
interface Props {
subscription: Subscription & {
items: LineItem[];
};
config: BillingConfig;
}
export function CurrentPlanCard({ export function CurrentPlanCard({
subscription, subscription,
config, config,
}: React.PropsWithChildren<{ }: React.PropsWithChildren<Props>) {
subscription: Database['public']['Tables']['subscriptions']['Row']; // line items have the same product id
config: BillingConfig; const lineItem = subscription.items[0] as LineItem;
}>) {
const { plan, product } = getProductPlanPair(config, subscription); const product = config.products.find(
(product) => product.id === lineItem.product_id,
);
if (!product) {
throw new Error(
'Product not found. Make sure the product exists in the billing config.',
);
}
return ( return (
<Card> <Card>
@@ -58,6 +77,7 @@ export function CurrentPlanCard({
/> />
<span>{product.name}</span> <span>{product.name}</span>
<CurrentPlanBadge status={subscription.status} /> <CurrentPlanBadge status={subscription.status} />
</div> </div>
</div> </div>

View File

@@ -96,7 +96,11 @@ export function PlanPicker(
return ( return (
<Form {...form}> <Form {...form}>
<div className={'flex space-x-4'}> <div
className={
'flex flex-col space-y-4 lg:flex-row lg:space-x-4 lg:space-y-0'
}
>
<form <form
className={'flex w-full max-w-xl flex-col space-y-4'} className={'flex w-full max-w-xl flex-col space-y-4'}
onSubmit={form.handleSubmit(props.onSubmit)} onSubmit={form.handleSubmit(props.onSubmit)}