Skip to content

Commit

Permalink
[FIX] Update selected network when delete network manually inserted (M…
Browse files Browse the repository at this point in the history
…etaMask#5219)

* comparing two rpc urls now working

* changed comparing rpc host to sanitize the url for cover more rpc cases

* network remove same behaviour

* update tests
  • Loading branch information
tommasini authored Dec 28, 2022
1 parent fc9dd26 commit e94e0f0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ exports[`NetworkSettings should render correctly 1`] = `
},
]
}
provider={
Object {
"rpcTarget": "http://10.0.2.2:8545",
"type": "mainnet",
}
}
showNetworkOnboardingAction={[Function]}
thirdPartyApiMode={true}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ import {
DEFAULT_MAINNET_CUSTOM_NAME,
MAINNET,
PRIVATENETWORK,
RPC,
} from '../../../../../constants/network';
import { ThemeContext, mockTheme } from '../../../../../util/theme';
import { showNetworkOnboardingAction } from '../../../../../actions/onboardNetwork';
import sanitizeUrl from '../../../../../util/sanitizeUrl';
import sanitizeUrl, {
compareSanitizedUrl,
} from '../../../../../util/sanitizeUrl';
import {
ADD_NETWORKS_ID,
RPC_VIEW_CONTAINER_ID,
Expand Down Expand Up @@ -231,6 +234,10 @@ class NetworkSettings extends PureComponent {
* Checks if adding custom mainnet.
*/
isCustomMainnet: PropTypes.bool,
/**
* NetworkController povider object
*/
provider: PropTypes.object,
};

state = {
Expand Down Expand Up @@ -752,10 +759,16 @@ class NetworkSettings extends PureComponent {
};

removeRpcUrl = () => {
const { navigation } = this.props;
this.switchToMainnet();
const { navigation, provider } = this.props;
const { rpcUrl } = this.state;
if (
compareSanitizedUrl(rpcUrl, provider.rpcTarget) &&
provider.type === RPC
) {
this.switchToMainnet();
}
const { PreferencesController } = Engine.context;
PreferencesController.removeFromFrequentRpcList(this.state.rpcUrl);
PreferencesController.removeFromFrequentRpcList(rpcUrl);
navigation.goBack();
};

Expand Down Expand Up @@ -1089,6 +1102,7 @@ const mapDispatchToProps = (dispatch) => ({
});

const mapStateToProps = (state) => ({
provider: state.engine.backgroundState.NetworkController.provider,
frequentRpcList:
state.engine.backgroundState.PreferencesController.frequentRpcList,
networkOnboardedState: state.networkOnboarded.networkOnboardedState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const mockStore = configureMockStore();
const initialState = {
engine: {
backgroundState: {
NetworkController: {
provider: { type: 'mainnet', rpcTarget: 'http://10.0.2.2:8545' },
},
PreferencesController: {
frequentRpcList: [],
},
Expand Down
6 changes: 5 additions & 1 deletion app/components/Views/Settings/NetworksSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import FontAwesome from 'react-native-vector-icons/FontAwesome';
import { ThemeContext, mockTheme } from '../../../../util/theme';
import ImageIcons from '../../../UI/ImageIcon';
import { ADD_NETWORK_BUTTON } from '../../../../../wdio/features/testIDs/Screens/NetworksScreen.testids';
import { compareSanitizedUrl } from '../../../../util/sanitizeUrl';

const createStyles = (colors) =>
StyleSheet.create({
Expand Down Expand Up @@ -175,7 +176,10 @@ class NetworksSettings extends PureComponent {
removeNetwork = () => {
// Check if it's the selected network and then switch to mainnet first
const { provider } = this.props;
if (provider.rpcTarget === this.networkToRemove && provider.type === RPC) {
if (
compareSanitizedUrl(provider.rpcTarget, this.networkToRemove) &&
provider.type === RPC
) {
this.switchToMainnet();
}
const { PreferencesController } = Engine.context;
Expand Down
3 changes: 3 additions & 0 deletions app/util/sanitizeUrl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const sanitizeUrl = (url: string | undefined) => url?.replace(/\/$/, '');

export default sanitizeUrl;

export const compareSanitizedUrl = (urlOne: string, urlTwo: string) =>
sanitizeUrl(urlOne) === sanitizeUrl(urlTwo);

0 comments on commit e94e0f0

Please sign in to comment.