2018-01-07 16:43:06 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { translateRaw } from 'translations';
|
|
|
|
import { connect } from 'react-redux';
|
2018-01-25 03:43:27 +00:00
|
|
|
import {
|
|
|
|
inputGasPrice,
|
|
|
|
TInputGasPrice,
|
|
|
|
getNonceRequested,
|
|
|
|
TGetNonceRequested,
|
|
|
|
reset,
|
|
|
|
TReset
|
|
|
|
} from 'actions/transaction';
|
2018-01-07 16:43:06 +00:00
|
|
|
import { fetchCCRates, TFetchCCRates } 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';
|
|
|
|
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-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'];
|
|
|
|
offline: AppState['config']['offline'];
|
|
|
|
network: AppState['config']['network'];
|
2018-01-15 09:59:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface DispatchProps {
|
2018-01-07 16:43:06 +00:00
|
|
|
inputGasPrice: TInputGasPrice;
|
|
|
|
fetchCCRates: TFetchCCRates;
|
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-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-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-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-01-25 03:43:27 +00:00
|
|
|
this.props.reset();
|
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) {
|
|
|
|
if (this.props.offline && !nextProps.offline) {
|
|
|
|
this.props.fetchCCRates([this.props.network.unit]);
|
|
|
|
}
|
2018-01-07 16:43:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2018-01-25 03:43:27 +00:00
|
|
|
const { offline, disableToggle, gasPrice, advancedGasOptions, className = '' } = this.props;
|
|
|
|
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-01-15 09:59:59 +00:00
|
|
|
<SimpleGas gasPrice={gasPrice} inputGasPrice={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-01-07 16:43:06 +00:00
|
|
|
<strong>
|
|
|
|
{showAdvanced
|
|
|
|
? `- ${translateRaw('Back to simple')}`
|
2018-01-15 09:59:59 +00:00
|
|
|
: `+ ${translateRaw('Advanced Settings')}`}
|
2018-01-07 16:43:06 +00:00
|
|
|
</strong>
|
|
|
|
</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-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-25 03:43:27 +00:00
|
|
|
fetchCCRates,
|
|
|
|
getNonceRequested,
|
|
|
|
reset
|
|
|
|
})(TXMetaDataPanel);
|