Skip to content

Commit

Permalink
clients/web: tweak loading status of checkout submit button
Browse files Browse the repository at this point in the history
  • Loading branch information
frankie567 committed Jan 24, 2025
1 parent 5852b53 commit 83be1e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions clients/apps/web/src/components/Checkout/Checkout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { CheckoutPublicConfirmed } from '@polar-sh/sdk/models/components/ch
import type { Stripe, StripeElements } from '@stripe/stripe-js'
import { useTheme } from 'next-themes'
import { useRouter } from 'next/navigation'
import { useCallback, useState } from 'react'
import { useCallback, useMemo, useState } from 'react'

export interface CheckoutProps {
embed?: boolean
Expand All @@ -38,25 +38,32 @@ const Checkout = ({ embed: _embed, theme: _theme }: CheckoutProps) => {
const theme = _theme || (resolvedTheme as 'light' | 'dark')

const router = useRouter()
const [fullLoading, setFullLoading] = useState(false)
const loading = useMemo(
() => confirmLoading || fullLoading,
[confirmLoading, fullLoading],
)
const [listenFulfillment, fullfillmentLabel] = useCheckoutFulfillmentListener(
client,
checkout,
)
const [fullfillmentLoading, setFullfillmentLoading] = useState(false)

const loading = fullfillmentLoading || confirmLoading
const label = fullfillmentLabel || loadingLabel
const label = useMemo(
() => fullfillmentLabel || loadingLabel,
[fullfillmentLabel, loadingLabel],
)

const confirm = useCallback(
async (
data: CheckoutConfirmStripe,
stripe: Stripe | null,
elements: StripeElements | null,
) => {
setFullLoading(true)
let confirmedCheckout: CheckoutPublicConfirmed
try {
confirmedCheckout = await _confirm(data, stripe, elements)
} catch (error) {
setFullLoading(false)
throw error
}

Expand All @@ -83,9 +90,7 @@ const Checkout = ({ embed: _embed, theme: _theme }: CheckoutProps) => {
// It ensures the user will have an up-to-date status when they are redirected,
// especially if the external URL doesn't implement proper webhook handling
if (!isInternalURL) {
setFullfillmentLoading(true)
await listenFulfillment()
setFullfillmentLoading(false)
}

if (checkout.embedOrigin) {
Expand All @@ -100,7 +105,7 @@ const Checkout = ({ embed: _embed, theme: _theme }: CheckoutProps) => {
}

if (isInternalURL || !embed) {
router.push(parsedURL.toString())
await router.push(parsedURL.toString())
}

return confirmedCheckout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ export const CheckoutFormProvider = ({
confirmationTokenId: confirmationToken.id,
})
} catch (e) {
throw e
} finally {
setLoading(false)
throw e
}

setLoadingLabel('Payment successful! Getting your products ready')
Expand All @@ -221,6 +220,7 @@ export const CheckoutFormProvider = ({
}
}

setLoading(false)
return updatedCheckout
},
[checkout, setError, _confirm],
Expand Down

0 comments on commit 83be1e9

Please sign in to comment.