diff --git a/template/app/src/user/AccountPage.tsx b/template/app/src/user/AccountPage.tsx index 8055d52b3..c0617bc24 100644 --- a/template/app/src/user/AccountPage.tsx +++ b/template/app/src/user/AccountPage.tsx @@ -10,6 +10,7 @@ import { } from "../components/ui/card"; import { Separator } from "../components/ui/separator"; import { + PaymentPlanId, SubscriptionStatus, parsePaymentPlanId, prettyPaymentPlanName, @@ -29,12 +30,12 @@ export default function AccountPage({ user }: { user: User }) { {!!user.email && (
-
+
Email address -
-
+
+
{user.email} - +
)} @@ -43,12 +44,12 @@ export default function AccountPage({ user }: { user: User }) {
-
+
Username -
-
+
+
{user.username} - +
@@ -56,28 +57,39 @@ export default function AccountPage({ user }: { user: User }) {
-
+
Your Plan -
- +
-
+
+ Credits +
+
+ {user.credits} credits +
+
+ +
+
+
+ +
+
+
About - -
+
+
I'm a cool customer. - +
@@ -87,129 +99,97 @@ export default function AccountPage({ user }: { user: User }) { ); } -type UserCurrentPaymentPlanProps = { - subscriptionPlan: string | null; - subscriptionStatus: SubscriptionStatus | null; - datePaid: Date | null; - credits: number; -}; - -function UserCurrentPaymentPlan({ +function UserCurrentSubscriptionPlan({ subscriptionPlan, subscriptionStatus, datePaid, - credits, -}: UserCurrentPaymentPlanProps) { - if (subscriptionStatus && subscriptionPlan && datePaid) { - return ( - <> -
- {getUserSubscriptionStatusDescription({ - subscriptionPlan, - subscriptionStatus, - datePaid, - })} -
- {subscriptionStatus !== SubscriptionStatus.Deleted ? ( - - ) : ( - - )} - +}: Pick) { + let subscriptionPlanMessage = "Free Plan"; + if ( + subscriptionPlan !== null && + subscriptionStatus !== null && + datePaid !== null + ) { + subscriptionPlanMessage = formatSubscriptionStatusMessage( + parsePaymentPlanId(subscriptionPlan), + datePaid, + subscriptionStatus as SubscriptionStatus, ); } return ( <> -
- Credits remaining: {credits} -
- +
+ {subscriptionPlanMessage} +
+
+ +
); } -function getUserSubscriptionStatusDescription({ - subscriptionPlan, - subscriptionStatus, - datePaid, -}: { - subscriptionPlan: string; - subscriptionStatus: SubscriptionStatus; - datePaid: Date; -}) { - const planName = prettyPaymentPlanName(parsePaymentPlanId(subscriptionPlan)); - const endOfBillingPeriod = prettyPrintEndOfBillingPeriod(datePaid); - return prettyPrintStatus(planName, subscriptionStatus, endOfBillingPeriod); -} - -function prettyPrintStatus( - planName: string, +function formatSubscriptionStatusMessage( + subscriptionPlan: PaymentPlanId, + datePaid: Date, subscriptionStatus: SubscriptionStatus, - endOfBillingPeriod: string, ): string { + const paymentPlanName = prettyPaymentPlanName(subscriptionPlan); const statusToMessage: Record = { - active: `${planName}`, - past_due: `Payment for your ${planName} plan is past due! Please update your subscription payment information.`, - cancel_at_period_end: `Your ${planName} plan subscription has been canceled, but remains active until the end of the current billing period${endOfBillingPeriod}`, + active: `${paymentPlanName}`, + past_due: `Payment for your ${paymentPlanName} plan is past due! Please update your subscription payment information.`, + cancel_at_period_end: `Your ${paymentPlanName} plan subscription has been canceled, but remains active until the end of the current billing period: ${prettyPrintEndOfBillingPeriod( + datePaid, + )}`, deleted: `Your previous subscription has been canceled and is no longer active.`, }; - if (Object.keys(statusToMessage).includes(subscriptionStatus)) { - return statusToMessage[subscriptionStatus]; - } else { - throw new Error(`Invalid subscriptionStatus: ${subscriptionStatus}`); + + if (!statusToMessage[subscriptionStatus]) { + throw new Error(`Invalid subscription status: ${subscriptionStatus}`); } + + return statusToMessage[subscriptionStatus]; } function prettyPrintEndOfBillingPeriod(date: Date) { const oneMonthFromNow = new Date(date); oneMonthFromNow.setMonth(oneMonthFromNow.getMonth() + 1); - return ": " + oneMonthFromNow.toLocaleDateString(); + return oneMonthFromNow.toLocaleDateString(); } -function BuyMoreButton() { +function CustomerPortalButton() { + const { data: customerPortalUrl, isLoading: isCustomerPortalUrlLoading } = + useQuery(getCustomerPortalUrl); + + if (!customerPortalUrl) { + return null; + } + return ( -
- - Buy More/Upgrade - -
+ + + ); } -function CustomerPortalButton() { - const { - data: customerPortalUrl, - isLoading: isCustomerPortalUrlLoading, - error: customerPortalUrlError, - } = useQuery(getCustomerPortalUrl); - - const handleClick = () => { - if (customerPortalUrlError) { - console.error("Error fetching customer portal url"); - } - - if (customerPortalUrl) { - window.open(customerPortalUrl, "_blank"); - } else { - console.error("Customer portal URL is not available"); - } - }; +function BuyMoreButton({ + subscriptionStatus, +}: Pick) { + if ( + subscriptionStatus === SubscriptionStatus.Active || + subscriptionStatus === SubscriptionStatus.CancelAtPeriodEnd + ) { + return null; + } return ( -
- -
+ + + ); } diff --git a/template/e2e-tests/tests/utils.ts b/template/e2e-tests/tests/utils.ts index d0d62806e..0da808e02 100644 --- a/template/e2e-tests/tests/utils.ts +++ b/template/e2e-tests/tests/utils.ts @@ -123,7 +123,7 @@ export const makeStripePayment = async ({ await page.waitForURL("**/checkout?status=success"); await page.waitForURL("**/account"); if (planId === "credits10") { - await expect(page.getByText("Credits remaining: 13")).toBeVisible(); + await expect(page.getByText("13 credits")).toBeVisible(); } else { await expect(page.getByText(planId)).toBeVisible(); }