Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refactor DemoOptions component #1623

Merged
merged 8 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 74 additions & 62 deletions playground/nextjs-app-router/components/DemoOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,74 +11,86 @@ import { SwapConfig } from './form/swap-config';
import { TransactionOptions } from './form/transaction-options';
import { WalletType } from './form/wallet-type';

const COMMON_OPTIONS = [
ActiveComponent,
ComponentMode,
ComponentTheme,
WalletType,
];

const COMPONENT_CONFIG: Partial<
Record<OnchainKitComponent, (() => React.JSX.Element)[]>
> = {
[OnchainKitComponent.Checkout]: [
Chain,
PaymasterUrl,
IsSponsored,
CheckoutOptions,
],
[OnchainKitComponent.Swap]: [Chain, PaymasterUrl, IsSponsored, SwapConfig],
[OnchainKitComponent.SwapDefault]: [
Chain,
PaymasterUrl,
IsSponsored,
SwapConfig,
],
[OnchainKitComponent.Transaction]: [
Chain,
PaymasterUrl,
IsSponsored,
TransactionOptions,
],
[OnchainKitComponent.TransactionDefault]: [
Chain,
PaymasterUrl,
IsSponsored,
TransactionOptions,
],
[OnchainKitComponent.NFTCard]: [Chain, NFTOptions],
[OnchainKitComponent.NFTCardDefault]: [Chain, NFTOptions],
[OnchainKitComponent.NFTMintCard]: [
Chain,
PaymasterUrl,
IsSponsored,
NFTOptions,
],
[OnchainKitComponent.NFTMintCardDefault]: [
Chain,
PaymasterUrl,
IsSponsored,
NFTOptions,
],
};

export function getComponentQueryParams(
component: OnchainKitComponent,
): string {
const options = COMPONENT_CONFIG[component] || [];
const paramKeys = [...COMMON_OPTIONS, ...options].map((Component) =>
Component.name.toLowerCase(),
);
return paramKeys.join('&');
}

export default function DemoOptions({
component,
}: {
component?: OnchainKitComponent;
}) {
const commonOptions = (
const commonElements = COMMON_OPTIONS.map((Component) => (
<Component key={Component.name} />
));

const specificElements = component
? (COMPONENT_CONFIG[component] || []).map((Component) => (
<Component key={Component.name} />
))
: [];

return (
<>
<ActiveComponent />
<ComponentMode />
<ComponentTheme />
<WalletType />
{commonElements}
{specificElements}
</>
);

switch (component) {
case OnchainKitComponent.Checkout:
return (
<>
{commonOptions}
<Chain />
<PaymasterUrl />
<IsSponsored />
<CheckoutOptions />
</>
);
case OnchainKitComponent.Swap:
case OnchainKitComponent.SwapDefault:
return (
<>
{commonOptions}
<Chain />
<PaymasterUrl />
<IsSponsored />
<SwapConfig />
</>
);
case OnchainKitComponent.Transaction:
case OnchainKitComponent.TransactionDefault:
return (
<>
{commonOptions}
<Chain />
<PaymasterUrl />
<IsSponsored />
<TransactionOptions />
</>
);
case OnchainKitComponent.NFTCard:
case OnchainKitComponent.NFTCardDefault:
return (
<>
{commonOptions}
<Chain />
<NFTOptions />
</>
);
case OnchainKitComponent.NFTMintCard:
case OnchainKitComponent.NFTMintCardDefault:
return (
<>
{commonOptions}
<Chain />
<PaymasterUrl />
<IsSponsored />
<NFTOptions />
</>
);
default:
return commonOptions;
}
}
142 changes: 71 additions & 71 deletions playground/nextjs-app-router/components/form/checkout-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,78 +23,78 @@ export function CheckoutOptions() {
chargeId: '',
});

