Skip to content

Commit

Permalink
H #2541 Add ability to automatically switch network in MetaMask when …
Browse files Browse the repository at this point in the history
…user is on wrong network
  • Loading branch information
RamRamez committed Sep 22, 2021
1 parent 5ed5652 commit 165d794
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 218 deletions.
Binary file removed public/img/change-network.gif
Binary file not shown.
47 changes: 38 additions & 9 deletions src/components/ActionNetworkWarning.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
import React from 'react';
import PropTypes from 'prop-types';
import NetworkChangeGuide from './NetworkChangeGuide';
import { Button } from 'antd';
import Web3 from 'web3';
import config from '../configuration';

/**
* It is used to show in modals when user wants to take an action while connected to wrong network.
*/

function ActionNetworkWarning(props) {
const { incorrectNetwork, networkName } = props;
const { incorrectNetwork, networkName, switchNetwork, web3, isInline } = props;
const isMetaMask = web3 && web3.MetaMask;

const switchNetworkBtn = () =>
// Just MetaMask supports changing network
isMetaMask && (
<div className="my-2">
<Button type="primary" ghost className="ml-4 rounded" onClick={switchNetwork}>
Switch Network
</Button>
</div>
);

return (
incorrectNetwork && (
<div>
<div className="alert alert-warning">
<i className="fa fa-exclamation-triangle" />
To take this action you need to connect metamask to <strong>{networkName}</strong>{' '}
network. Then try again.
<div className={isInline ? '' : 'text-center'}>
<div className="alert alert-warning d-flex flex-wrap align-items-center">
<div className="d-flex align-items-center">
<i className="fa fa-exclamation-triangle mr-3" />
<div>
To enable all actions, please connect your wallet to <strong>{networkName}</strong>{' '}
network.
</div>
</div>
{isInline && switchNetworkBtn()}
</div>
{NetworkChangeGuide}
{!isInline && switchNetworkBtn()}
</div>
)
);
}

ActionNetworkWarning.propTypes = {
incorrectNetwork: PropTypes.bool.isRequired,
networkName: PropTypes.string.isRequired,
networkName: PropTypes.string,
switchNetwork: PropTypes.func,
web3: PropTypes.instanceOf(Web3),
isInline: PropTypes.bool,
};

ActionNetworkWarning.defaultProps = {
switchNetwork: () => {},
web3: undefined,
isInline: false,
networkName: config.foreignNetworkName,
};

export default ActionNetworkWarning;
19 changes: 11 additions & 8 deletions src/components/ChangeOwnershipButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Button, Modal, Select, Form, notification } from 'antd';
import Campaign from 'models/Campaign';
import { checkBalance, authenticateUser } from '../lib/middleware';
import { ZERO_ADDRESS } from '../lib/helpers';
import config from '../configuration';
import ActionNetworkWarning from './ActionNetworkWarning';

