-
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: Add skeleton for Purchase widget (#2398)
- Loading branch information
Showing
23 changed files
with
585 additions
and
5 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
4 changes: 4 additions & 0 deletions
4
packages/checkout/sdk/src/widgets/definitions/configurations/purchase.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,4 @@ | ||
import { WidgetConfiguration } from './widget'; | ||
|
||
export type PurchaseWidgetConfiguration = { | ||
} & 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
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
35 changes: 35 additions & 0 deletions
35
packages/checkout/sdk/src/widgets/definitions/events/purchase.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,35 @@ | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { EIP6963ProviderInfo } from '../../../types'; | ||
|
||
export enum PurchaseEventType { | ||
CLOSE_WIDGET = 'close-widget', | ||
CONNECT_SUCCESS = 'connect-success', | ||
SUCCESS = 'success', | ||
FAILURE = 'failure', | ||
} | ||
|
||
/** | ||
* Represents a successful purchase. | ||
*/ | ||
export type PurchaseSuccess = {}; | ||
|
||
/** | ||
* Type representing a purchase failure | ||
* @property {string} reason | ||
*/ | ||
export type PurchaseFailed = { | ||
reason: string; | ||
timestamp: number; | ||
}; | ||
|
||
/** | ||
* Type representing a successful provider connection | ||
* @property {Web3Provider} provider | ||
* @property {EIP6963ProviderInfo} providerInfo | ||
* @property {'from' | 'to'} providerType | ||
*/ | ||
export type PurchaseConnectSuccess = { | ||
provider: Web3Provider; | ||
providerInfo: EIP6963ProviderInfo; | ||
providerType: 'from' | 'to'; | ||
}; |
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
23 changes: 23 additions & 0 deletions
23
packages/checkout/sdk/src/widgets/definitions/parameters/purchase.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,23 @@ | ||
import { WidgetLanguage } from '../configurations'; | ||
|
||
export type PurchaseWidgetParams = { | ||
/** The language to use for the Purchase widget */ | ||
language?: WidgetLanguage; | ||
|
||
/** Environment id from Immutable Hub */ | ||
environmentId?: string; | ||
|
||
/** Whether to show a back button on the first screen, on click triggers REQUEST_GO_BACK event */ | ||
showBackButton?: boolean; | ||
|
||
/** The list of products to be purchased */ | ||
items?: PurchaseItem[]; | ||
}; | ||
|
||
export type PurchaseItem = { | ||
productId: string; | ||
qty: number; | ||
name: string; | ||
image: string; | ||
description: 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
24 changes: 24 additions & 0 deletions
24
packages/checkout/widgets-lib/src/context/view-context/PurchaseViewContextTypes.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,24 @@ | ||
import { ViewType } from './ViewType'; | ||
|
||
export enum PurchaseWidgetViews { | ||
PURCHASE = 'PURCHASE', | ||
REVIEW = 'REVIEW', | ||
GEO_BLOCK_ERROR = 'GEO_BLOCK_ERROR', | ||
} | ||
|
||
export type PurchaseWidgetView = PurchaseView | PurchaseReview | GeoBlockErrorView; | ||
|
||
interface PurchaseView extends ViewType { | ||
type: PurchaseWidgetViews.PURCHASE; | ||
} | ||
|
||
interface PurchaseReview extends ViewType { | ||
type: PurchaseWidgetViews.REVIEW; | ||
data: PurchaseReviewData; | ||
} | ||
|
||
interface GeoBlockErrorView extends ViewType { | ||
type: PurchaseWidgetViews.GEO_BLOCK_ERROR; | ||
} | ||
|
||
export interface PurchaseReviewData {} |
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
98 changes: 98 additions & 0 deletions
98
packages/checkout/widgets-lib/src/widgets/purchase/PurchaseWidget.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,98 @@ | ||
import { IMTBLWidgetEvents, PurchaseWidgetParams } from '@imtbl/checkout-sdk'; | ||
import { CloudImage, Stack, useTheme } from '@biom3/react'; | ||
import { useContext, useMemo, useReducer } from 'react'; | ||
import { StrongCheckoutWidgetsConfig } from '../../lib/withDefaultWidgetConfig'; | ||
import { initialPurchaseState, PurchaseContext, purchaseReducer } from './context/PurchaseContext'; | ||
import { PurchaseWidgetViews } from '../../context/view-context/PurchaseViewContextTypes'; | ||
import { | ||
initialViewState, | ||
ViewContext, | ||
viewReducer, | ||
} from '../../context/view-context/ViewContext'; | ||
import { Purchase } from './views/Purchase'; | ||
import { sendPurchaseCloseEvent } from './PurchaseWidgetEvents'; | ||
import { orchestrationEvents } from '../../lib/orchestrationEvents'; | ||
import { EventTargetContext } from '../../context/event-target-context/EventTargetContext'; | ||
import { getRemoteImage } from '../../lib/utils'; | ||
|
||
export type PurchaseWidgetInputs = PurchaseWidgetParams & { | ||
config: StrongCheckoutWidgetsConfig; | ||
}; | ||
|
||
export default function PurchaseWidget({ | ||
config, | ||
showBackButton, | ||
}: PurchaseWidgetInputs) { | ||
const { base: { colorMode } } = useTheme(); | ||
|
||
const [viewState, viewDispatch] = useReducer(viewReducer, { | ||
...initialViewState, | ||
view: { type: PurchaseWidgetViews.PURCHASE }, | ||
history: [{ type: PurchaseWidgetViews.PURCHASE }], | ||
}); | ||
|
||
const viewReducerValues = useMemo( | ||
() => ({ | ||
viewState, | ||
viewDispatch, | ||
}), | ||
[viewState, viewReducer], | ||
); | ||
|
||
const [purchaseState, purchaseDispatch] = useReducer( | ||
purchaseReducer, | ||
initialPurchaseState, | ||
); | ||
|
||
const purchaseReducerValues = useMemo( | ||
() => ({ | ||
purchaseState, | ||
purchaseDispatch, | ||
}), | ||
[purchaseState, purchaseReducer], | ||
); | ||
|
||
const { | ||
eventTargetState: { eventTarget }, | ||
} = useContext(EventTargetContext); | ||
|
||
return ( | ||
<ViewContext.Provider value={viewReducerValues}> | ||
<PurchaseContext.Provider value={purchaseReducerValues}> | ||
<Stack sx={{ pos: 'relative' }}> | ||
<CloudImage | ||
use={( | ||
<img | ||
src={getRemoteImage( | ||
config.environment, | ||
`/add-tokens-bg-texture-${colorMode}.webp`, | ||
)} | ||
alt="background texture" | ||
/> | ||
)} | ||
sx={{ | ||
pos: 'absolute', | ||
h: '100%', | ||
w: '100%', | ||
objectFit: 'cover', | ||
objectPosition: 'center', | ||
}} | ||
/> | ||
{viewState.view.type === PurchaseWidgetViews.PURCHASE && ( | ||
<Purchase | ||
showBackButton={showBackButton} | ||
onCloseButtonClick={() => sendPurchaseCloseEvent(eventTarget)} | ||
onBackButtonClick={() => { | ||
orchestrationEvents.sendRequestGoBackEvent( | ||
eventTarget, | ||
IMTBLWidgetEvents.IMTBL_PURCHASE_WIDGET_EVENT, | ||
{}, | ||
); | ||
}} | ||
/> | ||
)} | ||
</Stack> | ||
</PurchaseContext.Provider> | ||
</ViewContext.Provider> | ||
); | ||
} |
Oops, something went wrong.