diff --git a/app/routes/payment-choice.tsx b/app/routes/payment-choice.tsx index 058db24..f2d9568 100644 --- a/app/routes/payment-choice.tsx +++ b/app/routes/payment-choice.tsx @@ -11,6 +11,11 @@ import { useDialPadContext } from '~/lib/context/dialpad' import { useEffect } from 'react' import type { WalletAddress } from '@interledger/open-payments' +type LoaderData = { + receiver: string + assetCode?: string | null +} + export async function loader({ request }: LoaderFunctionArgs) { const searchParams = new URL(request.url).searchParams const receiver = searchParams.get('receiver') || '' @@ -21,9 +26,11 @@ export async function loader({ request }: LoaderFunctionArgs) { try { receiverWalletAddress = await getValidWalletAddress(receiver) } catch (error) { - throw new Error( - 'Receiver Wallet Address is not valid. Please check and try again.' - ) + // Prefer redirecting to home instead of throwing a 500. + // You can customize this behavior (flash message, query param, etc.) + return redirect('/', { + headers: { 'Set-Cookie': await destroySession(session) } + }) } } else { return redirect('/', { @@ -31,20 +38,27 @@ export async function loader({ request }: LoaderFunctionArgs) { }) } - return json({ - receiver: receiver, - assetCode: receiverWalletAddress.assetCode - } as const) + return json({ + receiver, + assetCode: receiverWalletAddress.assetCode ?? null + }) } export default function PaymentChoice() { - const data = useLoaderData() + const data = useLoaderData() as LoaderData const { setAssetCode, amountValue } = useDialPadContext() + // only set the asset code when it's available and avoid rerunning every render useEffect(() => { - setAssetCode(data.assetCode) - }) + if (data?.assetCode) { + setAssetCode(data.assetCode) + } + }, [data?.assetCode, setAssetCode]) + + const encodedReceiver = encodeURIComponent(data.receiver) + const encodedAmount = encodeURIComponent(String(amountValue ?? '')) + const hasAmount = amountValue !== undefined && amountValue !== null && String(amountValue).trim() !== '' return ( <> @@ -66,7 +80,7 @@ export default function PaymentChoice() {
@@ -77,18 +91,36 @@ export default function PaymentChoice() { Pay with Interledger - - - - - - Pay with card - - + + {/* If no amount is entered, disable the card checkout link */} + {hasAmount ? ( + + + + + + Pay with card + + + ) : ( +
+ + + + + Pay with card + +
+ )}