aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/gas-customization/advanced-gas-inputs/advanced-gas-inputs.component.js
blob: 95894140cbe76e5ddf1e05e9b9c38cac3eccdeec (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import classnames from 'classnames'
import debounce from 'lodash.debounce'

export default class AdvancedTabContent extends Component {
  static contextTypes = {
    t: PropTypes.func,
  }

  static propTypes = {
    updateCustomGasPrice: PropTypes.func,
    updateCustomGasLimit: PropTypes.func,
    customGasPrice: PropTypes.number,
    customGasLimit: PropTypes.number,
    insufficientBalance: PropTypes.bool,
    customPriceIsSafe: PropTypes.bool,
    isSpeedUp: PropTypes.bool,
    showGasPriceInfoModal: PropTypes.func,
    showGasLimitInfoModal: PropTypes.func,
  }

  debouncedGasLimitReset = debounce((dVal) => {
    if (dVal < 21000) {
      this.props.updateCustomGasLimit(21000)
    }
  }, 1000, { trailing: true })

  onChangeGasLimit = (val) => {
    this.props.updateCustomGasLimit(val)
    this.debouncedGasLimitReset(val)
  }

  gasInputError ({ labelKey, insufficientBalance, customPriceIsSafe, isSpeedUp, value }) {
    const { t } = this.context
    let errorText
    let errorType
    let isInError = true


    if (insufficientBalance) {
      errorText = t('insufficientBalance')
      errorType = 'error'
    } else if (labelKey === 'gasPrice' && isSpeedUp && value === 0) {
      errorText = t('zeroGasPriceOnSpeedUpError')
      errorType = 'error'
    } else if (labelKey === 'gasPrice' && !customPriceIsSafe) {
      errorText = t('gasPriceExtremelyLow')
      errorType = 'warning'
    } else {
      isInError = false
    }

    return {
      isInError,
      errorText,
      errorType,
    }
  }

  gasInput ({ labelKey, value, onChange, insufficientBalance, showGWEI, customPriceIsSafe, isSpeedUp }) {
    const {
      isInError,
      errorText,
      errorType,
    } = this.gasInputError({ labelKey, insufficientBalance, customPriceIsSafe, isSpeedUp, value })

    return (
      <div className="advanced-gas-inputs__gas-edit-row__input-wrapper">
        <input
          className={classnames('advanced-gas-inputs__gas-edit-row__input', {
            'advanced-gas-inputs__gas-edit-row__input--error': isInError && errorType === 'error',
            'advanced-gas-inputs__gas-edit-row__input--warning': isInError && errorType === 'warning',
          })}
          type="number"
          value={value}
          onChange={event => onChange(Number(event.target.value))}
        />
        <div className={classnames('advanced-gas-inputs__gas-edit-row__input-arrows', {
          'advanced-gas-inputs__gas-edit-row__input--error': isInError && errorType === 'error',
          'advanced-gas-inputs__gas-edit-row__input--warning': isInError && errorType === 'warning',
        })}>
          <div
            className="advanced-gas-inputs__gas-edit-row__input-arrows__i-wrap"
            onClick={() => onChange(value + 1)}
          >
            <i className="fa fa-sm fa-angle-up" />
          </div>
          <div
            className="advanced-gas-inputs__gas-edit-row__input-arrows__i-wrap"
            onClick={() => onChange(Math.max(value - 1, 0))}
          >
            <i className="fa fa-sm fa-angle-down" />
          </div>
        </div>
        { isInError
          ? <div className={`advanced-gas-inputs__gas-edit-row__${errorType}-text`}>
              { errorText }
            </div>
          : null }
      </div>
    )
  }

  infoButton (onClick) {
    return <i className="fa fa-info-circle" onClick={onClick} />
  }

  renderGasEditRow (gasInputArgs) {
    return (
      <div className="advanced-gas-inputs__gas-edit-row">
        <div className="advanced-gas-inputs__gas-edit-row__label">
          { this.context.t(gasInputArgs.labelKey) }
          { this.infoButton(() => gasInputArgs.infoOnClick()) }
        </div>
        { this.gasInput(gasInputArgs) }
      </div>
    )
  }

  render () {
    const {
      customGasPrice,
      updateCustomGasPrice,
      customGasLimit,
      insufficientBalance,
      customPriceIsSafe,
      isSpeedUp,
      showGasPriceInfoModal,
      showGasLimitInfoModal,
    } = this.props

    return (
      <div className="advanced-gas-inputs__gas-edit-rows">
        { this.renderGasEditRow({
          labelKey: 'gasPrice',
          value: customGasPrice,
          onChange: updateCustomGasPrice,
          insufficientBalance,
          customPriceIsSafe,
          showGWEI: true,
          isSpeedUp,
          infoOnClick: showGasPriceInfoModal,
        }) }
        { this.renderGasEditRow({
          labelKey: 'gasLimit',
          value: customGasLimit,
          onChange: this.onChangeGasLimit,
          insufficientBalance,
          customPriceIsSafe,
          infoOnClick: showGasLimitInfoModal,
        }) }
      </div>
    )
  }
}