aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/pages/confirm-send-token/confirm-send-token.container.js
blob: 2d7efeed69ffac0448dcec924726c72b95ebbd74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { connect } from 'react-redux'
import { compose } from 'recompose'
import { withRouter } from 'react-router-dom'
import ConfirmSendToken from './confirm-send-token.component'
import { calcTokenAmount } from '../../../token-util'
import { clearConfirmTransaction } from '../../../ducks/confirm-transaction.duck'
import { setSelectedToken, updateSend, showSendTokenPage } from '../../../actions'
import { conversionUtil } from '../../../conversion-util'

const mapStateToProps = state => {
  const { confirmTransaction } = state
  const {
    tokenData = {},
    tokenProps: { tokenSymbol, tokenDecimals } = {},
    txData: { txParams: { to: tokenAddress } = {} } = {},
  } = confirmTransaction
  const { params = [] } = tokenData

  let toAddress = ''
  let tokenAmount = ''

  if (params && params.length === 2) {
    [{ value: toAddress }, { value: tokenAmount }] = params
  }

  const numberOfTokens = tokenAmount && tokenDecimals
    ? calcTokenAmount(tokenAmount, tokenDecimals)
    : 0

  return {
    toAddress,
    tokenAddress,
    tokenSymbol,
    numberOfTokens,
  }
}

const mapDispatchToProps = dispatch => {
  return {
    editTransaction: ({ txData, tokenData, tokenProps }) => {
      const { txParams: { to: tokenAddress, gas: gasLimit, gasPrice } = {}, id } = txData
      const { params = [] } = tokenData
      const { value: to } = params[0] || {}
      const { value: tokenAmountInDec } = params[1] || {}
      const tokenAmountInHex = conversionUtil(tokenAmountInDec, {
        fromNumericBase: 'dec',
        toNumericBase: 'hex',
      })
      dispatch(setSelectedToken(tokenAddress))
      dispatch(updateSend({
        gasLimit,
        gasPrice,
        gasTotal: null,
        to,
        amount: tokenAmountInHex,
        errors: { to: null, amount: null },
        editingTransactionId: id && id.toString(),
        token: {
          ...tokenProps,
          address: tokenAddress,
        },
      }))
      dispatch(clearConfirmTransaction())
      dispatch(showSendTokenPage())
    },
  }
}

export default compose(
  withRouter,
  connect(mapStateToProps, mapDispatchToProps)
)(ConfirmSendToken)