Skip to content

Commit

Permalink
feat: add settings components
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Feb 17, 2022
1 parent 2370a30 commit a9d4175
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 153 deletions.
6 changes: 6 additions & 0 deletions packages/widget/src/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ export const Select = styled(MuiSelect)(({ theme }) => ({
borderColor: theme.palette.primary.main,
},
}));

export const MultiSelect = styled(Select)(({ theme }) => ({
[`& .${inputBaseClasses.input}`]: {
padding: '4px 14px',
},
}));
9 changes: 6 additions & 3 deletions packages/widget/src/components/SendToRecipientForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box, Checkbox, FormControl, FormControlLabel } from '@mui/material';
import { useFormContext } from 'react-hook-form';
import { useFormContext, useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { SwapFormKey } from '../providers/SwapFormProvider';
import { Input } from './Input';
Expand All @@ -8,11 +8,14 @@ export const SendToRecipientForm: React.FC = () => {
const { t } = useTranslation();
const {
register,
watch,
formState: { isSubmitting },
} = useFormContext();

if (!watch(SwapFormKey.IsSendToRecipient)) {
const sendToRecipientChecked = useWatch({
name: SwapFormKey.IsSendToRecipient,
});

if (!sendToRecipientChecked) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { HelpOutline as HelpOutlineIcon } from '@mui/icons-material';
import { Box, FormControl, MenuItem, Typography } from '@mui/material';
import { ChangeEvent, useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { Select } from '../Select';
import { Switch } from '../Switch';
import { EnabledBridgesSelect } from './EnabledBridgesSelect';
import { EnabledExchangesSelect } from './EnabledExchangesSelect';

export const AdvancedPreferences = () => {
const { t } = useTranslation();
const { register } = useFormContext();
const [advancedPreferences, setAdvancedPreferences] =
useState<boolean>(false);

const handleAdvancedPreferences = (
_: ChangeEvent<HTMLInputElement>,
checked: boolean,
) => {
setAdvancedPreferences(checked);
};

return (
<Box px={3}>
<Box
mt={3}
sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
}}
>
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<Typography
variant="subtitle1"
color="text.primary"
lineHeight="normal"
>
{t(`settings.advancedPreferences`)}
</Typography>
</Box>
<Switch
value={advancedPreferences}
onChange={handleAdvancedPreferences}
/>
</Box>
{advancedPreferences && (
<Box mt={3} mb={1}>
<Box sx={{ display: 'flex', alignItems: 'center' }} mb={1}>
<HelpOutlineIcon sx={{ color: 'grey.500' }} />
<Typography
variant="subtitle1"
color="text.secondary"
lineHeight="normal"
ml={1}
>
{t(`settings.bridgePrioritization`)}
</Typography>
</Box>
<FormControl fullWidth>
<Select
defaultValue={1}
MenuProps={{ elevation: 2 }}
inputProps={{
...register(SwapFormKey.BridgePrioritization),
}}
>
<MenuItem value={1}>
{t(`swap.routePriority.recommended`)}
</MenuItem>
</Select>
</FormControl>
<EnabledBridgesSelect />
<EnabledExchangesSelect />
</Box>
)}
</Box>
);
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { HelpOutline as HelpOutlineIcon } from '@mui/icons-material';
import { Box, FormControl, MenuItem, Typography } from '@mui/material';
import {
Box,
Chip,
FormControl,
MenuItem,
Skeleton,
Typography,
} from '@mui/material';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useBridges } from '../../hooks/useBridges';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { Select } from '../Select';
import { MultiSelect } from '../Select';

export const EnabledBridgesSelect: React.FC = () => {
const { t } = useTranslation();
const { register } = useFormContext();
const bridges = useBridges();

return (
<Box mt={3}>
Expand All @@ -22,15 +31,37 @@ export const EnabledBridgesSelect: React.FC = () => {
{t(`settings.enabledBridges`)}
</Typography>
</Box>
<FormControl fullWidth>
<Select
defaultValue={1}
MenuProps={{ elevation: 2 }}
inputProps={{ ...register(SwapFormKey.EnabledBridges) }}
>
<MenuItem value={1}>{t(`swap.routePriority.recommended`)}</MenuItem>
</Select>
</FormControl>
{bridges.length > 0 ? (
<FormControl fullWidth>
<MultiSelect
multiple
placeholder={t(`settings.selectEnabledBridges`)}
defaultValue={bridges}
MenuProps={{ elevation: 2 }}
inputProps={{ ...register(SwapFormKey.EnabledBridges) }}
renderValue={(selected) => (
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}>
{(selected as string[]).map((value) => (
<Chip key={value} label={value} />
))}
</Box>
)}
>
{bridges.map((bridge) => (
<MenuItem key={bridge} value={bridge}>
{bridge}
</MenuItem>
))}
</MultiSelect>
</FormControl>
) : (
<Skeleton
variant="rectangular"
width="100%"
height={44}
sx={{ borderRadius: '8px' }}
/>
)}
</Box>
);
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { HelpOutline as HelpOutlineIcon } from '@mui/icons-material';
import { Box, FormControl, MenuItem, Typography } from '@mui/material';
import {
Box,
Chip,
FormControl,
MenuItem,
Skeleton,
Typography,
} from '@mui/material';
import { useFormContext } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { useExchanges } from '../../hooks/useExchanges';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { Select } from '../Select';
import { MultiSelect } from '../Select';

export const EnabledExchangesSelect: React.FC = () => {
const { t } = useTranslation();
const { register } = useFormContext();
const exchanges = useExchanges();

return (
<Box mt={3}>
Expand All @@ -22,15 +31,37 @@ export const EnabledExchangesSelect: React.FC = () => {
{t(`settings.enabledExchanges`)}
</Typography>
</Box>
<FormControl fullWidth>
<Select
defaultValue={1}
MenuProps={{ elevation: 2 }}
inputProps={{ ...register(SwapFormKey.EnabledExchanges) }}
>
<MenuItem value={1}>{t(`swap.routePriority.recommended`)}</MenuItem>
</Select>
</FormControl>
{exchanges.length > 0 ? (
<FormControl fullWidth>
<MultiSelect
multiple
placeholder={t(`settings.selectEnabledExchanges`)}
defaultValue={exchanges}
MenuProps={{ elevation: 2 }}
inputProps={{ ...register(SwapFormKey.EnabledExchanges) }}
renderValue={(selected) => (
<Box sx={{ display: 'flex', flexWrap: 'wrap', gap: 0.5 }}>
{(selected as string[]).map((value) => (
<Chip key={value} label={value} />
))}
</Box>
)}
>
{exchanges.map((exchange) => (
<MenuItem key={exchange} value={exchange}>
{exchange}
</MenuItem>
))}
</MultiSelect>
</FormControl>
) : (
<Skeleton
variant="rectangular"
width="100%"
height={44}
sx={{ borderRadius: '8px' }}
/>
)}
</Box>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const Button = styled(MuiButton)(({ theme }) => ({
border: `2px solid rgba(19, 60, 76, 0.12)`,
[`&.${buttonClasses.outlined}`]: {
color: theme.palette.text.secondary,
fontWeight: 400,
},
[`&:hover`]: {
border: 2,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { HelpOutline as HelpOutlineIcon } from '@mui/icons-material';
import { Box, Typography } from '@mui/material';
import { useEffect } from 'react';
import { useFormContext, useWatch } from 'react-hook-form';
import { useTranslation } from 'react-i18next';
import { SwapFormKey } from '../../providers/SwapFormProvider';
import { Button, ButtonGroup } from './GasPriceButtonGroup.style';

export const GasPriceButtonGroup = () => {
const { t } = useTranslation();
const { register, setValue } = useFormContext();

const value = useWatch({
name: SwapFormKey.GasPrice,
});

const handleChange = (newValue: 'slow' | 'normal' | 'fast') => {
setValue(SwapFormKey.GasPrice, newValue);
};

useEffect(() => {
register(SwapFormKey.GasPrice);
}, [register]);

return (
<Box>
<Box sx={{ display: 'flex', alignItems: 'center' }} mb={1}>
<HelpOutlineIcon sx={{ color: 'grey.500' }} />
<Typography
variant="subtitle1"
color="text.secondary"
lineHeight="normal"
ml={1}
>
{t(`settings.gasPrice.title`)}
</Typography>
</Box>
<ButtonGroup size="large">
<Button
key="slow"
disableElevation
variant={value === 'slow' ? 'contained' : 'outlined'}
onClick={() => handleChange('slow')}
>
{t(`settings.gasPrice.slow`)}
</Button>
<Button
key="normal"
disableElevation
variant={value === 'normal' ? 'contained' : 'outlined'}
onClick={() => handleChange('normal')}
>
{t(`settings.gasPrice.normal`)}
</Button>
<Button
key="fast"
disableElevation
variant={value === 'fast' ? 'contained' : 'outlined'}
onClick={() => handleChange('fast')}
>
{t(`settings.gasPrice.fast`)}
</Button>
</ButtonGroup>
</Box>
);
};
Loading

0 comments on commit a9d4175

Please sign in to comment.