aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/token-currency-display/token-currency-display.component.js
blob: 4bb09a4b6d1f1cfbe7564a893c464ad55ad71ffa (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
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import CurrencyDisplay from '../currency-display/currency-display.component'
import { getTokenData } from '../../helpers/transactions.util'
import { getTokenValue, calcTokenAmount } from '../../token-util'

export default class TokenCurrencyDisplay extends PureComponent {
  static propTypes = {
    transactionData: PropTypes.string,
    token: PropTypes.object,
  }

  state = {
    displayValue: '',
  }

  componentDidMount () {
    this.setDisplayValue()
  }

  componentDidUpdate (prevProps) {
    const { transactionData } = this.props
    const { transactionData: prevTransactionData } = prevProps

    if (transactionData !== prevTransactionData) {
      this.setDisplayValue()
    }
  }

  setDisplayValue () {
    const { transactionData: data, token } = this.props
    const { decimals = '', symbol = '' } = token
    const tokenData = getTokenData(data)

    let displayValue

    if (tokenData.params && tokenData.params.length) {
      const tokenValue = getTokenValue(tokenData.params)
      const tokenAmount = calcTokenAmount(tokenValue, decimals)
      displayValue = `${tokenAmount} ${symbol}`
    }

    this.setState({ displayValue })
  }

  render () {
    return (
      <CurrencyDisplay
        {...this.props}
        displayValue={this.state.displayValue}
      />
    )
  }
}