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

152 lines
4.1 KiB
TypeScript

import React from 'react';
import BN from 'bn.js';
import { translateRaw } from 'translations';
import { connect } from 'react-redux';
import {
inputGasPrice,
TInputGasPrice,
inputGasPriceIntent,
TInputGasPriceIntent,
getNonceRequested,
TGetNonceRequested,
reset,
TReset
} from 'actions/transaction';
import { fetchCCRatesRequested, TFetchCCRatesRequested } from 'actions/rates';
import { getNetworkConfig, getOffline } from 'selectors/config';
import { AppState } from 'reducers';
import { Units } from 'libs/units';
import SimpleGas from './components/SimpleGas';
import AdvancedGas, { AdvancedOptions } from './components/AdvancedGas';
import './TXMetaDataPanel.scss';
import { getGasPrice } from 'selectors/transaction';
import { NetworkConfig } from 'types/network';
type SliderStates = 'simple' | 'advanced';
interface StateProps {
gasPrice: AppState['transaction']['fields']['gasPrice'];
offline: AppState['config']['meta']['offline'];
network: NetworkConfig;
}
interface DispatchProps {
inputGasPrice: TInputGasPrice;
inputGasPriceIntent: TInputGasPriceIntent;
fetchCCRates: TFetchCCRatesRequested;
getNonceRequested: TGetNonceRequested;
reset: TReset;
}
// Set default props for props that can't be truthy or falsy
interface DefaultProps {
initialState: SliderStates;
}
interface OwnProps {
initialState?: SliderStates;
disableToggle?: boolean;
advancedGasOptions?: AdvancedOptions;
className?: string;
}
type Props = DispatchProps & OwnProps & StateProps;
interface State {
gasPrice: AppState['transaction']['fields']['gasPrice'];
sliderState: SliderStates;
}
class TXMetaDataPanel extends React.Component<Props, State> {
public static defaultProps: DefaultProps = {
initialState: 'simple'
};
public state: State = {
gasPrice: this.props.gasPrice,
sliderState: (this.props as DefaultProps).initialState
};
public componentDidMount() {
if (!this.props.offline) {
this.props.reset();
this.props.fetchCCRates([this.props.network.unit]);
this.props.getNonceRequested();
}
}
public componentWillReceiveProps(nextProps: Props) {
if (this.props.offline && !nextProps.offline) {
this.props.fetchCCRates([this.props.network.unit]);
}
if (this.props.gasPrice !== nextProps.gasPrice) {
this.setState({ gasPrice: nextProps.gasPrice });
}
}
public render() {
const { offline, disableToggle, advancedGasOptions, className = '' } = this.props;
const { gasPrice } = this.state;
const showAdvanced = this.state.sliderState === 'advanced' || offline;
return (
<div className={`Gas col-md-12 ${className}`}>
{showAdvanced ? (
<AdvancedGas
gasPrice={gasPrice}
inputGasPrice={this.props.inputGasPrice}
options={advancedGasOptions}
/>
) : (
<SimpleGas
gasPrice={gasPrice}
inputGasPrice={this.handleGasPriceInput}
setGasPrice={this.props.inputGasPrice}
/>
)}
{!offline &&
!disableToggle && (
<div className="help-block">
<a className="Gas-toggle" onClick={this.toggleAdvanced}>
<strong>
{showAdvanced
? `- ${translateRaw('Back to simple')}`
: `+ ${translateRaw('Advanced Settings')}`}
</strong>
</a>
</div>
)}
</div>
);
}
private toggleAdvanced = () => {
this.setState({ sliderState: this.state.sliderState === 'advanced' ? 'simple' : 'advanced' });
};
private handleGasPriceInput = (raw: string) => {
const gasBn = new BN(raw);
const value = gasBn.mul(new BN(Units.gwei));
this.setState({
gasPrice: { raw, value }
});
this.props.inputGasPriceIntent(raw);
};
}
function mapStateToProps(state: AppState): StateProps {
return {
gasPrice: getGasPrice(state),
offline: getOffline(state),
network: getNetworkConfig(state)
};
}
export default connect(mapStateToProps, {
inputGasPrice,
inputGasPriceIntent,
fetchCCRates: fetchCCRatesRequested,
getNonceRequested,
reset
})(TXMetaDataPanel);