MyCrypto/common/components/CurrentCustomMessage.tsx
HenryNguyen5 01fc5f1a89 Move Nodes/Networks to Redux (#961)
* Start splitting networks into their own reducers

* Split out nodes and networks into their own reducers

* Cleanup file structure

* Make selectors for new state

* Change custom network typing

* re-type repo

* Fix up components to use selectors, work on fixing sagas

* Provide consistency in naming, fix more sagas

* Get non web3 node switching working

* Split config rehydration off into a different file for store

* Inline auth for custom nodes

* Include typing for app state

* moar selectors

* Get web3 working + cleanup sagas

* Cleanup tsc errors

* Use forof loop instead of foreach for clearing pruning custom networks

* Add reducer tests for new redux state

* Export needed variables

* Add console error

* Remove old comment

* Work on saga tests

* Get passing existing saga tests

* Fix more tests

* Remove irrlevant tests

* add console error

* Get rest of tests passing

* Fix merge errors

* Remove random text

* Fix store saving

* Fix selector lib only grabbing from static nodes

* Fix custom node removal crashing app

* Infer selected network via node

* Prune custom networks properly on node removal

* Infer network name from chainid from selecting state

* Cleanup tsc errors

* Remove MEW nodes for main and testnet
2018-02-12 14:43:07 -06:00

105 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import { AppState } from 'reducers';
import { getCurrentTo, ICurrentTo } from 'selectors/transaction';
import { getAllTokens } from 'selectors/config';
import { getWalletInst } from 'selectors/wallet';
import { getAddressMessage } from 'config';
import { Token } from 'types/network';
interface ReduxProps {
currentTo: ICurrentTo;
tokens: Token[];
wallet: AppState['wallet']['inst'];
}
interface State {
walletAddress: string | null;
}
class CurrentCustomMessageClass extends PureComponent<ReduxProps, State> {
public state: State = {
walletAddress: null
};
public async componentDidMount() {
if (this.props.wallet) {
const walletAddress = await this.props.wallet.getAddressString();
this.setState({ walletAddress });
}
}
public render() {
const message = this.getMessage();
if (message) {
return (
<div className="clearfix form-group">
<div className={`alert alert-${message.severity} col-xs-12`}>{message.message}</div>
</div>
);
} else {
return null;
}
}
private getMessage() {
const { currentTo, tokens } = this.props;
const { walletAddress } = this.state;
// Make sure all comparisons are lower-cased.
const address = currentTo.raw.toLowerCase();
let message;
let severity;
// First check against our hard-coded messages
const msg = getAddressMessage(address);
if (msg) {
message = (
<React.Fragment>
<p>
<small>
A message regarding <strong>{address}</strong>:
</small>
</p>
<p>{msg.msg}</p>
</React.Fragment>
);
severity = msg.severity || 'info';
}
// Otherwise check if any of our tokens match the address
if (!message) {
const token = tokens.find(tk => tk.address.toLowerCase() === address);
if (token) {
message = `
Youre currently sending to the ${token.symbol} contract. If you
wanted to send ${token.symbol} to an address, change the To Address to
where you want it to go, make sure you have a positive ${token.symbol}
balance in your wallet, and select it from the dropdown next to the
Amount field.
`;
severity = 'warning';
}
}
// Finally check if they're sending to themselves (lol)
if (walletAddress === address) {
message = 'Youre sending to yourself. Are you sure you want to do that?';
severity = 'warning';
}
if (message) {
return {
message,
severity
};
}
}
}
export const CurrentCustomMessage = connect((state: AppState): ReduxProps => ({
currentTo: getCurrentTo(state),
tokens: getAllTokens(state),
wallet: getWalletInst(state)
}))(CurrentCustomMessageClass);