2018-01-07 16:43:06 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2018-06-18 01:53:00 +00:00
|
|
|
import BN from 'bn.js';
|
|
|
|
|
|
|
|
import { translateRaw } from 'translations';
|
|
|
|
import { NetworkConfig } from 'types/network';
|
2018-01-29 19:13:46 +00:00
|
|
|
import { Units } from 'libs/units';
|
2018-06-18 01:53:00 +00:00
|
|
|
import { AppState } from 'features/reducers';
|
|
|
|
import { getOffline, getNetworkConfig } from 'features/config';
|
|
|
|
import {
|
|
|
|
transactionFieldsActions,
|
|
|
|
transactionFieldsSelectors,
|
|
|
|
transactionNetworkActions
|
|
|
|
} from 'features/transaction';
|
|
|
|
import { ratesActions } from 'features/rates';
|
2018-01-25 03:43:27 +00:00
|
|
|
import AdvancedGas, { AdvancedOptions } from './components/AdvancedGas';
|
2018-06-18 01:53:00 +00:00
|
|
|
import SimpleGas from './components/SimpleGas';
|
2018-01-25 03:43:27 +00:00
|
|
|
import './TXMetaDataPanel.scss';
|
2018-01-07 16:43:06 +00:00
|
|
|
|
2018-01-25 03:43:27 +00:00
|
|
|
type SliderStates = 'simple' | 'advanced';
|
|
|
|
|
2018-01-15 09:59:59 +00:00
|
|
|
interface StateProps {
|
2018-01-07 16:43:06 +00:00
|
|
|
gasPrice: AppState['transaction']['fields']['gasPrice'];
|
2018-02-12 20:43:07 +00:00
|
|
|
offline: AppState['config']['meta']['offline'];
|
|
|
|
network: NetworkConfig;
|
2018-01-15 09:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface DispatchProps {
|
2018-06-18 01:53:00 +00:00
|
|
|
inputGasPrice: transactionFieldsActions.TInputGasPrice;
|
|
|
|
inputGasPriceIntent: transactionFieldsActions.TInputGasPriceIntent;
|
|
|
|
fetchCCRates: ratesActions.TFetchCCRatesRequested;
|
|
|
|
getNonceRequested: transactionNetworkActions.TGetNonceRequested;
|
|
|
|
resetTransactionRequested: transactionFieldsActions.TResetTransactionRequested;
|
2018-01-25 03:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set default props for props that can't be truthy or falsy
|
|
|
|
interface DefaultProps {
|
|
|
|
initialState: SliderStates;
|
2018-01-07 16:43:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 09:59:59 +00:00
|
|
|
interface OwnProps {
|
2018-01-25 03:43:27 +00:00
|
|
|
initialState?: SliderStates;
|
|
|
|
disableToggle?: boolean;
|
|
|
|
advancedGasOptions?: AdvancedOptions;
|
|
|
|
className?: string;
|
2018-04-14 22:21:33 +00:00
|
|
|
scheduling?: boolean;
|
2018-01-15 09:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Props = DispatchProps & OwnProps & StateProps;
|
|
|
|
|
2018-01-07 16:43:06 +00:00
|
|
|
interface State {
|
2018-01-29 19:13:46 +00:00
|
|
|
gasPrice: AppState['transaction']['fields']['gasPrice'];
|
2018-01-25 03:43:27 +00:00
|
|
|
sliderState: SliderStates;
|
2018-01-07 16:43:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:43:27 +00:00
|
|
|
class TXMetaDataPanel extends React.Component<Props, State> {
|
|
|
|
public static defaultProps: DefaultProps = {
|
|
|
|
initialState: 'simple'
|
|
|
|
};
|
|
|
|
|
2018-01-07 16:43:06 +00:00
|
|
|
public state: State = {
|
2018-01-29 19:13:46 +00:00
|
|
|
gasPrice: this.props.gasPrice,
|
2018-01-25 03:43:27 +00:00
|
|
|
sliderState: (this.props as DefaultProps).initialState
|
2018-01-07 16:43:06 +00:00
|
|
|
};
|
|
|
|
|
2018-06-11 21:53:49 +00:00
|
|
|
public componentWillMount() {
|
2018-01-11 18:04:11 +00:00
|
|
|
if (!this.props.offline) {
|
2018-04-23 23:35:24 +00:00
|
|
|
this.props.resetTransactionRequested();
|
2018-06-11 21:53:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
if (!this.props.offline) {
|
2018-01-11 18:04:11 +00:00
|
|
|
this.props.fetchCCRates([this.props.network.unit]);
|
2018-01-25 03:43:27 +00:00
|
|
|
this.props.getNonceRequested();
|
2018-01-11 18:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 02:34:27 +00:00
|
|
|
public UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
2018-02-25 01:32:34 +00:00
|
|
|
if (
|
|
|
|
(this.props.offline && !nextProps.offline) ||
|
|
|
|
this.props.network.unit !== nextProps.network.unit
|
|
|
|
) {
|
2018-01-11 18:04:11 +00:00
|
|
|
this.props.fetchCCRates([this.props.network.unit]);
|
|
|
|
}
|
2018-01-29 19:13:46 +00:00
|
|
|
if (this.props.gasPrice !== nextProps.gasPrice) {
|
|
|
|
this.setState({ gasPrice: nextProps.gasPrice });
|
|
|
|
}
|
2018-01-07 16:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2018-04-14 22:21:33 +00:00
|
|
|
const { offline, disableToggle, advancedGasOptions, className = '', scheduling } = this.props;
|
2018-01-29 19:13:46 +00:00
|
|
|
const { gasPrice } = this.state;
|
2018-01-25 03:43:27 +00:00
|
|
|
const showAdvanced = this.state.sliderState === 'advanced' || offline;
|
2018-04-14 22:21:33 +00:00
|
|
|
|
2018-01-07 16:43:06 +00:00
|
|
|
return (
|
2018-01-25 03:43:27 +00:00
|
|
|
<div className={`Gas col-md-12 ${className}`}>
|
2018-04-14 22:21:33 +00:00
|
|
|
<br />
|
2018-01-07 16:43:06 +00:00
|
|
|
{showAdvanced ? (
|
2018-01-25 03:43:27 +00:00
|
|
|
<AdvancedGas
|
|
|
|
gasPrice={gasPrice}
|
|
|
|
inputGasPrice={this.props.inputGasPrice}
|
|
|
|
options={advancedGasOptions}
|
2018-04-14 22:21:33 +00:00
|
|
|
scheduling={scheduling}
|
2018-01-25 03:43:27 +00:00
|
|
|
/>
|
2018-01-07 16:43:06 +00:00
|
|
|
) : (
|
2018-02-05 23:14:23 +00:00
|
|
|
<SimpleGas
|
|
|
|
gasPrice={gasPrice}
|
|
|
|
inputGasPrice={this.handleGasPriceInput}
|
|
|
|
setGasPrice={this.props.inputGasPrice}
|
|
|
|
/>
|
2018-01-07 16:43:06 +00:00
|
|
|
)}
|
|
|
|
|
|
|
|
{!offline &&
|
2018-01-25 03:43:27 +00:00
|
|
|
!disableToggle && (
|
2018-01-07 16:43:06 +00:00
|
|
|
<div className="help-block">
|
2018-01-25 03:43:27 +00:00
|
|
|
<a className="Gas-toggle" onClick={this.toggleAdvanced}>
|
2018-03-01 17:53:29 +00:00
|
|
|
{showAdvanced
|
2018-03-22 03:50:25 +00:00
|
|
|
? `- ${translateRaw('TRANS_SIMPLE')}`
|
|
|
|
: `+ ${translateRaw('TRANS_ADVANCED')}`}
|
2018-01-07 16:43:06 +00:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private toggleAdvanced = () => {
|
2018-01-25 03:43:27 +00:00
|
|
|
this.setState({ sliderState: this.state.sliderState === 'advanced' ? 'simple' : 'advanced' });
|
2018-01-07 16:43:06 +00:00
|
|
|
};
|
2018-01-29 19:13:46 +00:00
|
|
|
|
|
|
|
private handleGasPriceInput = (raw: string) => {
|
2018-02-24 18:00:00 +00:00
|
|
|
// Realistically, we're not going to end up with a > 32 bit int, so it's
|
|
|
|
// safe to cast to float, multiply by gwei units, then big number, since
|
|
|
|
// some of the inputs may be sub-one float values.
|
|
|
|
const value = new BN(parseFloat(raw) * parseFloat(Units.gwei));
|
2018-01-29 19:13:46 +00:00
|
|
|
this.setState({
|
|
|
|
gasPrice: { raw, value }
|
|
|
|
});
|
|
|
|
this.props.inputGasPriceIntent(raw);
|
|
|
|
};
|
2018-01-07 16:43:06 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 09:59:59 +00:00
|
|
|
function mapStateToProps(state: AppState): StateProps {
|
2018-01-07 16:43:06 +00:00
|
|
|
return {
|
2018-06-18 01:53:00 +00:00
|
|
|
gasPrice: transactionFieldsSelectors.getGasPrice(state),
|
2018-01-15 09:59:59 +00:00
|
|
|
offline: getOffline(state),
|
2018-01-07 16:43:06 +00:00
|
|
|
network: getNetworkConfig(state)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, {
|
2018-06-18 01:53:00 +00:00
|
|
|
inputGasPrice: transactionFieldsActions.inputGasPrice,
|
|
|
|
inputGasPriceIntent: transactionFieldsActions.inputGasPriceIntent,
|
|
|
|
fetchCCRates: ratesActions.fetchCCRatesRequested,
|
|
|
|
getNonceRequested: transactionNetworkActions.getNonceRequested,
|
|
|
|
resetTransactionRequested: transactionFieldsActions.resetTransactionRequested
|
2018-01-25 03:43:27 +00:00
|
|
|
})(TXMetaDataPanel);
|