Skip to content

Commit

Permalink
feat: add requiredUI config option
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Feb 1, 2023
1 parent 88b7f6a commit 61a7960
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/widget-playground/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export const widgetBaseConfig: WidgetConfig = {
// useRecommendedRoute: true,
buildSwapUrl: true,
// disabledUI: ['toAddress', 'fromAmount', 'toToken', 'fromToken'],
requiredUI: ['toAddress'],
sdkConfig: {
// apiUrl: 'https://staging.li.quest/v1/',
defaultRouteOptions: {
Expand Down
8 changes: 7 additions & 1 deletion packages/widget/src/components/Card/CardTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Typography } from '@mui/material';
import { styled } from '@mui/material/styles';

export const CardTitle = styled(Typography)(({ theme }) => ({
export const CardTitle = styled(Typography, {
shouldForwardProp: (prop) => !['required'].includes(prop as string),
})<{ required?: boolean }>(({ theme, required }) => ({
fontSize: '0.875rem',
lineHeight: '1.286',
letterSpacing: '0.01071em',
fontWeight: 700,
padding: theme.spacing(1.75, 2, 0, 2),
'&:after': {
content: required ? '" *"' : 'none',
color: theme.palette.error.main,
},
}));
44 changes: 29 additions & 15 deletions packages/widget/src/components/SendToWallet/SendToWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,44 @@ import { FormControl, Input } from './SendToWallet.style';

export const SendToWallet: React.FC<BoxProps> = forwardRef((props, ref) => {
const { t } = useTranslation();
const { register, trigger } = useFormContext();
const { register, unregister, trigger, getValues, clearErrors } =
useFormContext();
const { account, provider } = useWallet();
const { disabledUI, hiddenUI, toAddress } = useWidgetConfig();
const { disabledUI, hiddenUI, requiredUI, toAddress } = useWidgetConfig();
const { showSendToWallet, showSendToWalletDirty, setSendToWallet } =
useSendToWalletStore();
const { showDestinationWallet } = useSettings(['showDestinationWallet']);

useEffect(() => {
trigger(SwapFormKey.ToAddress);
}, [account.chainId, trigger]);

const hiddenToAddress = hiddenUI?.includes(HiddenUI.ToAddress);
const disabledToAddress = disabledUI?.includes(DisabledUI.ToAddress);
const requiredToAddress = requiredUI?.includes(DisabledUI.ToAddress);

// We want to show toAddress field if it is set via widget configuration, disabled for changes, but not hidden
const showInstantly = Boolean(
!showSendToWalletDirty &&
toAddress &&
disabledToAddress &&
showDestinationWallet &&
!hiddenToAddress,
);
// We want to show toAddress field if it is set via widget configuration and not hidden
const showInstantly =
Boolean(
!showSendToWalletDirty &&
showDestinationWallet &&
toAddress &&
!hiddenToAddress,
) || requiredToAddress;

useEffect(() => {
if (showInstantly) {
setSendToWallet(true);
}
}, [showInstantly, setSendToWallet]);

useEffect(() => {
const value = getValues(SwapFormKey.ToAddress);
if (value) {
trigger(SwapFormKey.ToAddress);
}
}, [account.chainId, getValues, trigger]);

useEffect(() => {
unregister(SwapFormKey.ToAddress);
}, [requiredToAddress, unregister]);

if (hiddenToAddress) {
return null;
}
Expand All @@ -51,6 +60,9 @@ export const SendToWallet: React.FC<BoxProps> = forwardRef((props, ref) => {
name,
ref: inputRef,
} = register(SwapFormKey.ToAddress, {
required:
requiredToAddress &&
(t('swap.error.title.walletAddressRequired') as string),
validate: async (value: string) => {
try {
if (!value) {
Expand All @@ -76,7 +88,9 @@ export const SendToWallet: React.FC<BoxProps> = forwardRef((props, ref) => {
unmountOnExit
>
<Card {...props} ref={ref}>
<CardTitle>{t('swap.sendToWallet')}</CardTitle>
<CardTitle required={requiredToAddress}>
{t('swap.sendToWallet')}
</CardTitle>
<FormControl fullWidth sx={{ paddingTop: '6px', paddingBottom: '5px' }}>
<Input
ref={inputRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ export const SendToWalletButton: React.FC = () => {
const { t } = useTranslation();
const { setValue } = useFormContext();
const { account } = useWallet();
const { disabledUI, hiddenUI } = useWidgetConfig();
const { disabledUI, hiddenUI, requiredUI } = useWidgetConfig();
const { showSendToWallet, toggleSendToWallet } = useSendToWalletStore();
const { showDestinationWallet } = useSettings(['showDestinationWallet']);

if (
!showDestinationWallet ||
!account.isActive ||
hiddenUI?.includes(HiddenUI.ToAddress)
hiddenUI?.includes(HiddenUI.ToAddress) ||
requiredUI?.includes(HiddenUI.ToAddress)
) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"transactionUnprepared": "Unable to prepare transaction",
"unknown": "Something went wrong",
"walletAddressInvalid": "Wallet address is invalid.",
"walletAddressRequired": "Wallet address is required.",
"walletEnsAddressInvalid": "Wallet address is invalid or network doesn't support ENS."
}
},
Expand Down
4 changes: 3 additions & 1 deletion packages/widget/src/pages/MainPage/MainSwapButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export const MainSwapButton: React.FC = () => {
<SwapButton
onClick={handleClick}
currentRoute={currentRoute}
disabled={isLoading || isFetching || isValidating || !isValid}
disabled={
currentRoute && (isLoading || isFetching || isValidating || !isValid)
}
/>
);
};
6 changes: 6 additions & 0 deletions packages/widget/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export enum HiddenUI {
}
export type HiddenUIType = `${HiddenUI}`;

export enum RequiredUI {
ToAddress = 'toAddress',
}
export type RequiredUIType = `${RequiredUI}`;

export type Appearance = PaletteMode | 'auto';
export type ThemeConfig = {
palette?: Pick<
Expand Down Expand Up @@ -114,6 +119,7 @@ export interface WidgetConfig {
disableTelemetry?: boolean;
disabledUI?: DisabledUIType[];
hiddenUI?: HiddenUIType[];
requiredUI?: RequiredUIType[];
useRecommendedRoute?: boolean;

walletManagement?: WidgetWalletManagement;
Expand Down

0 comments on commit 61a7960

Please sign in to comment.