Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent a user from switching to a custom network which fails #5330

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ module.exports = {
'src/modules/pos/validator/components/ClaimRewardsView/index.js',
'src/modules/blockchainApplication/manage/hooks/useApplicationManagement.js',
'src/modules/wallet/components/MenuSelect/MenuSelect.js',
'src/modules/network/components/networkSwitcherDropdown/networkMenuItem/NetworkMenuItem.js',
'src/modules/network/components/networkSwitcherDropdown/NetworkSwitcherDropdown.js',
'src/modules/blockchainApplication/manage/components/UserApplicationSelector/UserApplicationSelector.js',
'src/modules/network/components/selectNetwork/SelectNetwork.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ useApplicationManagement.mockReturnValue({
useSettings.mockReturnValue({
customNetworks: [],
mainChainNetwork: networks[DEFAULT_NETWORK],
setValue: jest.fn(),
});

describe('ModalNetworkApplicationSelector', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,6 @@
color: var(--color-zodiac-blue);
margin-bottom: 16px;
}

& .networkItem {
font-weight: var(--font-weight-normal) !important;
font-size: var(--font-size-h6);
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 10px 16px !important;
color: var(--color-content-light) !important;
width: calc(100% - 32px) !important;
border-radius: 8px !important;

&.selected {
background-color: var(--color-network-selected-background);
}

& .networkIcons {
& > :not(:last-child) {
margin-right: 10px;
}

& .modifyIcons {
display: none;
}
}

&:hover {
& .modifyIcons {
display: inline;
cursor: pointer;
vertical-align: top;
}
}
}
}

& .connectionFailedBlock {
Expand Down Expand Up @@ -95,7 +60,7 @@
}
}

