-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:[CM-708] Checkout Widget Setup (#1993)
- Loading branch information
1 parent
07b182c
commit 66a081d
Showing
14 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/checkout/sdk/src/widgets/definitions/configurations/checkout.ts
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,6 @@ | ||
/* eslint-disable max-len */ | ||
import { WidgetConfiguration } from './widget'; | ||
|
||
export type CheckoutWidgetConfiguration = { | ||
|
||
} & WidgetConfiguration; |
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
3 changes: 3 additions & 0 deletions
3
packages/checkout/sdk/src/widgets/definitions/events/checkout.ts
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,3 @@ | ||
export enum CheckoutEventType { | ||
|
||
} |
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
7 changes: 7 additions & 0 deletions
7
packages/checkout/sdk/src/widgets/definitions/parameters/checkout.ts
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,7 @@ | ||
/* eslint-disable max-len */ | ||
import { WidgetLanguage } from '../configurations'; | ||
|
||
export type CheckoutWidgetParams = { | ||
/** The language to use for the checkout widget */ | ||
language?: WidgetLanguage; | ||
}; |
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
26 changes: 26 additions & 0 deletions
26
packages/checkout/widgets-lib/src/widgets/checkout/CheckoutWidget.tsx
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,26 @@ | ||
import { CheckoutWidgetParams } from '@imtbl/checkout-sdk'; | ||
import { | ||
useContext, | ||
} from 'react'; | ||
import { ConnectLoaderContext } from '../../context/connect-loader-context/ConnectLoaderContext'; | ||
import { StrongCheckoutWidgetsConfig } from '../../lib/withDefaultWidgetConfig'; | ||
|
||
export type CheckoutWidgetInputs = CheckoutWidgetParams & { | ||
config: StrongCheckoutWidgetsConfig, | ||
}; | ||
|
||
export default function CheckoutWidget(props: CheckoutWidgetInputs) { | ||
const { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
config, | ||
} = props; | ||
|
||
const { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
connectLoaderState: { checkout, provider }, | ||
} = useContext(ConnectLoaderContext); | ||
|
||
return ( | ||
<div /> | ||
); | ||
} |
87 changes: 87 additions & 0 deletions
87
packages/checkout/widgets-lib/src/widgets/checkout/CheckoutWidgetRoot.tsx
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,87 @@ | ||
import { | ||
ChainId, | ||
IMTBLWidgetEvents, | ||
CheckoutWidgetConfiguration, | ||
CheckoutWidgetParams, | ||
WidgetProperties, | ||
WidgetTheme, | ||
WidgetType, | ||
} from '@imtbl/checkout-sdk'; | ||
import React, { Suspense } from 'react'; | ||
import { ConnectLoader, ConnectLoaderParams } from '../../components/ConnectLoader/ConnectLoader'; | ||
import { ThemeProvider } from '../../components/ThemeProvider/ThemeProvider'; | ||
import { CustomAnalyticsProvider } from '../../context/analytics-provider/CustomAnalyticsProvider'; | ||
import { HandoverProvider } from '../../context/handover-context/HandoverProvider'; | ||
import i18n from '../../i18n'; | ||
import { getL1ChainId, getL2ChainId } from '../../lib'; | ||
import { LoadingView } from '../../views/loading/LoadingView'; | ||
import { Base } from '../BaseWidgetRoot'; | ||
|
||
const CheckoutWidget = React.lazy(() => import('./CheckoutWidget')); | ||
|
||
export class CheckoutWidgetRoot extends Base<WidgetType.CHECKOUT> { | ||
protected eventTopic: IMTBLWidgetEvents = IMTBLWidgetEvents.IMTBL_CHECKOUT_WIDGET_EVENT; | ||
|
||
protected getValidatedProperties( | ||
{ config }: WidgetProperties<WidgetType.CHECKOUT>, | ||
): WidgetProperties<WidgetType.CHECKOUT> { | ||
let validatedConfig: CheckoutWidgetConfiguration | undefined; | ||
|
||
if (config) { | ||
validatedConfig = config; | ||
if (config.theme === WidgetTheme.LIGHT) validatedConfig.theme = WidgetTheme.LIGHT; | ||
else validatedConfig.theme = WidgetTheme.DARK; | ||
} | ||
|
||
return { | ||
config: validatedConfig, | ||
}; | ||
} | ||
|
||
protected getValidatedParameters(params: CheckoutWidgetParams): CheckoutWidgetParams { | ||
const validatedParams = params; | ||
|
||
return validatedParams; | ||
} | ||
|
||
protected render() { | ||
if (!this.reactRoot) return; | ||
|
||
const { t } = i18n; | ||
const connectLoaderParams: ConnectLoaderParams = { | ||
targetChainId: this.checkout.config.isProduction | ||
? ChainId.IMTBL_ZKEVM_MAINNET | ||
: ChainId.IMTBL_ZKEVM_TESTNET, | ||
// walletProviderName: this.parameters?.walletProviderName, | ||
web3Provider: this.web3Provider, | ||
checkout: this.checkout, | ||
allowedChains: [getL1ChainId(this.checkout.config), getL2ChainId(this.checkout.config)], | ||
}; | ||
|
||
this.reactRoot.render( | ||
<React.StrictMode> | ||
<CustomAnalyticsProvider checkout={this.checkout}> | ||
<ThemeProvider id="checkout-container" config={this.strongConfig()}> | ||
<HandoverProvider> | ||
<ConnectLoader | ||
widgetConfig={this.strongConfig()} | ||
params={connectLoaderParams} | ||
closeEvent={() => { }} | ||
> | ||
<Suspense | ||
fallback={ | ||
<LoadingView loadingText={t('views.LOADING_VIEW.text')} /> | ||
} | ||
> | ||
<CheckoutWidget | ||
config={this.strongConfig()} | ||
/> | ||
</Suspense> | ||
</ConnectLoader> | ||
</HandoverProvider> | ||
</ThemeProvider> | ||
</CustomAnalyticsProvider> | ||
</React.StrictMode>, | ||
); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
packages/checkout/widgets-sample-app/src/components/ui/checkout/checkout.tsx
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,32 @@ | ||
import { Checkout, WidgetTheme, WidgetType } from '@imtbl/checkout-sdk'; | ||
import { WidgetsFactory } from '@imtbl/checkout-widgets'; | ||
import { useEffect, useMemo } from 'react'; | ||
import { Environment } from '@imtbl/config'; | ||
|
||
function CheckoutUI() { | ||
const checkout = useMemo(() => new Checkout({ baseConfig: { environment: Environment.SANDBOX } }), []); | ||
const factory = useMemo(() => new WidgetsFactory(checkout, { theme: WidgetTheme.DARK }), [checkout]); | ||
const checkoutWidget = useMemo(() => factory.create(WidgetType.CHECKOUT), [checkout]) | ||
|
||
const unmount = () => { checkoutWidget.unmount() } | ||
const mount = () => { checkoutWidget.mount('checkout') } | ||
const update = (theme: WidgetTheme) => { checkoutWidget.update({ config: { theme } }) } | ||
|
||
useEffect(() => { | ||
mount(); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1 className="sample-heading">Checkout Widget</h1> | ||
<div id="checkout"></div> | ||
<button onClick={unmount}>Unmount</button> | ||
<button onClick={mount}>Mount</button> | ||
<button onClick={() => update(WidgetTheme.LIGHT)}>Light theme</button> | ||
<button onClick={() => update(WidgetTheme.DARK)}>Dark theme</button> | ||
|
||
</div> | ||
); | ||
} | ||
|
||
export default CheckoutUI; |
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