aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/modals/customize-gas/customize-gas.component.js
blob: 3f526bd43a7199ce513dce222a29310aac4a2617 (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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import GasModalCard from '../../customize-gas-modal/gas-modal-card'
import { MIN_GAS_PRICE_GWEI } from '../../send/send.constants'
import Button from '../../button'

import {
  getDecimalGasLimit,
  getDecimalGasPrice,
  getPrefixedHexGasLimit,
  getPrefixedHexGasPrice,
} from './customize-gas.util'

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

  static propTypes = {
    txData: PropTypes.object.isRequired,
    hideModal: PropTypes.func,
    validate: PropTypes.func,
    onSubmit: PropTypes.func,
  }

  state = {
    gasPrice: 0,
    gasLimit: 0,
    originalGasPrice: 0,
    originalGasLimit: 0,
  }

  componentDidMount () {
    const { txData = {} } = this.props
    const { txParams: { gas: hexGasLimit, gasPrice: hexGasPrice } = {} } = txData

    const gasLimit = getDecimalGasLimit(hexGasLimit)
    const gasPrice = getDecimalGasPrice(hexGasPrice)

    this.setState({
      gasPrice,
      gasLimit,
      originalGasPrice: gasPrice,
      originalGasLimit: gasLimit,
    })
  }

  handleRevert () {
    const { originalGasPrice, originalGasLimit } = this.state

    this.setState({
      gasPrice: originalGasPrice,
      gasLimit: originalGasLimit,
    })
  }

  handleSave () {
    const { onSubmit, hideModal } = this.props
    const { gasLimit, gasPrice } = this.state
    const prefixedHexGasPrice = getPrefixedHexGasPrice(gasPrice)
    const prefixedHexGasLimit = getPrefixedHexGasLimit(gasLimit)

    Promise.resolve(onSubmit({ gasPrice: prefixedHexGasPrice, gasLimit: prefixedHexGasLimit }))
      .then(() => hideModal())
  }

  validate () {
    const { gasLimit, gasPrice } = this.state
    return this.props.validate({
      gasPrice: getPrefixedHexGasPrice(gasPrice),
      gasLimit: getPrefixedHexGasLimit(gasLimit),
    })
  }

  render () {
    const { t } = this.context
    const { hideModal } = this.props
    const { gasPrice, gasLimit } = this.state
    const { valid, errorKey } = this.validate()

    return (
      <div className="customize-gas">
        <div className="customize-gas__content">
          <div className="customize-gas__header">
            <div className="customize-gas__title">
              { this.context.t('customGas') }
            </div>
            <div
              className="customize-gas__close"
              onClick={() => hideModal()}
            />
          </div>
          <div className="customize-gas__body">
            <GasModalCard
              value={gasPrice}
              min={MIN_GAS_PRICE_GWEI}
              step={1}
              onChange={value => this.setState({ gasPrice: value })}
              title={t('gasPrice')}
              copy={t('gasPriceCalculation')}
            />
            <GasModalCard
              value={gasLimit}
              min={1}
              step={1}
              onChange={value => this.setState({ gasLimit: value })}
              title={t('gasLimit')}
              copy={t('gasLimitCalculation')}
            />
          </div>
          <div className="customize-gas__footer">
            { !valid && <div className="customize-gas__error-message">{ t(errorKey) }</div> }
            <div
              className="customize-gas__revert"
              onClick={() => this.handleRevert()}
            >
              { t('revert') }
            </div>
            <div className="customize-gas__buttons">
              <Button
                type="default"
                className="customize-gas__cancel"
                onClick={() => hideModal()}
                style={{ marginRight: '10px' }}
              >
                { t('cancel') }
              </Button>
              <Button
                type="primary"
                className="customize-gas__save"
                onClick={() => this.handleSave()}
                style={{ marginRight: '10px' }}
                disabled={!valid}
              >
                { t('save') }
              </Button>
            </div>
          </div>
        </div>
      </div>
    )
  }
}