Files

159 lines
4.8 KiB
TypeScript
Raw Permalink Normal View History

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