Skip to content

Commit

Permalink
feat: add delete active swaps button
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Oct 3, 2022
1 parent 6f09075 commit 3365ed8
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 47 deletions.
9 changes: 1 addition & 8 deletions packages/widget/src/components/Header/NavigationHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ export const NavigationHeader: React.FC = () => {
return (
<HeaderAppBar elevation={0}>
{backButtonRoutes.includes(path) ? (
<IconButton
size="medium"
aria-label="settings"
edge="start"
onClick={navigateBack}
>
<IconButton size="medium" edge="start" onClick={navigateBack}>
<ArrowBackIcon />
</IconButton>
) : null}
Expand All @@ -86,7 +81,6 @@ export const NavigationHeader: React.FC = () => {
{account.isActive ? (
<IconButton
size="medium"
aria-label="swap-history"
edge="start"
onClick={() => navigate(navigationRoutes.swapHistory)}
>
Expand All @@ -95,7 +89,6 @@ export const NavigationHeader: React.FC = () => {
) : null}
<IconButton
size="medium"
aria-label="settings"
edge="end"
onClick={() => navigate(navigationRoutes.settings)}
>
Expand Down
8 changes: 1 addition & 7 deletions packages/widget/src/components/Header/WalletHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ export const WalletHeader: React.FC = () => {
{walletAddress}
</Typography>
</Box>
<IconButton
size="medium"
aria-label="disconnect"
edge="end"
onClick={disconnect}
>
<IconButton size="medium" edge="end" onClick={disconnect}>
<LogoutIcon />
</IconButton>
</>
Expand Down Expand Up @@ -75,7 +70,6 @@ const ConnectButton = () => {
return (
<IconButton
size="medium"
aria-label="disconnect"
edge="end"
onClick={
!pathname.includes(navigationRoutes.selectWallet) ? connect : undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ export const ReverseTokensButton: React.FC<{ vertical?: boolean }> = ({
setValue(SwapFormKey.ToToken, fromToken);
};
return (
<IconButton
onClick={handleClick}
size="small"
aria-label="swap-destinations"
>
<IconButton onClick={handleClick} size="small">
{vertical ? <SwapVertIcon /> : <SwapHorizIcon />}
</IconButton>
);
Expand Down
1 change: 0 additions & 1 deletion packages/widget/src/components/Step/StepProcess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const StepProcess: React.FC<{
>
<LinkButton
size="small"
aria-label="settings"
edge="end"
LinkComponent={Link}
href={process.txLink}
Expand Down
6 changes: 4 additions & 2 deletions packages/widget/src/i18n/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@
"title": {
"insufficientGas": "Insufficient gas",
"deleteSwap": "Delete this swap?",
"deleteSwapHistory": "Delete swap history?"
"deleteSwapHistory": "Delete swap history?",
"deleteActiveSwaps": "Delete all active swaps?"
},
"message": {
"insufficientFunds": "You don't have enough funds to execute the swap.",
"insufficientGas": "You need to add at least:",
"deleteSwap": "Swap history is only stored locally and can't be recovered if you delete it."
"deleteSwapHistory": "Swap history is only stored locally and can't be recovered if you delete it.",
"deleteActiveSwaps": "Active swaps are only stored locally and can't be recovered if you delete them."
}
},
"error": {
Expand Down
51 changes: 49 additions & 2 deletions packages/widget/src/pages/ActiveSwapsPage/ActiveSwapsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
import { Container, List } from '@mui/material';
/* eslint-disable consistent-return */
import { DeleteOutline as DeleteIcon } from '@mui/icons-material';
import {
Button,
Container,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
IconButton,
List,
} from '@mui/material';
import { useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { ActiveSwapItem } from '../../components/ActiveSwaps';
import { Dialog } from '../../components/Dialog';
import { useHeaderActionStore } from '../../components/Header';
import { useWallet } from '../../providers';
import { useExecutingRoutesIds } from '../../stores';
import { useExecutingRoutesIds, useRouteExecutionStore } from '../../stores';
import { ActiveSwapsEmpty } from './ActiveSwapsEmpty';

export const ActiveSwapsPage = () => {
const { t } = useTranslation();
const { account } = useWallet();
const executingRoutes = useExecutingRoutesIds(account.address);
const deleteRoutes = useRouteExecutionStore((store) => store.deleteRoutes);
const [open, setOpen] = useState(false);

const toggleDialog = useCallback(() => {
setOpen((open) => !open);
}, []);

useEffect(() => {
if (executingRoutes.length) {
return useHeaderActionStore.getState().setAction(
<IconButton size="medium" edge="end" onClick={toggleDialog}>
<DeleteIcon />
</IconButton>,
);
}
}, [executingRoutes.length, toggleDialog]);

if (!executingRoutes.length) {
return <ActiveSwapsEmpty />;
}
Expand All @@ -23,6 +56,20 @@ export const ActiveSwapsPage = () => {
<ActiveSwapItem key={routeId} routeId={routeId} />
))}
</List>
<Dialog open={open} onClose={toggleDialog}>
<DialogTitle>{t('swap.warning.title.deleteActiveSwaps')}</DialogTitle>
<DialogContent>
<DialogContentText>
{t('swap.warning.message.deleteActiveSwaps')}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={toggleDialog}>{t('button.cancel')}</Button>
<Button onClick={() => deleteRoutes('active')} autoFocus>
{t('button.delete')}
</Button>
</DialogActions>
</Dialog>
</Container>
);
};
13 changes: 2 additions & 11 deletions packages/widget/src/pages/SwapDetailsPage/SwapDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ export const SwapDetailsPage: React.FC = () => {

useEffect(() => {
return useHeaderActionStore.getState().setAction(
<IconButton
size="medium"
aria-label="settings"
edge="end"
onClick={toggleDialog}
>
<IconButton size="medium" edge="end" onClick={toggleDialog}>
<DeleteIcon />
</IconButton>,
);
Expand Down Expand Up @@ -120,11 +115,7 @@ export const SwapDetailsPage: React.FC = () => {
>
<CardTitle flex={1}>{t('swap.supportId')}</CardTitle>
<Box mr={1} mt={1}>
<IconButton
size="medium"
aria-label="settings"
onClick={copyRouteId}
>
<IconButton size="medium" onClick={copyRouteId}>
<ContentCopyIcon fontSize="small" />
</IconButton>
</Box>
Expand Down
11 changes: 3 additions & 8 deletions packages/widget/src/pages/SwapHistoryPage/SwapHistoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ export const SwapHistoryPage: React.FC = () => {
useEffect(() => {
if (swaps.length) {
return useHeaderActionStore.getState().setAction(
<IconButton
size="medium"
aria-label="settings"
edge="end"
onClick={toggleDialog}
>
<IconButton size="medium" edge="end" onClick={toggleDialog}>
<DeleteIcon />
</IconButton>,
);
Expand All @@ -65,12 +60,12 @@ export const SwapHistoryPage: React.FC = () => {
<DialogTitle>{t('swap.warning.title.deleteSwapHistory')}</DialogTitle>
<DialogContent>
<DialogContentText>
{t('swap.warning.message.deleteSwap')}
{t('swap.warning.message.deleteSwapHistory')}
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={toggleDialog}>{t('button.cancel')}</Button>
<Button onClick={deleteRoutes} autoFocus>
<Button onClick={() => deleteRoutes('completed')} autoFocus>
{t('button.delete')}
</Button>
</DialogActions>
Expand Down
2 changes: 1 addition & 1 deletion packages/widget/src/stores/routes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface RouteExecutionStore {
updateRoute: (route: Route) => void;
restartRoute: (routeId: string) => void;
deleteRoute: (routeId: string) => void;
deleteRoutes: () => void;
deleteRoutes: (type: 'completed' | 'active') => void;
}

export type RouteExecutionStatus = 'error' | 'idle' | 'loading' | 'success';
Expand Down
10 changes: 8 additions & 2 deletions packages/widget/src/stores/routes/useRouteExecutionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ export const useRouteExecutionStore = create<RouteExecutionStore>()(
delete state.routes[routeId];
}
}),
deleteRoutes: () =>
deleteRoutes: (type) =>
set((state: RouteExecutionStore) => {
state.routes = {};
Object.keys(state.routes)
.filter((routeId) =>
type === 'completed'
? state.routes[routeId]?.status === 'success'
: state.routes[routeId]?.status !== 'success',
)
.forEach((routeId) => delete state.routes[routeId]);
}),
})),
{
Expand Down

0 comments on commit 3365ed8

Please sign in to comment.