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

[WT-1786] Error view with orchestration events #992

Merged
merged 6 commits into from
Oct 17, 2023
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Box, Button } from '@biom3/react';
import { SimpleLayout } from '../../components/SimpleLayout/SimpleLayout';
import { HeaderNavigation } from '../../components/Header/HeaderNavigation';
import { SimpleTextBody } from '../../components/Body/SimpleTextBody';
Expand All @@ -10,11 +11,19 @@ import { ServiceType } from './serviceTypes';
export interface ServiceUnavailableErrorViewProps {
service: ServiceType;
onCloseClick: () => void;
primaryActionText?: string;
onPrimaryButtonClick?: () => void;
secondaryActionText?: string;
onSecondaryButtonClick?: () => void;
}

export function ServiceUnavailableErrorView({
service,
onCloseClick,
primaryActionText,
onPrimaryButtonClick,
secondaryActionText,
onSecondaryButtonClick,
}: ServiceUnavailableErrorViewProps) {
const errorText = text.views[SharedViews.SERVICE_UNAVAILABLE_ERROR_VIEW];
const headingText = errorText.heading[service];
Expand All @@ -30,6 +39,55 @@ export function ServiceUnavailableErrorView({
testId="service-unavailable-error-view"
>
<SimpleTextBody heading={headingText}>{errorText.body}</SimpleTextBody>

<Box
testId="button-container"
sx={{
height: '100%',
display: 'flex',
flexDirection: 'column',
justifyContent: 'flex-end',
}}
>

{primaryActionText && onPrimaryButtonClick && (
<Box
sx={{
paddingX: 'base.spacing.x4',
paddingBottom: 'base.spacing.x2',
}}
>
<Button
sx={{ width: '100%' }}
testId="primary-action-button"
variant="primary"
size="large"
onClick={onPrimaryButtonClick}
>
{primaryActionText}
</Button>
</Box>
)}

{secondaryActionText && onSecondaryButtonClick && (
<Box
sx={{
paddingX: 'base.spacing.x4',
paddingBottom: 'base.spacing.x2',
}}
>
<Button
sx={{ width: '100%' }}
testId="secondary-action-button"
variant="secondary"
size="large"
onClick={onSecondaryButtonClick}
>
{secondaryActionText}
</Button>
</Box>
)}
</Box>
</SimpleLayout>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export function TopUpView({
});
return;
}
orchestrationEvents.sendRequestOnrampEvent(window, widgetEvent, {
orchestrationEvents.sendRequestOnrampEvent(eventTarget, widgetEvent, {
tokenAddress: tokenAddress ?? '',
amount: amount ?? '',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
import { CustomAnalyticsProvider } from '../../context/analytics-provider/CustomAnalyticsProvider';
import { ServiceUnavailableErrorView } from '../../views/error/ServiceUnavailableErrorView';
import { ServiceType } from '../../views/error/serviceTypes';
import { isPassportProvider } from '../../lib/providerUtils';
import { topUpBridgeOption, topUpOnRampOption } from './helpers';

export class ImmutableSwap extends ImmutableWebComponent {
walletProvider: WalletProviderName | undefined = undefined;
Expand Down Expand Up @@ -95,6 +97,24 @@ export class ImmutableSwap extends ImmutableWebComponent {
}
}

private isNotPassport = !isPassportProvider(this.provider)
|| this.walletProvider !== WalletProviderName.PASSPORT;

private topUpOptions(): { text:string, action: ()=>void }[] | undefined {
deepti-imx marked this conversation as resolved.
Show resolved Hide resolved
const optionsArray: { text:string, action: ()=>void }[] = [];

const isOnramp = topUpOnRampOption(this.widgetConfig!.isOnRampEnabled);
if (isOnramp) {
optionsArray.push({ text: isOnramp.text, action: isOnramp.action });
}
const isBridge = topUpBridgeOption(this.widgetConfig!.isBridgeEnabled, this.isNotPassport);
if (isBridge) {
optionsArray.push({ text: isBridge.text, action: isBridge.action });
}

return optionsArray;
}

renderWidget() {
this.validateInputs();
const connectLoaderParams: ConnectLoaderParams = {
Expand All @@ -113,6 +133,8 @@ export class ImmutableSwap extends ImmutableWebComponent {

let isSwapAvailable = false;

const topUpOptions = this.topUpOptions();

this.checkout
?.isSwapAvailable()
.then((available) => {
Expand All @@ -132,6 +154,14 @@ export class ImmutableSwap extends ImmutableWebComponent {
<ServiceUnavailableErrorView
service={ServiceType.SWAP}
onCloseClick={() => sendSwapWidgetCloseEvent(window)}
primaryActionText={topUpOptions && topUpOptions?.length > 0 ? topUpOptions[0].text : undefined}
onPrimaryButtonClick={
topUpOptions && topUpOptions?.length > 0
? topUpOptions[0].action
: undefined
}
secondaryActionText={topUpOptions?.length === 2 ? topUpOptions[1].text : undefined}
onSecondaryButtonClick={topUpOptions?.length === 2 ? topUpOptions[1].action : undefined}
/>
</BiomeCombinedProviders>
)}
Expand Down
55 changes: 45 additions & 10 deletions packages/checkout/widgets-lib/src/widgets/swap/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
import { TokenInfo } from '@imtbl/checkout-sdk';

export const findTokenByAddress = (
tokens: TokenInfo[],
contractAddress: string | undefined,
): TokenInfo | undefined => {
if (!contractAddress) return tokens[0];
return tokens.find(
(t) => t.address?.toLowerCase() === contractAddress.toLowerCase(),
);
};
import { IMTBLWidgetEvents } from '@imtbl/checkout-widgets';
import { text } from '../../resources/text/textConfig';
import { SharedViews } from '../../context/view-context/ViewContext';
import { orchestrationEvents } from '../../lib/orchestrationEvents';

export const alphaSortTokensList = (
tokens: TokenInfo[],
): TokenInfo[] => tokens.sort((a, b) => a.symbol.localeCompare(b.symbol));

export const topUpBridgeOption = (
isBridgeEnabled:boolean,
isNotPassport:boolean,
): { text:string, action: ()=>void } | undefined => {
if (isBridgeEnabled && isNotPassport) {
return {
text: text.views[SharedViews.TOP_UP_VIEW].topUpOptions.bridge.heading,
action: () => {
orchestrationEvents.sendRequestBridgeEvent(
window,
IMTBLWidgetEvents.IMTBL_BRIDGE_WIDGET_EVENT,
{
tokenAddress: '',
amount: '',
},
);
},
};
}
return undefined;
};

export const topUpOnRampOption = (isOnRampEnabled:boolean): { text:string, action: ()=>void } | undefined => {
if (isOnRampEnabled) {
return {
text: text.views[SharedViews.TOP_UP_VIEW].topUpOptions.onramp.heading,
action: () => {
orchestrationEvents.sendRequestOnrampEvent(
window,
IMTBLWidgetEvents.IMTBL_ONRAMP_WIDGET_EVENT,
{
tokenAddress: '',
amount: '',
},
);
},
};
}
return undefined;
};