-
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: [NO CHANGELOG][Add Funds Widget] Create Add Funds Widget by sur…
…facing Top Up View (#2089) Co-authored-by: Mimi Tran <[email protected]>
- Loading branch information
1 parent
b0b671e
commit 549a631
Showing
16 changed files
with
401 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
packages/checkout/sdk/src/widgets/definitions/configurations/addFunds.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,8 @@ | ||
import { WidgetConfiguration } from './widget'; | ||
|
||
/** | ||
* Add Funds Widget Configuration represents the configuration options for the Add Funds Widget. | ||
*/ | ||
export type AddFundsWidgetConfiguration = { | ||
// TODO : [ADD_FUNDS] - What are the configuration needed? | ||
} & WidgetConfiguration; |
11 changes: 11 additions & 0 deletions
11
packages/checkout/sdk/src/widgets/definitions/events/addFunds.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,11 @@ | ||
/** | ||
* Enum of possible Add Funds Widget event types. | ||
*/ | ||
export enum AddFundsEventType { | ||
CLOSE_WIDGET = 'close-widget', | ||
LANGUAGE_CHANGED = 'language-changed', | ||
REQUEST_BRIDGE = 'request-bridge', | ||
REQUEST_ONRAMP = 'request-onramp', | ||
REQUEST_SWAP = 'request-swap', | ||
GO_BACK = 'go-back', | ||
} |
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
21 changes: 21 additions & 0 deletions
21
packages/checkout/sdk/src/widgets/definitions/parameters/addFunds.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,21 @@ | ||
import { WidgetLanguage } from '../configurations'; | ||
|
||
export type AddFundsWidgetParams = { | ||
/** The language to use for the Add Funds widget */ | ||
language?: WidgetLanguage; | ||
|
||
/** Configure to show on-ramp option */ | ||
showOnrampOption?: boolean; | ||
|
||
/** Configure to show swap option */ | ||
showSwapOption?: boolean; | ||
|
||
/** Configure to show on bridge option */ | ||
showBridgeOption?: boolean; | ||
|
||
/** Token address of the fund to be added */ | ||
tokenAddress?: string; | ||
|
||
/** Amount of the fund to be added */ | ||
amount?: string; | ||
}; |
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
11 changes: 11 additions & 0 deletions
11
packages/checkout/widgets-lib/src/context/view-context/AddFundsViewContextTypes.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,11 @@ | ||
import { ViewType } from './ViewType'; | ||
|
||
export enum AddFundsWidgetViews { | ||
ADD_FUNDS = 'ADD_FUNDS', | ||
} | ||
|
||
export type AddFundsWidgetView = AddFundsView; | ||
|
||
interface AddFundsView extends ViewType { | ||
type: AddFundsWidgetViews.ADD_FUNDS; | ||
} |
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
100 changes: 100 additions & 0 deletions
100
packages/checkout/widgets-lib/src/widgets/add-funds/AddFundsRoot.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,100 @@ | ||
import { | ||
ChainId, | ||
IMTBLWidgetEvents, | ||
WidgetConfiguration, | ||
WidgetProperties, | ||
WidgetTheme, | ||
WidgetType, | ||
} from '@imtbl/checkout-sdk'; | ||
import React, { Suspense } from 'react'; | ||
import { AddFundsWidgetParams } from '@imtbl/checkout-sdk/dist/widgets/definitions/parameters/addFunds'; | ||
import { Base } from '../BaseWidgetRoot'; | ||
import { CustomAnalyticsProvider } from '../../context/analytics-provider/CustomAnalyticsProvider'; | ||
import { HandoverProvider } from '../../context/handover-context/HandoverProvider'; | ||
import i18n from '../../i18n'; | ||
import { LoadingView } from '../../views/loading/LoadingView'; | ||
import { ThemeProvider } from '../../components/ThemeProvider/ThemeProvider'; | ||
import { | ||
ConnectLoader, | ||
ConnectLoaderParams, | ||
} from '../../components/ConnectLoader/ConnectLoader'; | ||
import { getL1ChainId, getL2ChainId } from '../../lib'; | ||
import { sendAddFundsCloseEvent } from './AddFundsWidgetEvents'; | ||
|
||
const AddFundsWidget = React.lazy(() => import('./AddFundsWidget')); | ||
|
||
export class AddFunds extends Base<WidgetType.ADD_FUNDS> { | ||
protected eventTopic: IMTBLWidgetEvents = IMTBLWidgetEvents.IMTBL_ADD_FUNDS_WIDGET_EVENT; | ||
|
||
protected getValidatedProperties({ | ||
config, | ||
}: WidgetProperties<WidgetType.ADD_FUNDS>): WidgetProperties<WidgetType.ADD_FUNDS> { | ||
let validatedConfig: WidgetConfiguration | 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: AddFundsWidgetParams, | ||
): AddFundsWidgetParams { | ||
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, | ||
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="add-funds-container" config={this.strongConfig()}> | ||
<HandoverProvider> | ||
<ConnectLoader | ||
widgetConfig={this.strongConfig()} | ||
params={connectLoaderParams} | ||
closeEvent={() => sendAddFundsCloseEvent(window)} | ||
> | ||
<Suspense | ||
fallback={ | ||
<LoadingView loadingText={t('views.LOADING_VIEW.text')} /> | ||
} | ||
> | ||
<AddFundsWidget | ||
checkout={this.checkout} | ||
web3Provider={this.web3Provider} | ||
showBridgeOption={this.parameters.showBridgeOption} | ||
showSwapOption={this.parameters.showSwapOption} | ||
showOnrampOption={this.parameters.showOnrampOption} | ||
tokenAddress={this.parameters.tokenAddress} | ||
amount={this.parameters.amount} | ||
/> | ||
</Suspense> | ||
</ConnectLoader> | ||
</HandoverProvider> | ||
</ThemeProvider> | ||
</CustomAnalyticsProvider> | ||
</React.StrictMode>, | ||
); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
packages/checkout/widgets-lib/src/widgets/add-funds/AddFundsWidget.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,60 @@ | ||
import { AddFundsWidgetParams } from '@imtbl/checkout-sdk/dist/widgets/definitions/parameters/addFunds'; | ||
import { Checkout, IMTBLWidgetEvents } from '@imtbl/checkout-sdk'; | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { useContext, useMemo, useReducer } from 'react'; | ||
import { UserJourney } from '../../context/analytics-provider/SegmentAnalyticsProvider'; | ||
import { TopUpView } from '../../views/top-up/TopUpView'; | ||
import { | ||
sendAddFundsCloseEvent, | ||
sendAddFundsGoBackEvent, | ||
} from './AddFundsWidgetEvents'; | ||
import { EventTargetContext } from '../../context/event-target-context/EventTargetContext'; | ||
import { | ||
ViewContext, | ||
initialViewState, | ||
viewReducer, | ||
} from '../../context/view-context/ViewContext'; | ||
|
||
export type AddFundsWidgetInputs = AddFundsWidgetParams & { | ||
checkout: Checkout; | ||
web3Provider?: Web3Provider; | ||
}; | ||
|
||
export default function AddFundsWidget({ | ||
checkout, | ||
web3Provider, | ||
showOnrampOption = true, | ||
showSwapOption = true, | ||
showBridgeOption = true, | ||
tokenAddress, | ||
amount, | ||
}: AddFundsWidgetInputs) { | ||
const [viewState, viewDispatch] = useReducer(viewReducer, initialViewState); | ||
|
||
const viewReducerValues = useMemo( | ||
() => ({ viewState, viewDispatch }), | ||
[viewState, viewReducer], | ||
); | ||
|
||
const { | ||
eventTargetState: { eventTarget }, | ||
} = useContext(EventTargetContext); | ||
|
||
return ( | ||
<ViewContext.Provider value={viewReducerValues}> | ||
<TopUpView | ||
analytics={{ userJourney: UserJourney.ADD_FUNDS }} | ||
widgetEvent={IMTBLWidgetEvents.IMTBL_ADD_FUNDS_WIDGET_EVENT} | ||
checkout={checkout} | ||
provider={web3Provider} | ||
tokenAddress={tokenAddress} | ||
amount={amount} | ||
showOnrampOption={showOnrampOption} | ||
showSwapOption={showSwapOption} | ||
showBridgeOption={showBridgeOption} | ||
onCloseButtonClick={() => sendAddFundsCloseEvent(eventTarget)} | ||
onBackButtonClick={() => sendAddFundsGoBackEvent(eventTarget)} | ||
/> | ||
</ViewContext.Provider> | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/checkout/widgets-lib/src/widgets/add-funds/AddFundsWidgetEvents.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,38 @@ | ||
import { | ||
WidgetEvent, | ||
WidgetType, | ||
AddFundsEventType, | ||
IMTBLWidgetEvents, | ||
} from '@imtbl/checkout-sdk'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export function sendAddFundsCloseEvent(eventTarget: Window | EventTarget) { | ||
const closeWidgetEvent = new CustomEvent< | ||
WidgetEvent<WidgetType.ADD_FUNDS, AddFundsEventType.CLOSE_WIDGET> | ||
>(IMTBLWidgetEvents.IMTBL_ADD_FUNDS_WIDGET_EVENT, { | ||
detail: { | ||
type: AddFundsEventType.CLOSE_WIDGET, | ||
data: {}, | ||
}, | ||
}); | ||
// TODO: please remove or if necessary keep the eslint ignore | ||
// eslint-disable-next-line no-console | ||
console.log('close widget event:', closeWidgetEvent); | ||
if (eventTarget !== undefined) eventTarget.dispatchEvent(closeWidgetEvent); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export function sendAddFundsGoBackEvent(eventTarget: Window | EventTarget) { | ||
const closeWidgetEvent = new CustomEvent< | ||
WidgetEvent<WidgetType.ADD_FUNDS, AddFundsEventType.GO_BACK> | ||
>(IMTBLWidgetEvents.IMTBL_ADD_FUNDS_WIDGET_EVENT, { | ||
detail: { | ||
type: AddFundsEventType.GO_BACK, | ||
data: {}, | ||
}, | ||
}); | ||
// TODO: please remove or if necessary keep the eslint ignore | ||
// eslint-disable-next-line no-console | ||
console.log('go back event:', closeWidgetEvent); | ||
if (eventTarget !== undefined) eventTarget.dispatchEvent(closeWidgetEvent); | ||
} |
Oops, something went wrong.