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

[FIX] Update selected network when delete network manually inserted #5219

Merged
merged 6 commits into from
Dec 28, 2022
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
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);