return (
activeComponent === OnchainKitComponent.Checkout && (
<div className="grid gap-2">
<Label htmlFor="chain">Checkout Types</Label>
<RadioGroup
id="pay-type"
value={checkoutTypes}
className="flex items-center justify-between"
onValueChange={(value) => setCheckoutTypes?.(value as CheckoutTypes)}
>
<div className="flex items-center gap-2">
<Label
htmlFor="pay-type-product-id"
className="flex cursor-pointer items-center gap-2 rounded-md border p-2 [&:has(:checked)]:bg-muted"
>
<RadioGroupItem
id="pay-type-product-id"
value={CheckoutTypes.ProductID}
/>
Product ID
</Label>
<Label
htmlFor="pay-type-charge-id"
className="flex cursor-pointer items-center gap-2 rounded-md border p-2 [&:has(:checked)]:bg-muted"
>
<RadioGroupItem
id="pay-type-charge-id"
value={CheckoutTypes.ChargeID}
/>
Charge ID
</Label>
</div>
</RadioGroup>

{checkoutTypes === CheckoutTypes.ChargeID && (
<div>
<Label htmlFor="charge-id">Charge ID</Label>
<Input
id="charge-id"
value={productOptions.chargeId}
placeholder="Enter Charge ID"
onChange={(e) => {
setProductOptions({
...productOptions,
chargeId: e.target.value,
});
setCheckoutOptions?.({
...checkoutOptions,
chargeId: e.target.value,
});
}}
return activeComponent === OnchainKitComponent.Checkout ? (
dschlabach marked this conversation as resolved.
Show resolved Hide resolved
<div className="grid gap-2">
<Label htmlFor="chain">Checkout Types</Label>
<RadioGroup
id="pay-type"
value={checkoutTypes}
className="flex items-center justify-between"
onValueChange={(value) => setCheckoutTypes?.(value as CheckoutTypes)}
>
<div className="flex items-center gap-2">
<Label
htmlFor="pay-type-product-id"
className="flex cursor-pointer items-center gap-2 rounded-md border p-2 [&:has(:checked)]:bg-muted"
>
<RadioGroupItem
id="pay-type-product-id"
value={CheckoutTypes.ProductID}
/>
</div>
)}
{checkoutTypes === CheckoutTypes.ProductID && (
<div>
<Label htmlFor="product-id">Product ID</Label>
<Input
id="product-id"
placeholder="Enter Product ID"
value={productId}
onChange={(e) => {
setProductId(e.target.value);
setCheckoutOptions?.({
...checkoutOptions,
productId: e.target.value,
});
}}
Product ID
</Label>
<Label
htmlFor="pay-type-charge-id"
className="flex cursor-pointer items-center gap-2 rounded-md border p-2 [&:has(:checked)]:bg-muted"
>
<RadioGroupItem
id="pay-type-charge-id"
value={CheckoutTypes.ChargeID}
/>
</div>
)}
</div>
)
Charge ID
</Label>
</div>
</RadioGroup>

{checkoutTypes === CheckoutTypes.ChargeID && (
<div>
<Label htmlFor="charge-id">Charge ID</Label>
<Input
id="charge-id"
value={productOptions.chargeId}
placeholder="Enter Charge ID"
onChange={(e) => {
setProductOptions({
...productOptions,
chargeId: e.target.value,
});
setCheckoutOptions?.({
...checkoutOptions,
chargeId: e.target.value,
});
}}
/>
</div>
)}
{checkoutTypes === CheckoutTypes.ProductID && (
<div>
<Label htmlFor="product-id">Product ID</Label>
<Input
id="product-id"
placeholder="Enter Product ID"
value={productId}
onChange={(e) => {
setProductId(e.target.value);
setCheckoutOptions?.({
...checkoutOptions,
productId: e.target.value,
});
}}
/>
</div>
)}
</div>
) : (
<></>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,38 @@ export function TransactionOptions() {
const { activeComponent, transactionType, setTransactionType } =
useContext(AppContext);

return (
(activeComponent === OnchainKitComponent.Transaction ||
activeComponent === OnchainKitComponent.TransactionDefault) && (
<div className="grid gap-2">
<Label htmlFor="chain">Transaction Options</Label>
<Select
value={transactionType}
onValueChange={(value) =>
value ? setTransactionType?.(value as TransactionTypes) : value
}
>
<SelectTrigger>
<SelectValue placeholder="Select transaction type" />
</SelectTrigger>
<SelectContent>
<SelectItem value={TransactionTypes.Calls}>Calls</SelectItem>
<SelectItem value={TransactionTypes.Contracts}>
Contracts
</SelectItem>
<SelectItem value={TransactionTypes.CallsPromise}>
Calls Promise
</SelectItem>
<SelectItem value={TransactionTypes.ContractsPromise}>
Contracts Promise
</SelectItem>
<SelectItem value={TransactionTypes.CallsCallback}>
Calls Callback
</SelectItem>
<SelectItem value={TransactionTypes.ContractsCallback}>
Contracts Callback
</SelectItem>
</SelectContent>
</Select>
</div>
)
return activeComponent === OnchainKitComponent.Transaction ||
dschlabach marked this conversation as resolved.
Show resolved Hide resolved
activeComponent === OnchainKitComponent.TransactionDefault ? (
<div className="grid gap-2">
<Label htmlFor="chain">Transaction Options</Label>
<Select
value={transactionType}
onValueChange={(value) =>
value ? setTransactionType?.(value as TransactionTypes) : value
}
>
<SelectTrigger>
<SelectValue placeholder="Select transaction type" />
</SelectTrigger>
<SelectContent>
<SelectItem value={TransactionTypes.Calls}>Calls</SelectItem>
<SelectItem value={TransactionTypes.Contracts}>Contracts</SelectItem>
<SelectItem value={TransactionTypes.CallsPromise}>
Calls Promise
</SelectItem>
<SelectItem value={TransactionTypes.ContractsPromise}>
Contracts Promise
</SelectItem>
<SelectItem value={TransactionTypes.CallsCallback}>
Calls Callback
</SelectItem>
<SelectItem value={TransactionTypes.ContractsCallback}>
Contracts Callback
</SelectItem>
</SelectContent>
</Select>
</div>
) : (
<></>
);
}