Skip to content

Commit

Permalink
feat: add you pay token price
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed May 27, 2022
1 parent f3b9498 commit 5a6fddf
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 90 deletions.
1 change: 0 additions & 1 deletion packages/widget/src/components/PriceTypography.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ import { styled } from '@mui/material/styles';
export const PriceTypography = styled(Typography)(({ theme }) => ({
fontWeight: 400,
fontSize: '0.75rem',
color: theme.palette.text.secondary,
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { FormHelperText as MuiFormHelperText } from '@mui/material';
import { styled } from '@mui/material/styles';

export const FormHelperText = styled(MuiFormHelperText)(({ theme }) => ({
display: 'flex',
justifyContent: 'space-between',
margin: 0,
}));
59 changes: 59 additions & 0 deletions packages/widget/src/components/SwapInput/FormPriceHelperText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Skeleton } from '@mui/material';
import { useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useTokenBalance } from '../../hooks';
import {
SwapFormKeyHelper,
SwapFormTypeProps,
} from '../../providers/SwapFormProvider';
import { formatTokenPrice } from '../../utils/format';
import { PriceTypography } from '../PriceTypography';
import { FormHelperText } from './FormPriceHelperText.style';

export const FormPriceHelperText: React.FC<
SwapFormTypeProps & { selected: boolean }
> = ({ formType, selected }) => {
const { t } = useTranslation();
const [amount, chainId, tokenAddress] = useWatch({
name: [
SwapFormKeyHelper.getAmountKey(formType),
SwapFormKeyHelper.getChainKey(formType),
SwapFormKeyHelper.getTokenKey(formType),
],
});
const { token, isLoading, isFetching } = useTokenBalance(
chainId,
tokenAddress,
);

const fromAmountTokenPrice = formatTokenPrice(amount, token?.priceUSD);
const maxAmountTokenPrice = formatTokenPrice(token?.amount, token?.priceUSD);

return (
<FormHelperText>
<PriceTypography
color={fromAmountTokenPrice ? 'text.secondary' : 'grey.600'}
marginLeft={selected ? 8 : 2}
lineHeight={1.3334}
>
{t(`swap.currency`, {
value: fromAmountTokenPrice,
})}
</PriceTypography>
{isLoading && isFetching ? (
<Skeleton
variant="text"
width={48}
height={16}
sx={{ borderRadius: 0.5 }}
/>
) : maxAmountTokenPrice ? (
<PriceTypography color="text.secondary" lineHeight={1.3334}>
{t(`swap.currency`, {
value: maxAmountTokenPrice,
})}
</PriceTypography>
) : null}
</FormHelperText>
);
};
8 changes: 6 additions & 2 deletions packages/widget/src/components/SwapInput/SwapInput.style.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { InputBase } from '@mui/material';
import { FormControl as MuiFormControl, InputBase } from '@mui/material';
import { inputBaseClasses } from '@mui/material/InputBase';
import { styled } from '@mui/material/styles';

export const FormControl = styled(MuiFormControl)(({ theme }) => ({
padding: theme.spacing(1.5, 2, 1.5, 0),
}));

export const Input = styled(InputBase)(({ theme }) => ({
fontSize: 24,
fontWeight: 700,
padding: theme.spacing(2, 2, 2, 0),
// padding: theme.spacing(2, 2, 2, 0),
[`.${inputBaseClasses.input}`]: {
height: 32,
padding: theme.spacing(0, 0, 0, 2),
Expand Down
9 changes: 5 additions & 4 deletions packages/widget/src/components/SwapInput/SwapInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Avatar, FormControl } from '@mui/material';
import { Avatar } from '@mui/material';
import { ChangeEvent } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
Expand All @@ -9,8 +9,9 @@ import {
} from '../../providers/SwapFormProvider';
import { formatAmount } from '../../utils/format';
import { CardContainer, CardTitle } from '../Card';
import { SwapInputAdornment } from '../SwapInputAdornment';
import { Input } from './SwapInput.style';
import { FormPriceHelperText } from './FormPriceHelperText';
import { FormControl, Input } from './SwapInput.style';
import { SwapInputAdornment } from './SwapInputAdornment';

export const SwapInput: React.FC<SwapFormTypeProps> = ({ formType }) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -71,7 +72,7 @@ export const SwapInput: React.FC<SwapFormTypeProps> = ({ formType }) => {
name={amountKey}
required
/>
{/* <FormHelperText id="swap-from-helper-text">Text</FormHelperText> */}
<FormPriceHelperText selected={isSelected} formType={formType} />
</FormControl>
</CardContainer>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { styled } from '@mui/material/styles';
export const SwapMaxAmountTypography = styled(Typography)(({ theme }) => ({
fontSize: 24,
fontWeight: '700',
color: theme.palette.grey[600],
lineHeight: 1.3334,
color: theme.palette.text.secondary,
transition: theme.transitions.create(['color']),
padding: theme.spacing(0.5, 0),
// padding: theme.spacing(0.5, 0),
'&:hover': {
cursor: 'pointer',
color: theme.palette.text.primary,
Expand Down
56 changes: 56 additions & 0 deletions packages/widget/src/components/SwapInput/SwapInputAdornment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { InputAdornment, Skeleton } from '@mui/material';
import { useFormContext, useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useTokenBalance } from '../../hooks';
import {
SwapFormKeyHelper,
SwapFormTypeProps,
} from '../../providers/SwapFormProvider';
import { SwapMaxAmountTypography } from './SwapInputAdornment.style';

export const SwapInputAdornment: React.FC<SwapFormTypeProps> = ({
formType,
}) => {
const { t } = useTranslation();
const { setValue } = useFormContext();
const [chainId, tokenAddress] = useWatch({
name: [
SwapFormKeyHelper.getChainKey(formType),
SwapFormKeyHelper.getTokenKey(formType),
],
});
const { token, isLoading, isFetching } = useTokenBalance(
chainId,
tokenAddress,
);

const handleMax = () => {
setValue(SwapFormKeyHelper.getAmountKey(formType), token?.amount ?? '');
};

return (
<InputAdornment position="end">
{isLoading && isFetching ? (
<Skeleton
variant="rectangular"
width={96}
height={24}
sx={{ borderRadius: 1 }}
/>
) : formType === 'from' && token?.amount ? (
<SwapMaxAmountTypography
onClick={handleMax}
role="button"
sx={{
userSelect: 'none',
}}
data-amount={token?.amount}
>
{t(`swap.maxAmount`, {
amount: token?.amount,
})}
</SwapMaxAmountTypography>
) : null}
</InputAdornment>
);
};

This file was deleted.

1 change: 0 additions & 1 deletion packages/widget/src/components/SwapInputAdornment/index.ts

This file was deleted.

5 changes: 4 additions & 1 deletion packages/widget/src/components/TokenList/TokenListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export const TokenListItem: React.FC<TokenListItemProps> = memo(
{token.amount ?? '0'}
</Typography>
{tokenPrice ? (
<PriceTypography noWrap data-price={token.priceUSD}>
<PriceTypography
color="text.secondary"
data-price={token.priceUSD}
>
{t(`swap.currency`, {
value: tokenPrice,
})}
Expand Down
1 change: 1 addition & 0 deletions packages/widget/src/hooks/useTokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const useTokenBalance = (chainId: number, tokenAddress: string) => {
}
const tokenBalance = await LiFi.getTokenBalance(address as string, token);
return {
...token,
...tokenBalance,
amount: formatTokenAmount(tokenBalance?.amount),
};
Expand Down

0 comments on commit 5a6fddf

Please sign in to comment.