Skip to content

Commit bb1a90f

Browse files
committed
refactor(556x): migrate pages/swaps to react-router-dom-v5-compat
1 parent 6182154 commit bb1a90f

File tree

20 files changed

+198
-214
lines changed

20 files changed

+198
-214
lines changed

ui/ducks/swaps/swaps.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,12 @@ export {
525525
slice as swapsSlice,
526526
};
527527

528-
export const navigateBackToPrepareSwap = (history) => {
528+
export const navigateBackToPrepareSwap = (navigate) => {
529529
return async (dispatch) => {
530530
// TODO: Ensure any fetch in progress is cancelled
531531
await dispatch(setBackgroundSwapRouteState(''));
532532
dispatch(navigatedBackToBuildQuote());
533-
history.push(PREPARE_SWAP_ROUTE);
533+
navigate(PREPARE_SWAP_ROUTE);
534534
};
535535
};
536536

@@ -632,7 +632,7 @@ const isTokenAlreadyAdded = (tokenAddress, tokens) => {
632632
};
633633

634634
export const fetchQuotesAndSetQuoteState = (
635-
history,
635+
navigate,
636636
inputValue,
637637
maxSlippage,
638638
trackEvent,
@@ -658,7 +658,7 @@ export const fetchQuotesAndSetQuoteState = (
658658
await dispatch(setSwapsLiveness(swapsLivenessForNetwork));
659659

660660
if (!swapsLivenessForNetwork.swapsFeatureIsLive) {
661-
await history.push(SWAPS_MAINTENANCE_ROUTE);
661+
await navigate(SWAPS_MAINTENANCE_ROUTE);
662662
return;
663663
}
664664

@@ -691,7 +691,7 @@ export const fetchQuotesAndSetQuoteState = (
691691
// In that case we just want to silently prefetch quotes without redirecting to the quotes loading page.
692692
if (!pageRedirectionDisabled) {
693693
await dispatch(setBackgroundSwapRouteState('loading'));
694-
history.push(LOADING_QUOTES_ROUTE);
694+
navigate(LOADING_QUOTES_ROUTE);
695695
}
696696
dispatch(setFetchingQuotes(true));
697697

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import React, { useMemo } from 'react';
1+
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { Redirect, Route } from 'react-router-dom';
3+
import { Navigate } from 'react-router-dom-v5-compat';
44

5-
export default function FeatureToggledRoute({ flag, redirectRoute, ...props }) {
6-
const redirect = useMemo(
7-
() => ({ pathname: redirectRoute }),
8-
[redirectRoute],
9-
);
5+
export default function FeatureToggledRoute({ flag, redirectRoute, element }) {
106
if (flag) {
11-
return <Route {...props} />;
7+
return element;
128
}
13-
return <Redirect to={redirect} />;
9+
return <Navigate to={redirectRoute} replace />;
1410
}
1511

1612
FeatureToggledRoute.propTypes = {
1713
flag: PropTypes.bool.isRequired,
1814
redirectRoute: PropTypes.string.isRequired,
15+
element: PropTypes.node.isRequired,
1916
};

ui/pages/routes/routes.component.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,18 @@ export default function Routes() {
624624
component={ConfirmTransaction}
625625
/>
626626
<Authenticated path={`${SEND_ROUTE}/:page?`} component={SendPage} />
627-
<Authenticated path={SWAPS_ROUTE} component={Swaps} />
627+
<Route path={SWAPS_ROUTE}>
628+
{(props: RouteComponentProps) => {
629+
const { location: v5Location } = props;
630+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
631+
const SwapsComponent = Swaps as any;
632+
return (
633+
<AuthenticatedV5Compat>
634+
<SwapsComponent location={v5Location} />
635+
</AuthenticatedV5Compat>
636+
);
637+
}}
638+
</Route>
628639
<Route
629640
path={`${CROSS_CHAIN_SWAP_TX_DETAILS_ROUTE}/:srcTxMetaId`}
630641
// v5 Route supports exact with render props, but TS types don't recognize it
@@ -1024,6 +1035,7 @@ export default function Routes() {
10241035
{renderRoutes()}
10251036
</Box>
10261037
{isUnlocked ? <Alerts history={history} /> : null}
1038+
{/* @ts-expect-error - ToastMaster is a JS component */}
10271039
<ToastMaster location={location} />
10281040
</div>
10291041
);

ui/pages/swaps/awaiting-signatures/awaiting-signatures.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useContext, useEffect } from 'react';
22
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
3-
import { useHistory } from 'react-router-dom';
3+
import { useNavigate } from 'react-router-dom-v5-compat';
44
import isEqual from 'lodash/isEqual';
5-
65
import { I18nContext } from '../../../contexts/i18n';
76
import {
87
getFetchParams,
@@ -39,7 +38,7 @@ import SwapStepIcon from './swap-step-icon';
3938

4039
export default function AwaitingSignatures() {
4140
const t = useContext(I18nContext);
42-
const history = useHistory();
41+
const navigate = useNavigate();
4342
const dispatch = useDispatch();
4443
const fetchParams = useSelector(getFetchParams, isEqual);
4544
const { destinationTokenInfo, sourceTokenInfo } = fetchParams?.metaData || {};
@@ -149,8 +148,8 @@ export default function AwaitingSignatures() {
149148
await dispatch(prepareToLeaveSwaps());
150149
// Go to the default route and then to the build quote route in order to clean up
151150
// the `inputValue` local state in `pages/swaps/index.js`
152-
history.push(DEFAULT_ROUTE);
153-
history.push(PREPARE_SWAP_ROUTE);
151+
navigate(DEFAULT_ROUTE);
152+
navigate(PREPARE_SWAP_ROUTE);
154153
}}
155154
submitText={t('cancel')}
156155
hideCancel

ui/pages/swaps/awaiting-signatures/awaiting-signatures.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import React from 'react';
22
import configureMockStore from 'redux-mock-store';
3-
4-
import {
5-
renderWithProvider,
6-
createSwapsMockStore,
7-
} from '../../../../test/jest';
3+
import { createSwapsMockStore } from '../../../../test/jest';
4+
import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate';
85
import AwaitingSignatures from '.';
96

107
describe('AwaitingSignatures', () => {

ui/pages/swaps/awaiting-swap/awaiting-swap.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import EventEmitter from 'events';
22
import React, { useContext, useRef, useState, useEffect } from 'react';
33
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
44
import PropTypes from 'prop-types';
5-
import { useHistory } from 'react-router-dom';
5+
import { useNavigate } from 'react-router-dom-v5-compat';
66
import isEqual from 'lodash/isEqual';
77
import { getBlockExplorerLink } from '@metamask/etherscan-link';
88
import { I18nContext } from '../../../contexts/i18n';
@@ -80,7 +80,7 @@ export default function AwaitingSwap({
8080
}) {
8181
const t = useContext(I18nContext);
8282
const trackEvent = useContext(MetaMetricsContext);
83-
const history = useHistory();
83+
const navigate = useNavigate();
8484
const dispatch = useDispatch();
8585
const hdEntropyIndex = useSelector(getHDEntropyIndex);
8686
const animationEventEmitter = useRef(new EventEmitter());
@@ -329,31 +329,31 @@ export default function AwaitingSwap({
329329
/* istanbul ignore next */
330330
if (errorKey === OFFLINE_FOR_MAINTENANCE) {
331331
await dispatch(prepareToLeaveSwaps());
332-
history.push(DEFAULT_ROUTE);
332+
navigate(DEFAULT_ROUTE);
333333
} else if (errorKey === QUOTES_EXPIRED_ERROR) {
334334
dispatch(prepareForRetryGetQuotes());
335335
await dispatch(
336336
fetchQuotesAndSetQuoteState(
337-
history,
337+
navigate,
338338
fromTokenInputValue,
339339
maxSlippage,
340340
trackEvent,
341341
),
342342
);
343343
} else if (errorKey) {
344-
await dispatch(navigateBackToPrepareSwap(history));
344+
await dispatch(navigateBackToPrepareSwap(navigate));
345345
} else if (
346346
isSwapsDefaultTokenSymbol(destinationTokenSymbol, chainId) ||
347347
swapComplete
348348
) {
349-
history.push(DEFAULT_ROUTE);
349+
navigate(DEFAULT_ROUTE);
350350
} else {
351351
await dispatch(setDefaultHomeActiveTabName('activity'));
352-
history.push(DEFAULT_ROUTE);
352+
navigate(DEFAULT_ROUTE);
353353
}
354354
}}
355355
onCancel={async () =>
356-
await dispatch(navigateBackToPrepareSwap(history))
356+
await dispatch(navigateBackToPrepareSwap(navigate))
357357
}
358358
submitText={submitText}
359359
disabled={submittingSwap}

ui/pages/swaps/awaiting-swap/awaiting-swap.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import configureMockStore from 'redux-mock-store';
33
import thunk from 'redux-thunk';
44

55
import { setBackgroundConnection } from '../../../store/background-connection';
6-
import {
7-
renderWithProvider,
8-
createSwapsMockStore,
9-
fireEvent,
10-
} from '../../../../test/jest';
6+
import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate';
7+
import { createSwapsMockStore, fireEvent } from '../../../../test/jest';
118
import {
129
Slippage,
1310
QUOTES_EXPIRED_ERROR,

ui/pages/swaps/create-new-swap/create-new-swap.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, { useContext } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33
import PropTypes from 'prop-types';
4-
import { useHistory } from 'react-router-dom';
4+
import { useNavigate } from 'react-router-dom-v5-compat';
55
import isEqual from 'lodash/isEqual';
6-
76
import Box from '../../../components/ui/box';
87
import { I18nContext } from '../../../contexts/i18n';
98
import { MetaMetricsContext } from '../../../contexts/metametrics';
@@ -24,7 +23,7 @@ export default function CreateNewSwap({ sensitiveTrackingProperties }) {
2423
const trackEvent = useContext(MetaMetricsContext);
2524
const hdEntropyIndex = useSelector(getHDEntropyIndex);
2625
const dispatch = useDispatch();
27-
const history = useHistory();
26+
const navigate = useNavigate();
2827
const defaultSwapsToken = useSelector(getSwapsDefaultToken, isEqual);
2928

3029
return (
@@ -39,8 +38,8 @@ export default function CreateNewSwap({ sensitiveTrackingProperties }) {
3938
hd_entropy_index: hdEntropyIndex,
4039
},
4140
});
42-
history.push(DEFAULT_ROUTE); // It cleans up Swaps state.
43-
await dispatch(navigateBackToPrepareSwap(history));
41+
navigate(DEFAULT_ROUTE); // It cleans up Swaps state.
42+
await dispatch(navigateBackToPrepareSwap(navigate));
4443
dispatch(setSwapsFromToken(defaultSwapsToken));
4544
}}
4645
>

ui/pages/swaps/create-new-swap/create-new-swap.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import configureMockStore from 'redux-mock-store';
33
import thunk from 'redux-thunk';
44

55
import { setBackgroundConnection } from '../../../store/background-connection';
6-
import {
7-
renderWithProvider,
8-
createSwapsMockStore,
9-
fireEvent,
10-
} from '../../../../test/jest';
6+
import { renderWithProvider } from '../../../../test/lib/render-helpers-navigate';
7+
import { createSwapsMockStore, fireEvent } from '../../../../test/jest';
118
import {
129
setSwapsFromToken,
1310
navigateBackToPrepareSwap,

0 commit comments

Comments
 (0)