aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/balance-component.js
blob: 62024895b41dc1843219f5fdc5fd65e7938af50a (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits

const { formatBalance, generateBalanceObject } = require('../util')

module.exports = BalanceComponent

inherits(BalanceComponent, Component)
function BalanceComponent () {
  Component.call(this)
}

BalanceComponent.prototype.render = function () {
  const props = this.props
  const { balanceValue } = props
  const needsParse = 'needsParse' in props ? props.needsParse : true
  const formattedBalance = balanceValue ? formatBalance(balanceValue, 6, needsParse) : '...'

  return h('div.balance-container', {}, [
    // laptop: 50px 50px
    // mobile: 100px 100px

    // TODO: balance icon needs to be passed in
    h('img.balance-icon', {
      src: '../images/eth_logo.svg',
      style: {},
    }),

    this.renderBalance(formattedBalance),
  ])
}

BalanceComponent.prototype.renderBalance = function (formattedBalance) {
  const props = this.props
  const { shorten } = props
  const showFiat = 'showFiat' in props ? props.showFiat : true

  if (formattedBalance === 'None' || formattedBalance === '...') {
    return h('div.flex-column.balance-display', {}, [
      h('div.token-amount', {
        style: {},
      }, formattedBalance),
    ])
  }

  var balanceObj = generateBalanceObject(formattedBalance, shorten ? 1 : 3)
  var balanceValue = shorten ? balanceObj.shortBalance : balanceObj.balance

  var label = balanceObj.label

  // laptop: 5vw?
  // phone: 50vw?
  return h('div.flex-column.balance-display', {}, [
    h('div.token-amount', {
      style: {},
    }, `${balanceValue} ${label}`),

    showFiat ? this.renderFiatValue(formattedBalance) : null,
  ])
}

BalanceComponent.prototype.renderFiatValue = function (formattedBalance) {

  const props = this.props
  const { conversionRate, currentCurrency } = props

  if (formattedBalance === 'None') return formattedBalance
  var fiatDisplayNumber
  var splitBalance = formattedBalance.split(' ')

  if (conversionRate !== 0) {
    fiatDisplayNumber = (Number(splitBalance[0]) * conversionRate).toFixed(2)
  } else {
    fiatDisplayNumber = 'N/A'
  }

  return fiatDisplay(fiatDisplayNumber, currentCurrency)
}

function fiatDisplay (fiatDisplayNumber, fiatSuffix) {
  if (fiatDisplayNumber !== 'N/A') {
    return h('div.fiat-amount', {
      style: {},
    }, `${fiatDisplayNumber} ${fiatSuffix}`)
  } else {
    return h('div')
  }
}