-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
Checkout
component to Playground (#1428)
- Loading branch information
Showing
8 changed files
with
309 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { ComponentMode } from '@/components/form/component-mode'; | ||
import { ComponentTheme } from '@/components/form/component-theme'; | ||
import { PaymasterUrl } from '@/components/form/paymaster'; | ||
import { OnchainKitComponent } from './AppProvider'; | ||
import { ActiveComponent } from './form/active-component'; | ||
import { Chain } from './form/chain'; | ||
import { CheckoutOptions } from './form/checkout-options'; | ||
import { SwapConfig } from './form/swap-config'; | ||
import { TransactionOptions } from './form/transaction-options'; | ||
import { WalletType } from './form/wallet-type'; | ||
|
||
export default function DemoOptions({ | ||
component, | ||
}: { | ||
component?: OnchainKitComponent; | ||
}) { | ||
const commonOptions = ( | ||
<> | ||
<ComponentMode /> | ||
<ComponentTheme /> | ||
<ActiveComponent /> | ||
<WalletType /> | ||
</> | ||
); | ||
|
||
switch (component) { | ||
case OnchainKitComponent.Checkout: | ||
return ( | ||
<> | ||
{commonOptions} | ||
<Chain /> | ||
<PaymasterUrl /> | ||
<CheckoutOptions /> | ||
</> | ||
); | ||
case OnchainKitComponent.Swap || OnchainKitComponent.SwapDefault: | ||
return ( | ||
<> | ||
{commonOptions} | ||
<Chain /> | ||
<PaymasterUrl /> | ||
<SwapConfig /> | ||
</> | ||
); | ||
case OnchainKitComponent.Transaction || | ||
OnchainKitComponent.TransactionDefault: | ||
return ( | ||
<> | ||
{commonOptions} | ||
<Chain /> | ||
<PaymasterUrl /> | ||
<TransactionOptions /> | ||
</> | ||
); | ||
default: | ||
return commonOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { useCapabilities } from '@/lib/hooks'; | ||
import { | ||
Checkout, | ||
CheckoutButton, | ||
CheckoutStatus, | ||
} from '@coinbase/onchainkit/checkout'; | ||
import type { LifecycleStatus } from '@coinbase/onchainkit/checkout'; | ||
import { useCallback, useMemo } from 'react'; | ||
import { useContext } from 'react'; | ||
import { AppContext, CheckoutTypes } from '../AppProvider'; | ||
|
||
export default function CheckoutDemo() { | ||
const { checkoutTypes, checkoutOptions } = useContext(AppContext); | ||
const capabilities = useCapabilities(); | ||
|
||
const chargeIDKey = useMemo(() => { | ||
return `${checkoutOptions?.chargeId}`; | ||
}, [checkoutOptions]); | ||
|
||
const productIDKey = useMemo(() => { | ||
return `${checkoutOptions?.productId}`; | ||
}, [checkoutOptions]); | ||
|
||
const handleOnStatus = useCallback((status: LifecycleStatus) => { | ||
console.log('Playground.Checkout.onStatus:', status); | ||
}, []); | ||
|
||
const createCharge = useCallback(async () => { | ||
return Promise.resolve(checkoutOptions?.chargeId || ''); | ||
}, [checkoutOptions]); | ||
|
||
const chargeIDDisabled = !checkoutOptions?.chargeId; | ||
const productIDDisabled = !checkoutOptions?.productId; | ||
|
||
return ( | ||
<div className="mx-auto grid w-1/2 gap-8"> | ||
{checkoutTypes === CheckoutTypes.ProductID && ( | ||
<> | ||
{checkoutOptions?.productId ? ( | ||
<Checkout | ||
key={productIDKey} | ||
productId={checkoutOptions?.productId} | ||
onStatus={handleOnStatus} | ||
isSponsored={capabilities?.paymasterService?.url != null} | ||
> | ||
<CheckoutButton | ||
coinbaseBranded={true} | ||
disabled={productIDDisabled} | ||
/> | ||
<CheckoutStatus /> | ||
</Checkout> | ||
) : ( | ||
<> | ||
<div className="relative flex h-full w-full flex-col items-center"> | ||
<div className="absolute top-0 left-0 z-10 flex h-full w-full flex-col justify-center rounded-xl bg-[#000000] bg-opacity-50 text-center"> | ||
<div className="mx-auto w-2/3 rounded-md bg-muted p-6 text-sm"> | ||
Enter a Product ID to continue | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
)} | ||
</> | ||
)} | ||
{checkoutTypes === CheckoutTypes.ChargeID && ( | ||
<> | ||
{checkoutOptions?.chargeId ? ( | ||
<Checkout | ||
key={chargeIDKey} | ||
chargeHandler={createCharge} | ||
onStatus={handleOnStatus} | ||
isSponsored={capabilities?.paymasterService?.url != null} | ||
> | ||
<CheckoutButton | ||
coinbaseBranded={true} | ||
disabled={chargeIDDisabled} | ||
/> | ||
<CheckoutStatus /> | ||
</Checkout> | ||
) : ( | ||
<> | ||
<div className="relative flex h-full w-full flex-col items-center"> | ||
<div className="absolute top-0 left-0 z-10 flex h-full w-full flex-col justify-center rounded-xl bg-[#000000] bg-opacity-50 text-center"> | ||
<div className="mx-auto w-2/3 rounded-md bg-muted p-6 text-sm"> | ||
Enter a Charge ID to continue | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
)} | ||
</> | ||
)} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.