// @flow import React from 'react'; import PropTypes from 'prop-types'; import translate from 'translations'; import { UnlockHeader } from 'components/ui'; import { Donate, DataField, CustomMessage, GasField, AmountField, AddressField } from './components'; import { BalanceSidebar } from 'components'; import pickBy from 'lodash/pickBy'; import type { State as AppState } from 'reducers'; import { connect } from 'react-redux'; import BaseWallet from 'libs/wallet/base'; // import type { Transaction } from './types'; import customMessages from './messages'; import { donationAddressMap } from 'config/data'; import Big from 'big.js'; import type { TokenBalance } from 'selectors/wallet'; import { getTokenBalances } from 'selectors/wallet'; type State = { hasQueryString: boolean, readOnly: boolean, to: string, value: string, unit: string, gasLimit: string, data: string, gasChanged: boolean }; function getParam(query: { [string]: string }, key: string) { const keys = Object.keys(query); const index = keys.findIndex(k => k.toLowerCase() === key.toLowerCase()); if (index === -1) { return null; } return query[keys[index]]; } // TODO query string // TODO how to handle DATA? type Props = { location: { query: { [string]: string } }, wallet: BaseWallet, balance: Big, tokenBalances: TokenBalance[] }; export class SendTransaction extends React.Component { props: Props; state: State = { hasQueryString: false, readOnly: false, // FIXME use correct defaults to: '', value: '', unit: 'ether', gasLimit: '21000', data: '', gasChanged: false }; componentDidMount() { const queryPresets = pickBy(this.parseQuery()); if (Object.keys(queryPresets).length) { this.setState({ ...queryPresets, hasQueryString: true }); } } render() { const unlocked = !!this.props.wallet; const unitReadable = 'UNITREADABLE'; const nodeUnit = 'NODEUNIT'; const hasEnoughBalance = false; const { to, value, unit, gasLimit, data, readOnly, hasQueryString } = this.state; const customMessage = customMessages.find(m => m.to === to); // tokens // ng-show="token.balance!=0 && token.balance!='loading' || token.type!=='default' || tokenVisibility=='shown'" return (
{hasQueryString &&

{translate('WARN_Send_Link')}

} {unlocked &&
{'' /* */}

{readOnly && !hasEnoughBalance &&
Warning! You do not have enough funds to complete this swap.
Please add more funds or access a different wallet.
}

{translate('SEND_trans')}

!token.balance.eq(0)) .map(token => token.symbol) .sort()} onChange={readOnly ? void 0 : this.onAmountChange} /> {unit === 'ether' && }
{'' /* */} { '' /* @@if (site === 'mew' ) { @@include( './sendTx-content.tpl', { "site": "mew" } ) } @@if (site === 'cx' ) { @@include( './sendTx-content.tpl', { "site": "cx" } ) } @@if (site === 'mew' ) { @@include( './sendTx-modal.tpl', { "site": "mew" } ) } @@if (site === 'cx' ) { @@include( './sendTx-modal.tpl', { "site": "cx" } ) } */ }
}
); } parseQuery() { const query = this.props.location.query; const to = getParam(query, 'to'); const data = getParam(query, 'data'); // FIXME validate token against presets const unit = getParam(query, 'tokenSymbol'); const value = getParam(query, 'value'); let gasLimit = getParam(query, 'gas'); if (gasLimit === null) { gasLimit = getParam(query, 'limit'); } const readOnly = getParam(query, 'readOnly') == null ? false : true; return { to, data, value, unit, gasLimit, readOnly }; } // FIXME use mkTx instead or something that could take care of default gas/data and whatnot, // FIXME also should it reset gasChanged? onNewTx = ( address: string, amount: string, unit: string, data: string = '', gasLimit: string = '21000' ) => { this.setState({ to: address, value: amount, unit, data, gasLimit, gasChanged: false }); }; onAddressChange = (value: string) => { this.setState({ to: value }); }; onDataChange = (value: string) => { if (this.state.unit !== 'ether') { return; } this.setState({ ...this.state, data: value }); }; onGasChange = (value: string) => { this.setState({ gasLimit: value, gasChanged: true }); }; onAmountChange = (value: string, unit: string) => { // TODO sub gas for eth if (value === 'everything') { if (unit === 'ether') { value = this.props.balance.toString(); } const token = this.props.tokenBalances.find( token => token.symbol === unit ); if (!token) { return; } value = token.balance.toString(); } this.setState({ value, unit }); }; } function mapStateToProps(state: AppState) { return { wallet: state.wallet.inst, balance: state.wallet.balance, tokenBalances: getTokenBalances(state) }; } export default connect(mapStateToProps)(SendTransaction);