.menuSelect .networkItem {
.menuSelect .networkItemProp {
font-family: var(--content-font) !important;
background-color: unset !important;
font-weight: var(--font-weight-normal);
Expand Down
eniolam1000752 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also the issue of residual persisted network/application information lingering around the local storage even though they have been removed.

Below is a list of all the networks which I don't even see in my network list

image image

Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/* eslint-disable complexity, max-statements */
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { useHistory } from 'react-router-dom';
import { useSelector, useDispatch } from 'react-redux';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import MenuSelect, { MenuItem } from '@wallet/components/MenuSelect';
import MenuSelect from '@wallet/components/MenuSelect';
import Icon from '@theme/Icon';
import useSettings from '@settings/hooks/useSettings';
import { Client } from 'src/utils/api/client';
import DialogLink from '@theme/dialog/link';
import { selectStaking } from 'src/redux/selectors';
import { stakesReset } from 'src/redux/actions';
import {
removeThenAppendSearchParamsToUrl,
removeSearchParamsFromUrl,
removeThenAppendSearchParamsToUrl,
} from 'src/utils/searchParams';
import { createConfirmSwitchState } from '@common/utils/createConfirmSwitchState';
import stylesSecondaryButton from '@theme/buttons/css/secondaryButton.css';
import classNames from 'classnames';
import NetworkMenuItem from '@network/components/networkSwitcherDropdown/networkMenuItem/NetworkMenuItem';
import networks from '../../configuration/networks';
import { useNetworkStatus } from '../../hooks/queries';
import styles from './NetworkSwitcherDropdown.css';
Expand Down Expand Up @@ -52,15 +53,21 @@ function NetworkSwitcherDropdown({ noLabel, onNetworkSwitchSuccess }) {
queryClient.current.create({
http: network.serviceUrl,
});
networkStatus.refetch().then((res) => {
setSelectedNetwork(network);
},
[networkStatus]
);

useEffect(() => {
if (selectedNetwork.serviceUrl) {
networkStatus?.refetch?.()?.then((res) => {
eniolam1000752 marked this conversation as resolved.
Show resolved Hide resolved
if (!res.error) {
// clear stakes list during network switch
if (pendingStakes.length) {
const onCancel = /* istanbul ignore next */ () =>
removeSearchParamsFromUrl(history, ['modal']);
const onConfirm = /* istanbul ignore next */ () => {
setValue(network);
setSelectedNetwork(network);
setValue(selectedNetwork);

dispatch(stakesReset());
removeSearchParamsFromUrl(history, ['modal']);
Expand All @@ -78,25 +85,12 @@ function NetworkSwitcherDropdown({ noLabel, onNetworkSwitchSuccess }) {
state
);
} else {
setValue(network);
setSelectedNetwork(network);
setValue(selectedNetwork);
}
}
});
},
[networkStatus]
);

const editCustomNetwork = (e, data) => {
e.stopPropagation();
removeThenAppendSearchParamsToUrl(history, { modal: 'dialogAddNetwork', ...data }, ['modal']);
};
const deleteCustomNetwork = (e, data) => {
e.stopPropagation();
removeThenAppendSearchParamsToUrl(history, { modal: 'dialogRemoveNetwork', ...data }, [
'modal',
]);
};
}
}, [selectedNetwork.serviceUrl]);

useEffect(() => {
const isSuccess = networkStatus.isSuccess && !networkStatus.isFetching;
Expand All @@ -109,7 +103,7 @@ function NetworkSwitcherDropdown({ noLabel, onNetworkSwitchSuccess }) {
{!noLabel && <label className={styles.label}>{t('Switch network')}</label>}
<MenuSelect
value={selectedNetwork}
select={(selectedValue, option) => selectedValue.label === option.label}
select={(selectedValue, option) => selectedValue.label === option?.label}
onChange={handleChangeNetwork}
popupClassName={styles.networksPopup}
className={styles.menuSelect}
Expand All @@ -120,48 +114,15 @@ function NetworkSwitcherDropdown({ noLabel, onNetworkSwitchSuccess }) {
.filter((networkKey) => networksWithCustomNetworks[networkKey].isAvailable)
.map((networkKey) => {
const network = networksWithCustomNetworks[networkKey];

return (
<MenuItem
className={`${styles.networkItem} ${
selectedNetwork.label === network.label ? styles.selected : ''
}`}
value={network}
<NetworkMenuItem
className={styles.networkItemProp}
key={network.label}
>
<span>
<span>{network.label}</span>
</span>
<span className={styles.networkIcons}>
{network.isCustom && (
<>
<span
onClick={(e) =>
editCustomNetwork(e, {
name: network.name,
serviceUrl: network.serviceUrl,
})
}
>
<Icon name="edit" className={styles.modifyIcons} />
</span>
{currentNetworkName !== network.name && (
<span
onClick={(e) =>
deleteCustomNetwork(e, {
name: network.name,
serviceUrl: network.serviceUrl,
})
}
>
<Icon name="deleteIcon" className={styles.modifyIcons} />
</span>
)}
</>
)}
{selectedNetwork.label === network.label && <Icon name="okIcon" />}
</span>
</MenuItem>
selectedNetwork={selectedNetwork}
currentNetworkName={currentNetworkName}
value={network}
isSelected={network.label === selectedNetwork.label}
/>
);
})}
</MenuSelect>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@import '../../../../../../setup/react/app/mixins.css';

.NetworkMenuItem {
font-weight: var(--font-weight-normal) !important;
font-size: var(--font-size-h6) !important;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
padding: 10px 16px !important;
color: var(--color-content-light) !important;
width: calc(100% - 32px) !important;
border-radius: 8px !important;

&.selected {
background-color: var(--color-network-selected-background) !important;
}

& .networkIcons {
& > :not(:last-child) {
margin-right: 10px;
}

& .modifyIcons {
display: none;
}
}

&:hover {
& .modifyIcons {
display: inline;
cursor: pointer;
vertical-align: top;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from 'react';
import { MenuItem } from '@wallet/components/MenuSelect';
import Icon from '@theme/Icon';
import { removeThenAppendSearchParamsToUrl } from 'src/utils/searchParams';
import { useHistory } from 'react-router-dom';
import classNames from 'classnames';
import styles from './NetworkMenuItem.css';

function NetworkMenuItem({ className, value, currentNetworkName, isSelected }) {
const history = useHistory();

const editCustomNetwork = (e, data) => {
e.stopPropagation();
removeThenAppendSearchParamsToUrl(history, { modal: 'dialogAddNetwork', ...data }, ['modal']);
};
const deleteCustomNetwork = (e, data) => {
e.stopPropagation();
removeThenAppendSearchParamsToUrl(history, { modal: 'dialogRemoveNetwork', ...data }, [
'modal',
]);
};

return (
<MenuItem
className={classNames(styles.NetworkMenuItem, className, {
[styles.selected]: isSelected,
})}
value={value}
key={value.label}
>
<span>
<span>{value.label}</span>
</span>
<span className={styles.networkIcons}>
{value.isCustom && (
<>
<span
onClick={(e) =>
editCustomNetwork(e, {
name: value.name,
serviceUrl: value.serviceUrl,
})
}
>
<Icon name="edit" className={styles.modifyIcons} />
</span>
{currentNetworkName !== value.name && (
<span
onClick={(e) =>
deleteCustomNetwork(e, {
name: value.name,
serviceUrl: value.serviceUrl,
})
}
>
<Icon name="deleteIcon" className={styles.modifyIcons} />
</span>
)}
</>
)}
{isSelected && <Icon name="okIcon" />}
</span>
</MenuItem>
);
}

export default NetworkMenuItem;