Skip to content

Commit 6aa4b51

Browse files
committed
fix buy button trigger only when showing
1 parent fcf7e4b commit 6aa4b51

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

‎components/user-buy.tsx‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Link from "next/link";
1515
import { useCookies } from "react-cookie";
1616
import { jwtDecode } from "jwt-decode";
1717
import { CourseQueryResult, HomePageQueryResult } from "@/sanity.types";
18+
import { Price3 } from "@/lib/stripe.types";
1819

1920
export default function Buy({
2021
title,
@@ -25,10 +26,7 @@ export default function Buy({
2526
}) {
2627
const { currentUser } = useFirestoreUser();
2728
const [redirecting, setRedirecting] = useState(false);
28-
const [price, setPrice] = useState({
29-
product: "",
30-
unit_amount: 0,
31-
});
29+
const [price, setPrice] = useState<Price3 | null>(null);
3230
const [showBuy, setShowBuy] = useState(false);
3331
const { toast } = useToast();
3432
const [cookies] = useCookies(["app.idt"]);
@@ -50,13 +48,15 @@ export default function Buy({
5048

5149
const getPrice = async () => {
5250
const price = await getStripePrice({ stripeProduct });
53-
setPrice(price.data() as any);
51+
const priceData = price.data() as any;
52+
console.log(priceData);
53+
setPrice(priceData);
5454
};
5555

5656
useEffect(() => {
57-
if (stripeProduct) getPrice();
57+
if (stripeProduct && showBuy) getPrice();
5858
// eslint-disable-next-line react-hooks/exhaustive-deps
59-
}, [stripeProduct]);
59+
}, [stripeProduct, showBuy]);
6060

6161
const onSubscribe = async () => {
6262
setRedirecting(true);
@@ -81,8 +81,19 @@ export default function Buy({
8181
const subscribe = (
8282
<>
8383
<DialogHeader>
84-
<DialogTitle>Purchase {title}</DialogTitle>
84+
<DialogTitle className="text-2xl font-bold">
85+
Purchase: {title}
86+
</DialogTitle>
8587
</DialogHeader>
88+
{price && (
89+
<div className="mt-4 flex gap-2 sm:gap-4 text-xl sm:text-2xl">
90+
<div className="flex items-center justify-between">Price:</div>
91+
<div className="flex items-center justify-between gap-2">
92+
<span className="font-bold">{price.unit_amount / 100}</span>
93+
<span className="uppercase"> {price.currency}</span>
94+
</div>
95+
</div>
96+
)}
8697
<Button onClick={onSubscribe} disabled={redirecting}>
8798
{redirecting ? "Redirecting..." : "Continue to Stripe"}
8899
{!redirecting && <ArrowRightIcon className="ml-2" />}

‎lib/stripe.types.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export interface Price3 {
280280
created: number;
281281
custom_unit_amount: any;
282282
type: string;
283-
product: Product2;
283+
product: Product2 | string;
284284
active: boolean;
285285
unit_amount: number;
286286
billing_scheme: string;

0 commit comments

Comments
 (0)