import CampaignService from '../services/CampaignService';
Expand All @@ -31,6 +30,7 @@ const noCoownerOption = {
const ChangeOwnershipButton = props => {
const {
state: { balance, isForeignNetwork, validProvider, web3 },
actions: { switchNetwork },
} = useContext(Web3Context);
const {
state: { currentUser },
Expand Down Expand Up @@ -103,16 +103,19 @@ const ChangeOwnershipButton = props => {
</div>
)}
{validProvider && (
<ActionNetworkWarning
incorrectNetwork={!isForeignNetwork}
networkName={config.foreignNetworkName}
/>
)}
{isForeignNetwork && (
<Form onFinish={submit}>
<Fragment>
<p>
Changing ownership for<strong> {campaign.title}</strong>
</p>
<ActionNetworkWarning
incorrectNetwork={!isForeignNetwork}
switchNetwork={switchNetwork}
web3={web3}
/>
</Fragment>
)}
{isForeignNetwork && (
<Form onFinish={submit}>
<div className="label mb-2">Select a new co-owner:</div>
{formIsReady && (
<Fragment>
Expand Down
11 changes: 2 additions & 9 deletions src/components/DelegateMultipleButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ const modalStyles = {
*/
const DelegateMultipleButton = props => {
const {
state: { isForeignNetwork, validProvider, balance, web3 },
actions: { displayForeignNetRequiredWarning },
state: { balance, web3 },
} = useContext(Web3Context);

const {
Expand All @@ -49,13 +48,7 @@ const DelegateMultipleButton = props => {
<Fragment>
<Button
className="ant-btn-delegate"
onClick={() => {
if (validProvider && !isForeignNetwork) {
displayForeignNetRequiredWarning();
} else {
openDialog().then();
}
}}
onClick={openDialog}
block
size={props.size}
style={props.style}
Expand Down
32 changes: 19 additions & 13 deletions src/components/DelegateMultipleButtonModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const ModalContent = props => {
} = useContext(UserContext);
const {
state: { isForeignNetwork, validProvider, isEnabled: Web3ContextIsEnabled, web3 },
actions: { switchNetwork },
} = useContext(Web3Context);
const {
actions: { delegationPending, delegationSuccessful, delegationFailed },
Expand Down Expand Up @@ -340,11 +341,6 @@ const ModalContent = props => {

const modalContent = (
<div id="delegate-multiple-modal">
<p>
You are delegating donations to
{!trace && <strong> {campaign.title}</strong>}
{trace && <strong> {trace.title}</strong>}
</p>
<Fragment>
{isLimitedDelegateCount() && (
<div className="alert alert-warning">
Expand Down Expand Up @@ -488,22 +484,32 @@ const ModalContent = props => {
const isContextReady =
!whiteListIsLoading && !userContextIsLoading && Web3ContextIsEnabled && isCommunitiesFetched;

const modalBody = isContextReady ? modalContent : modalLoading;

return (
<React.Fragment>
<Fragment>
{!validProvider && (
<div className="alert alert-warning">
<i className="fa fa-exclamation-triangle" />
It is recommended that you install <a href="https://metamask.io/">MetaMask</a> to donate
</div>
)}
{validProvider && (
<ActionNetworkWarning
incorrectNetwork={!isForeignNetwork}
networkName={config.foreignNetworkName}
/>
)}{' '}
{isContextReady ? validProvider && isForeignNetwork && modalContent : modalLoading}
</React.Fragment>
<Fragment>
<p>
You are delegating donations to
{!trace && <strong> {campaign.title}</strong>}
{trace && <strong> {trace.title}</strong>}
</p>
<ActionNetworkWarning
incorrectNetwork={!isForeignNetwork}
switchNetwork={switchNetwork}
web3={web3}
/>
</Fragment>
)}
{isForeignNetwork && modalBody}
</Fragment>
);
};

Expand Down
3 changes: 3 additions & 0 deletions src/components/DonateButtonModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const DonateButtonModal = props => {
} = useContext(UserContext);
const {
state: { isHomeNetwork, validProvider, balance: NativeTokenBalance, web3 },
actions: { switchNetwork },
} = useContext(Web3Context);
const {
actions: { donationPending, donationSuccessful, donationFailed },
Expand Down Expand Up @@ -690,6 +691,8 @@ const DonateButtonModal = props => {
<ActionNetworkWarning
incorrectNetwork={!isCorrectNetwork}
networkName={config.homeNetworkName}
switchNetwork={switchNetwork}
web3={web3}
/>
)}

Expand Down
9 changes: 0 additions & 9 deletions src/components/NetworkChangeGuide.jsx

This file was deleted.

32 changes: 9 additions & 23 deletions src/components/NetworkWarningModal.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Button, Modal } from 'antd';
import Web3 from 'web3';
import ActionNetworkWarning from './ActionNetworkWarning';
import config from '../configuration';

const NetworkWarningModal = props => {
const { show, closeModal, networkName } = props;
const { show, closeModal, switchNetwork, web3 } = props;
return (
<Modal
visible={show}
Expand All @@ -19,35 +19,21 @@ const NetworkWarningModal = props => {
</Button>,
]}
>
<div>
<ActionNetworkWarning incorrectNetwork networkName={networkName} />
</div>
<ActionNetworkWarning incorrectNetwork switchNetwork={switchNetwork} web3={web3} />
</Modal>
);
};

NetworkWarningModal.propTypes = {
show: PropTypes.bool.isRequired,
closeModal: PropTypes.func.isRequired,
networkName: PropTypes.string.isRequired,
switchNetwork: PropTypes.func,
web3: PropTypes.instanceOf(Web3),
};

const ForeignRequiredModal = props => {
return <NetworkWarningModal networkName={config.foreignNetworkName} {...props} />;
};
const HomeRequiredModal = props => {
return <NetworkWarningModal networkName={config.foreignNetworkName} {...props} />;
};

ForeignRequiredModal.propTypes = {
show: PropTypes.bool.isRequired,
closeModal: PropTypes.func.isRequired,
buttonLabel: PropTypes.string.isRequired,
};
HomeRequiredModal.propTypes = {
show: PropTypes.bool.isRequired,
closeModal: PropTypes.func.isRequired,
buttonLabel: PropTypes.string.isRequired,
NetworkWarningModal.defaultProps = {
switchNetwork: () => {},
web3: undefined,
};

export { ForeignRequiredModal, HomeRequiredModal };
export default NetworkWarningModal;
60 changes: 0 additions & 60 deletions src/components/ViewNetworkWarning.jsx

This file was deleted.

17 changes: 9 additions & 8 deletions src/components/views/MyCampaigns.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import React, { Fragment, useCallback, useContext, useEffect, useRef, useState } from 'react';
import { Link } from 'react-router-dom';
import Pagination from 'react-js-pagination';

import ViewNetworkWarning from 'components/ViewNetworkWarning';
import { Context as Web3Context } from 'contextProviders/Web3Provider';
import config from 'configuration';
import { Helmet } from 'react-helmet';

import { Context as Web3Context } from '../../contextProviders/Web3Provider';
import { Context as UserContext } from '../../contextProviders/UserProvider';
import Loader from '../Loader';
import { convertEthHelper, getTruncatedText } from '../../lib/helpers';
import CampaignService from '../../services/CampaignService';
import Campaign from '../../models/Campaign';
import AuthenticationWarning from '../AuthenticationWarning';
import { Context as UserContext } from '../../contextProviders/UserProvider';
import CancelCampaignButton from '../CancelCampaignButton';
import EditCampaignButton from '../EditCampaignButton';
import ErrorHandler from '../../lib/ErrorHandler';
import Web3ConnectWarning from '../Web3ConnectWarning';
import ActionNetworkWarning from '../ActionNetworkWarning';

/**
* The my campaings view
Expand All @@ -26,7 +24,8 @@ function MyCampaigns() {
state: { currentUser },
} = useContext(UserContext);
const {
state: { isForeignNetwork },
state: { isForeignNetwork, web3 },
actions: { switchNetwork },
} = useContext(Web3Context);

const [isLoading, setLoading] = useState(true);
Expand Down Expand Up @@ -107,9 +106,11 @@ function MyCampaigns() {

<AuthenticationWarning />

<ViewNetworkWarning
<ActionNetworkWarning
incorrectNetwork={!isForeignNetwork}
networkName={config.foreignNetworkName}
switchNetwork={switchNetwork}
web3={web3}
isInline
/>

{isLoading && <Loader className="fixed" />}
Expand Down
Loading

0 comments on commit 165d794

Please sign in to comment.