2018-01-07 16:43:06 +00:00
|
|
|
import React from 'react';
|
2018-01-29 19:13:46 +00:00
|
|
|
import BN from 'bn.js';
|
2018-01-07 16:43:06 +00:00
|
|
|
import { translateRaw } from 'translations';
|
|
|
|
import { connect } from 'react-redux';
|
2018-01-25 03:43:27 +00:00
|
|
|
import {
|
|
|
|
inputGasPrice,
|
|
|
|
TInputGasPrice,
|
2018-01-29 19:13:46 +00:00
|
|
|
inputGasPriceIntent,
|
|
|
|
TInputGasPriceIntent,
|
2018-01-25 03:43:27 +00:00
|
|
|
getNonceRequested,
|
|
|
|
TGetNonceRequested,
|
|
|
|
reset,
|
2018-03-02 21:57:17 +00:00
|
|
|
TReset,
|
|
|
|
ResetAction
|
2018-01-25 03:43:27 +00:00
|
|
|
} from 'actions/transaction';
|
2018-02-07 23:59:55 +00:00
|
|
|
import { fetchCCRatesRequested, TFetchCCRatesRequested } from 'actions/rates';
|
2018-01-15 09:59:59 +00:00
|
|
|
import { getNetworkConfig, getOffline } from 'selectors/config';
|
2018-01-07 16:43:06 +00:00
|
|
|
import { AppState } from 'reducers';
|
2018-01-29 19:13:46 +00:00
|
|
|
import { Units } from 'libs/units';
|
2018-01-07 16:43:06 +00:00
|
|
|
import SimpleGas from './components/SimpleGas';
|
2018-01-25 03:43:27 +00:00
|
|
|
import AdvancedGas, { AdvancedOptions } from './components/AdvancedGas';
|
|
|
|
import './TXMetaDataPanel.scss';
|
2018-01-15 09:59:59 +00:00
|
|
|
import { getGasPrice } from 'selectors/transaction';
|
2018-02-12 20:43:07 +00:00
|
|
|
import { NetworkConfig } from 'types/network';
|
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-01-07 16:43:06 +00:00
|
|
|
inputGasPrice: TInputGasPrice;
|
2018-01-29 19:13:46 +00:00
|
|
|
inputGasPriceIntent: TInputGasPriceIntent;
|
2018-02-07 23:59:55 +00:00
|
|
|
fetchCCRates: TFetchCCRatesRequested;
|
2018-01-25 03:43:27 +00:00
|
|
|
getNonceRequested: TGetNonceRequested;
|
|
|
|
reset: TReset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-02 21:57:17 +00:00
|
|
|
resetIncludeExcludeProperties?: ResetAction['payload'];
|
2018-01-25 03:43:27 +00:00
|
|
|
initialState?: SliderStates;
|
|
|
|
disableToggle?: boolean;
|
|
|
|
advancedGasOptions?: AdvancedOptions;
|
|
|
|
className?: string;
|
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
|
|
|
};
|
|
|
|
|
|
|
|
public componentDidMount() {
|
2018-01-11 18:04:11 +00:00
|
|
|
if (!this.props.offline) {
|
2018-03-02 21:57:17 +00:00
|
|
|
this.props.reset(this.props.resetIncludeExcludeProperties);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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-01-29 19:13:46 +00:00
|
|
|
const { offline, disableToggle, advancedGasOptions, className = '' } = this.props;
|
|
|
|
const { gasPrice } = this.state;
|
2018-01-25 03:43:27 +00:00
|
|
|
const showAdvanced = this.state.sliderState === 'advanced' || offline;
|
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-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-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
|
|
|
|
? `- ${translateRaw('Back to simple')}`
|
|
|
|
: `+ ${translateRaw('Advanced Settings')}`}
|
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-01-15 09:59:59 +00:00
|
|
|
gasPrice: getGasPrice(state),
|
|
|
|
offline: getOffline(state),
|
2018-01-07 16:43:06 +00:00
|
|
|
network: getNetworkConfig(state)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, {
|
|
|
|
inputGasPrice,
|
2018-01-29 19:13:46 +00:00
|
|
|
inputGasPriceIntent,
|
2018-02-07 23:59:55 +00:00
|
|
|
fetchCCRates: fetchCCRatesRequested,
|
2018-01-25 03:43:27 +00:00
|
|
|
getNonceRequested,
|
|
|
|
reset
|
|
|
|
})(TXMetaDataPanel);
|