From fe38ef430afc3308e4e283a4af1ad04fd62ec6e0 Mon Sep 17 00:00:00 2001 From: jackiettran <41605212+jackiettran@users.noreply.github.com> Date: Thu, 1 Jan 2026 18:48:01 -0500 Subject: [PATCH] Fixed a bug with What will you use it for, fixed a bug with the sticky pricing card, text change --- .../src/components/EmbeddedStripeCheckout.tsx | 29 ++++++++++++++----- frontend/src/pages/ItemDetail.tsx | 8 ++--- frontend/src/pages/RentItem.tsx | 17 ++++++----- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/frontend/src/components/EmbeddedStripeCheckout.tsx b/frontend/src/components/EmbeddedStripeCheckout.tsx index 01006d1..0d1c64a 100644 --- a/frontend/src/components/EmbeddedStripeCheckout.tsx +++ b/frontend/src/components/EmbeddedStripeCheckout.tsx @@ -26,6 +26,18 @@ const EmbeddedStripeCheckout: React.FC = ({ const [sessionId, setSessionId] = useState(""); const hasCreatedSession = useRef(false); + // Use refs to avoid recreating handleComplete when props change + const rentalDataRef = useRef(rentalData); + const onSuccessRef = useRef(onSuccess); + const onErrorRef = useRef(onError); + const sessionIdRef = useRef(sessionId); + + // Keep refs up to date + rentalDataRef.current = rentalData; + onSuccessRef.current = onSuccess; + onErrorRef.current = onError; + sessionIdRef.current = sessionId; + const createCheckoutSession = useCallback(async () => { // Prevent multiple session creations if (hasCreatedSession.current) return; @@ -54,16 +66,17 @@ const EmbeddedStripeCheckout: React.FC = ({ createCheckoutSession(); }, [createCheckoutSession]); + // Use useCallback with empty deps - refs provide access to latest values const handleComplete = useCallback(() => { // For embedded checkout, we need to retrieve the session to get payment method (async () => { try { - if (!sessionId) { + if (!sessionIdRef.current) { throw new Error("No session ID available"); } // Get the completed checkout session - const sessionResponse = await stripeAPI.getCheckoutSession(sessionId); + const sessionResponse = await stripeAPI.getCheckoutSession(sessionIdRef.current); const { status: sessionStatus, setup_intent } = sessionResponse.data; if (sessionStatus !== "complete") { @@ -75,8 +88,8 @@ const EmbeddedStripeCheckout: React.FC = ({ } // Extract payment method ID - handle both string ID and object cases - const paymentMethodId = typeof setup_intent.payment_method === 'string' - ? setup_intent.payment_method + const paymentMethodId = typeof setup_intent.payment_method === 'string' + ? setup_intent.payment_method : setup_intent.payment_method.id; if (!paymentMethodId) { @@ -85,17 +98,17 @@ const EmbeddedStripeCheckout: React.FC = ({ // Create the rental with the payment method ID const rentalPayload = { - ...rentalData, + ...rentalDataRef.current, stripePaymentMethodId: paymentMethodId }; await rentalAPI.createRental(rentalPayload); - onSuccess(); + onSuccessRef.current(); } catch (error: any) { - onError(error.response?.data?.error || error.message || "Failed to complete rental request"); + onErrorRef.current(error.response?.data?.error || error.message || "Failed to complete rental request"); } })(); - }, [sessionId, rentalData, onSuccess, onError]); + }, []); if (creating) { return ( diff --git a/frontend/src/pages/ItemDetail.tsx b/frontend/src/pages/ItemDetail.tsx index 55fa552..d23664a 100644 --- a/frontend/src/pages/ItemDetail.tsx +++ b/frontend/src/pages/ItemDetail.tsx @@ -588,7 +588,7 @@ const ItemDetail: React.FC = () => { {/* Mobile Pricing Card - shown inline on mobile */} -
+
{(() => { @@ -1132,9 +1132,9 @@ const ItemDetail: React.FC = () => {