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' }) => (
{label}
{errorMessage}
) 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 (
Upload File
this.setState({ jsonFile: e.target.files[0] })} />
{name}
{warning && 'Something went wrong. Please make sure your JSON file is properly formatted.'}
{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.', })}
) } 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 ? : (
{ e.preventDefault() this.props.back() }} href="#" > {`< Back`}
Import an Account
How would you like to import your account?
{this.renderContent()} File import not working?
) } } export default connect( ({ appState: { isLoading, warning } }) => ({ isLoading, warning }), dispatch => ({ importNewAccount: (strategy, args) => dispatch(importNewAccount(strategy, args)), hideWarning: () => dispatch(hideWarning()), }) )(ImportAccountScreen)