From e3a5e04d643ee7e9e29e5340f11bfe1bf3eb4fbc Mon Sep 17 00:00:00 2001 From: Kyle Thornton Date: Fri, 13 Sep 2024 16:06:32 -0700 Subject: [PATCH] Adding a conditional that invokes a "get stripe dashboard" function if the user is already subscribed, to disallow a second subscription form being created. --- components/ui/Pricing/Pricing.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/components/ui/Pricing/Pricing.tsx b/components/ui/Pricing/Pricing.tsx index 327e759e1..4588d6e0b 100644 --- a/components/ui/Pricing/Pricing.tsx +++ b/components/ui/Pricing/Pricing.tsx @@ -4,7 +4,7 @@ import Button from '@/components/ui/Button'; import LogoCloud from '@/components/ui/LogoCloud'; import type { Tables } from '@/types_db'; import { getStripe } from '@/utils/stripe/client'; -import { checkoutWithStripe } from '@/utils/stripe/server'; +import { checkoutWithStripe, createStripePortal } from '@/utils/stripe/server'; import { getErrorRedirect } from '@/utils/helpers'; import { User } from '@supabase/supabase-js'; import cn from 'classnames'; @@ -81,6 +81,13 @@ export default function Pricing({ user, products, subscription }: Props) { setPriceIdLoading(undefined); }; + const handleStripePortalRequest = async (price: Price) => { + setPriceIdLoading(price.id); + const redirectUrl = await createStripePortal(currentPath); + setPriceIdLoading(undefined); + return router.push(redirectUrl); + }; + if (!products.length) { return (
@@ -186,7 +193,7 @@ export default function Pricing({ user, products, subscription }: Props) { variant="slim" type="button" loading={priceIdLoading === price.id} - onClick={() => handleStripeCheckout(price)} + onClick={subscription ? () => handleStripePortalRequest(price) : () => handleStripeCheckout(price)} className="block w-full py-2 mt-8 text-sm font-semibold text-center text-white rounded-md hover:bg-zinc-900" > {subscription ? 'Manage' : 'Subscribe'}