aboutsummaryrefslogtreecommitdiffstats
path: root/mascara/src/app/first-time/import-account-screen.js
blob: 555a26386deb3c4f66f7497a419d6cebbce86e02 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import classnames from 'classnames'
import LoadingScreen from './loading-screen'
import {importNewAccount, hideWarning} from '../../../../ui/app/actions'

const Input = ({ label, placeholder, onChange, errorMessage, type = 'text' }) => (
  <div className="import-account__input-wrapper">
    <div className="import-account__input-label">{label}</div>
    <input
      type={type}
      placeholder={placeholder}
      className={classnames('first-time-flow__input import-account__input', {
        'first-time-flow__input--error': errorMessage,
      })}
      onChange={onChange}
    />
    <div className="import-account__input-error-message">{errorMessage}</div>
  </div>
)

Input.prototype.propTypes = {
  label: PropTypes.string.isRequired,
  placeholder: PropTypes.string.isRequired,
  type: PropTypes.string.isRequired,
  errorMessage: PropTypes.string.isRequired,
  onChange: PropTypes.func.isRequired,
}

class ImportAccountScreen extends Component {
  static OPTIONS = {
    PRIVATE_KEY: 'private_key',
    JSON_FILE: 'json_file',
  };

  static propTypes = {
    warning: PropTypes.string,
    back: PropTypes.func.isRequired,
    next: PropTypes.func.isRequired,
    importNewAccount: PropTypes.func.isRequired,
    hideWarning: PropTypes.func.isRequired,
    isLoading: PropTypes.bool.isRequired,
  };

  state = {
    selectedOption: ImportAccountScreen.OPTIONS.PRIVATE_KEY,
    privateKey: '',
    jsonFile: {},
  }

  isValid () {
    const { OPTIONS } = ImportAccountScreen
    const { privateKey, jsonFile, password } = this.state

    switch (this.state.selectedOption) {
      case OPTIONS.JSON_FILE:
        return Boolean(jsonFile && password)
      case OPTIONS.PRIVATE_KEY:
      default:
        return Boolean(privateKey)
    }
  }

  onClick = () => {
    const { OPTIONS } = ImportAccountScreen
    const { importNewAccount, next } = this.props
    const { privateKey, jsonFile, password } = this.state

    switch (this.state.selectedOption) {
      case OPTIONS.JSON_FILE:
        return importNewAccount('JSON File', [ jsonFile, password ])
          // JS runtime requires caught rejections but failures are handled by Redux
          .catch()
          .then(next)
      case OPTIONS.PRIVATE_KEY:
      default:
        return importNewAccount('Private Key', [ privateKey ])
          // JS runtime requires caught rejections but failures are handled by Redux
          .catch()
          .then(next)
    }
  }

  renderPrivateKey () {
    return Input({
      label: 'Add Private Key String',
      placeholder: 'Enter private key',
      onChange: e => this.setState({ privateKey: e.target.value }),
      errorMessage: this.props.warning && 'Something went wrong. Please make sure your private key is correct.',
    })
  }

  renderJsonFile () {
    const { jsonFile: { name } } = this.state
    const { warning } = this.props

    return (
      <div className="">
        <div className="import-account__input-wrapper">
          <div className="import-account__input-label">Upload File</div>
          <div className="import-account__file-picker-wrapper">
            <input
              type="file"
              id="file"
              className="import-account__file-input"
              onChange={e => this.setState({ jsonFile: e.target.files[0] })}
            />
            <label
              htmlFor="file"
              className={classnames('import-account__file-input-label', {
                'import-account__file-input-label--error': warning,
              })}
            >
              Choose File
            </label>
            <div className="import-account__file-name">{name}</div>
          </div>
          <div className="import-account__input-error-message">
            {warning && 'Something went wrong. Please make sure your JSON file is properly formatted.'}
          </div>
        </div>
        {Input({
          label: 'Enter Password',
          placeholder: 'Enter Password',
          type: 'password',
          onChange: e => this.setState({ password: e.target.value }),
          errorMessage: warning && 'Please make sure your password is correct.',
        })}
      </div>
    )
  }

  renderContent () {
    const { OPTIONS } = ImportAccountScreen

    switch (this.state.selectedOption) {
      case OPTIONS.JSON_FILE:
        return this.renderJsonFile()
      case OPTIONS.PRIVATE_KEY:
      default:
        return this.renderPrivateKey()
    }
  }

  render () {
    const { OPTIONS } = ImportAccountScreen
    const { selectedOption } = this.state

    return this.props.isLoading
      ? <LoadingScreen loadingMessage="Creating your new account" />
      : (
        <div className="import-account">
          <a
            className="import-account__back-button"
            onClick={e => {
              e.preventDefault()
              this.props.back()
            }}
            href="#"
          >
            {`< Back`}
          </a>
          <div className="import-account__title">
            Import an Account
          </div>
          <div className="import-account__selector-label">
            How would you like to import your account?
          </div>
          <select
            className="import-account__dropdown"
            value={selectedOption}
            onChange={e => {
              this.setState({ selectedOption: e.target.value })
              this.props.hideWarning()
            }}
          >
            <option value={OPTIONS.PRIVATE_KEY}>Private Key</option>
            <option value={OPTIONS.JSON_FILE}>JSON File</option>
          </select>
          {this.renderContent()}
          <button
            className="first-time-flow__button"
            disabled={!this.isValid()}
            onClick={this.onClick}
          >
            Import
          </button>
          <a
            href="https://github.com/MetaMask/faq/blob/master/README.md#q-i-cant-use-the-import-feature-for-uploading-a-json-file-the-window-keeps-closing-when-i-try-to-select-a-file"
            className="first-time-flow__link import-account__faq-link"
            rel="noopener noreferrer"
            target="_blank"
          >
            File import not working?
          </a>
        </div>
      )
  }
}

export default connect(
  ({ appState: { isLoading, warning } }) => ({ isLoading, warning }),
  dispatch => ({
    importNewAccount: (strategy, args) => dispatch(importNewAccount(strategy, args)),
    hideWarning: () => dispatch(hideWarning()),
  })
)(ImportAccountScreen)