You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
5.1 KiB
188 lines
5.1 KiB
4 years ago
|
import React, { Component } from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import { withRouter } from 'react-router-dom';
|
||
|
import { compose } from 'redux';
|
||
|
import { connect } from 'react-redux';
|
||
|
import FileInput from 'react-simple-file-input';
|
||
|
import * as actions from '../../../store/actions';
|
||
|
import { getMetaMaskAccounts } from '../../../selectors';
|
||
|
import Button from '../../../components/ui/button';
|
||
|
import { getMostRecentOverviewPage } from '../../../ducks/history/history';
|
||
8 years ago
|
|
||
4 years ago
|
const HELP_LINK =
|
||
4 years ago
|
'https://metamask.zendesk.com/hc/en-us/articles/360015489331-Importing-an-Account';
|
||
6 years ago
|
|
||
7 years ago
|
class JsonImportSubview extends Component {
|
||
5 years ago
|
state = {
|
||
|
fileContents: '',
|
||
5 years ago
|
isEmpty: true,
|
||
4 years ago
|
};
|
||
8 years ago
|
|
||
4 years ago
|
inputRef = React.createRef();
|
||
5 years ago
|
|
||
4 years ago
|
render() {
|
||
4 years ago
|
const { error, history, mostRecentOverviewPage } = this.props;
|
||
|
const enabled = !this.state.isEmpty && this.state.fileContents !== '';
|
||
7 years ago
|
|
||
|
return (
|
||
5 years ago
|
<div className="new-account-import-form__json">
|
||
|
<p>{this.context.t('usedByClients')}</p>
|
||
4 years ago
|
<a
|
||
|
className="warning"
|
||
|
href={HELP_LINK}
|
||
|
target="_blank"
|
||
|
rel="noopener noreferrer"
|
||
|
>
|
||
|
{this.context.t('fileImportFail')}
|
||
|
</a>
|
||
5 years ago
|
<FileInput
|
||
|
readAs="text"
|
||
|
onLoad={this.onLoad.bind(this)}
|
||
|
style={{
|
||
6 years ago
|
padding: '20px 0px 12px 15%',
|
||
7 years ago
|
fontSize: '15px',
|
||
|
display: 'flex',
|
||
|
justifyContent: 'center',
|
||
6 years ago
|
width: '100%',
|
||
5 years ago
|
}}
|
||
|
/>
|
||
|
<input
|
||
|
className="new-account-import-form__input-password"
|
||
|
type="password"
|
||
|
placeholder={this.context.t('enterPassword')}
|
||
|
id="json-password-box"
|
||
|
onKeyPress={this.createKeyringOnEnter.bind(this)}
|
||
5 years ago
|
onChange={() => this.checkInputEmpty()}
|
||
|
ref={this.inputRef}
|
||
5 years ago
|
/>
|
||
|
<div className="new-account-create-form__buttons">
|
||
|
<Button
|
||
|
type="default"
|
||
|
large
|
||
|
className="new-account-create-form__button"
|
||
5 years ago
|
onClick={() => history.push(mostRecentOverviewPage)}
|
||
5 years ago
|
>
|
||
|
{this.context.t('cancel')}
|
||
|
</Button>
|
||
|
<Button
|
||
|
type="secondary"
|
||
|
large
|
||
|
className="new-account-create-form__button"
|
||
|
onClick={() => this.createNewKeychain()}
|
||
5 years ago
|
disabled={!enabled}
|
||
5 years ago
|
>
|
||
|
{this.context.t('import')}
|
||
|
</Button>
|
||
|
</div>
|
||
4 years ago
|
{error ? <span className="error">{error}</span> : null}
|
||
5 years ago
|
</div>
|
||
4 years ago
|
);
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
onLoad(event) {
|
||
5 years ago
|
this.setState({
|
||
|
fileContents: event.target.result,
|
||
4 years ago
|
});
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
4 years ago
|
createKeyringOnEnter(event) {
|
||
7 years ago
|
if (event.key === 'Enter') {
|
||
4 years ago
|
event.preventDefault();
|
||
|
this.createNewKeychain();
|
||
7 years ago
|
}
|
||
|
}
|
||
8 years ago
|
|
||
4 years ago
|
createNewKeychain() {
|
||
5 years ago
|
const {
|
||
|
firstAddress,
|
||
|
displayWarning,
|
||
|
history,
|
||
|
importNewJsonAccount,
|
||
|
mostRecentOverviewPage,
|
||
|
setSelectedAddress,
|
||
4 years ago
|
} = this.props;
|
||
|
const { fileContents } = this.state;
|
||
7 years ago
|
|
||
7 years ago
|
if (!fileContents) {
|
||
4 years ago
|
const message = this.context.t('needImportFile');
|
||
|
displayWarning(message);
|
||
|
return;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
4 years ago
|
const password = this.inputRef.current.value;
|
||
8 years ago
|
|
||
4 years ago
|
importNewJsonAccount([fileContents, password])
|
||
7 years ago
|
.then(({ selectedAddress }) => {
|
||
|
if (selectedAddress) {
|
||
4 years ago
|
history.push(mostRecentOverviewPage);
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Accounts',
|
||
|
action: 'Import Account',
|
||
|
name: 'Imported Account with JSON',
|
||
|
},
|
||
4 years ago
|
});
|
||
|
displayWarning(null);
|
||
7 years ago
|
} else {
|
||
4 years ago
|
displayWarning('Error importing account.');
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Accounts',
|
||
|
action: 'Import Account',
|
||
|
name: 'Error importing JSON',
|
||
|
},
|
||
4 years ago
|
});
|
||
|
setSelectedAddress(firstAddress);
|
||
7 years ago
|
}
|
||
|
})
|
||
4 years ago
|
.catch((err) => err && displayWarning(err.message || err));
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
4 years ago
|
checkInputEmpty() {
|
||
4 years ago
|
const password = this.inputRef.current.value;
|
||
|
let isEmpty = true;
|
||
5 years ago
|
if (password !== '') {
|
||
4 years ago
|
isEmpty = false;
|
||
5 years ago
|
}
|
||
4 years ago
|
this.setState({ isEmpty });
|
||
5 years ago
|
}
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
JsonImportSubview.propTypes = {
|
||
|
error: PropTypes.string,
|
||
|
displayWarning: PropTypes.func,
|
||
7 years ago
|
firstAddress: PropTypes.string,
|
||
7 years ago
|
importNewJsonAccount: PropTypes.func,
|
||
7 years ago
|
history: PropTypes.object,
|
||
7 years ago
|
setSelectedAddress: PropTypes.func,
|
||
5 years ago
|
mostRecentOverviewPage: PropTypes.string.isRequired,
|
||
4 years ago
|
};
|
||
8 years ago
|
|
||
5 years ago
|
const mapStateToProps = (state) => {
|
||
7 years ago
|
return {
|
||
|
error: state.appState.warning,
|
||
6 years ago
|
firstAddress: Object.keys(getMetaMaskAccounts(state))[0],
|
||
5 years ago
|
mostRecentOverviewPage: getMostRecentOverviewPage(state),
|
||
4 years ago
|
};
|
||
|
};
|
||
8 years ago
|
|
||
5 years ago
|
const mapDispatchToProps = (dispatch) => {
|
||
7 years ago
|
return {
|
||
5 years ago
|
displayWarning: (warning) => dispatch(actions.displayWarning(warning)),
|
||
4 years ago
|
importNewJsonAccount: (options) =>
|
||
|
dispatch(actions.importNewAccount('JSON File', options)),
|
||
|
setSelectedAddress: (address) =>
|
||
|
dispatch(actions.setSelectedAddress(address)),
|
||
4 years ago
|
};
|
||
|
};
|
||
7 years ago
|
|
||
7 years ago
|
JsonImportSubview.contextTypes = {
|
||
|
t: PropTypes.func,
|
||
6 years ago
|
metricsEvent: PropTypes.func,
|
||
4 years ago
|
};
|
||
7 years ago
|
|
||
5 years ago
|
export default compose(
|
||
7 years ago
|
withRouter,
|
||
4 years ago
|
connect(mapStateToProps, mapDispatchToProps),
|
||
4 years ago
|
)(JsonImportSubview);
|