Skip to content

Commit

Permalink
Adding a conditional that invokes a "get stripe dashboard" function i…
Browse files Browse the repository at this point in the history
…f the user is already subscribed, to disallow a second subscription form being created.
  • Loading branch information
k-thornton committed Sep 13, 2024
1 parent 1623fb9 commit e3a5e04
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions components/ui/Pricing/Pricing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 (
<section className="bg-black">
Expand Down Expand Up @@ -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'}
Expand Down

1 comment on commit e3a5e04

@atowersc
Copy link

Choose a reason for hiding this comment

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

I think the most logical solution since it is not convenient to remove the stripe subscription and create a new one

Please sign in to comment.