2021-02-04 19:15:23 +01:00
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import getCaretCoordinates from 'textarea-caret';
|
2021-10-05 21:20:42 +02:00
|
|
|
import Button from '../../components/ui/button';
|
2021-02-04 19:15:23 +01:00
|
|
|
import TextField from '../../components/ui/text-field';
|
|
|
|
import Mascot from '../../components/ui/mascot';
|
|
|
|
import { DEFAULT_ROUTE } from '../../helpers/constants/routes';
|
2018-07-10 23:56:12 +02:00
|
|
|
|
|
|
|
export default class UnlockPage extends Component {
|
2018-05-11 01:51:26 +02:00
|
|
|
static contextTypes = {
|
2019-03-05 16:45:01 +01:00
|
|
|
metricsEvent: PropTypes.func,
|
2018-05-11 01:51:26 +02:00
|
|
|
t: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2018-07-10 23:56:12 +02:00
|
|
|
static propTypes = {
|
2020-02-10 18:42:59 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
2018-07-10 23:56:12 +02:00
|
|
|
isUnlocked: PropTypes.bool,
|
2019-01-23 16:25:34 +01:00
|
|
|
onRestore: PropTypes.func,
|
|
|
|
onSubmit: PropTypes.func,
|
2019-03-12 15:39:56 +01:00
|
|
|
forceUpdateMetamaskState: PropTypes.func,
|
|
|
|
showOptInModal: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-07-10 23:56:12 +02:00
|
|
|
|
2019-12-19 23:00:22 +01:00
|
|
|
state = {
|
|
|
|
password: '',
|
|
|
|
error: null,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
submitting = false;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
animationEventEmitter = new EventEmitter();
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
UNSAFE_componentWillMount() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { isUnlocked, history } = this.props;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
if (isUnlocked) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(DEFAULT_ROUTE);
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
handleSubmit = async (event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const { password } = this.state;
|
|
|
|
const { onSubmit, forceUpdateMetamaskState, showOptInModal } = this.props;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2018-07-10 23:56:12 +02:00
|
|
|
if (password === '' || this.submitting) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ error: null });
|
|
|
|
this.submitting = true;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2018-05-30 18:23:31 +02:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await onSubmit(password);
|
|
|
|
const newState = await forceUpdateMetamaskState();
|
2019-03-05 16:45:01 +01:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Navigation',
|
|
|
|
action: 'Unlock',
|
|
|
|
name: 'Success',
|
|
|
|
},
|
|
|
|
isNewVisit: true,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-03-05 16:45:01 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
newState.participateInMetaMetrics === null ||
|
|
|
|
newState.participateInMetaMetrics === undefined
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
showOptInModal();
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
2018-05-30 18:23:31 +02:00
|
|
|
} catch ({ message }) {
|
2019-03-05 16:45:01 +01:00
|
|
|
if (message === 'Incorrect password') {
|
2021-02-04 19:15:23 +01:00
|
|
|
const newState = await forceUpdateMetamaskState();
|
2019-03-05 16:45:01 +01:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Navigation',
|
|
|
|
action: 'Unlock',
|
2020-07-20 19:02:49 +02:00
|
|
|
name: 'Incorrect Password',
|
2019-03-05 16:45:01 +01:00
|
|
|
},
|
|
|
|
customVariables: {
|
|
|
|
numberOfTokens: newState.tokens.length,
|
|
|
|
numberOfAccounts: Object.keys(newState.accounts).length,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-03-05 16:45:01 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ error: message });
|
|
|
|
this.submitting = false;
|
2018-05-30 18:23:31 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
handleInputChange({ target }) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ password: target.value, error: null });
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
// tell mascot to look at page action
|
2020-01-30 20:34:45 +01:00
|
|
|
if (target.getBoundingClientRect) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const element = target;
|
|
|
|
const boundingRect = element.getBoundingClientRect();
|
|
|
|
const coordinates = getCaretCoordinates(element, element.selectionEnd);
|
2020-01-30 20:34:45 +01:00
|
|
|
this.animationEventEmitter.emit('point', {
|
|
|
|
x: boundingRect.left + coordinates.left - element.scrollLeft,
|
|
|
|
y: boundingRect.top + coordinates.top - element.scrollTop,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-01-30 20:34:45 +01:00
|
|
|
}
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderSubmitButton() {
|
2018-05-11 01:51:26 +02:00
|
|
|
const style = {
|
2021-04-23 00:31:13 +02:00
|
|
|
backgroundColor: '#037dd6',
|
2018-05-11 01:51:26 +02:00
|
|
|
color: 'white',
|
|
|
|
marginTop: '20px',
|
|
|
|
height: '60px',
|
|
|
|
fontWeight: '400',
|
|
|
|
boxShadow: 'none',
|
2021-10-05 21:20:42 +02:00
|
|
|
borderRadius: '100px',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
style={style}
|
|
|
|
disabled={!this.state.password}
|
|
|
|
fullWidth
|
2020-10-15 17:36:15 +02:00
|
|
|
variant="contained"
|
2018-05-11 01:51:26 +02:00
|
|
|
size="large"
|
2019-01-23 16:25:34 +01:00
|
|
|
onClick={this.handleSubmit}
|
2018-05-11 01:51:26 +02:00
|
|
|
disableRipple
|
|
|
|
>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.context.t('unlock')}
|
2018-05-11 01:51:26 +02:00
|
|
|
</Button>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { password, error } = this.state;
|
|
|
|
const { t } = this.context;
|
2021-04-23 00:31:13 +02:00
|
|
|
const { onRestore } = this.props;
|
2018-05-11 01:51:26 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="unlock-page__container">
|
|
|
|
<div className="unlock-page">
|
|
|
|
<div className="unlock-page__mascot-container">
|
|
|
|
<Mascot
|
|
|
|
animationEventEmitter={this.animationEventEmitter}
|
|
|
|
width="120"
|
|
|
|
height="120"
|
|
|
|
/>
|
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
<h1 className="unlock-page__title">{t('welcomeBack')}</h1>
|
|
|
|
<div>{t('unlockMessage')}</div>
|
|
|
|
<form className="unlock-page__form" onSubmit={this.handleSubmit}>
|
2018-05-11 01:51:26 +02:00
|
|
|
<TextField
|
|
|
|
id="password"
|
2018-07-10 23:56:12 +02:00
|
|
|
label={t('password')}
|
2018-05-11 01:51:26 +02:00
|
|
|
type="password"
|
2018-07-10 23:56:12 +02:00
|
|
|
value={password}
|
2020-02-15 21:34:12 +01:00
|
|
|
onChange={(event) => this.handleInputChange(event)}
|
2018-05-11 01:51:26 +02:00
|
|
|
error={error}
|
|
|
|
autoFocus
|
|
|
|
autoComplete="current-password"
|
2019-12-04 03:21:55 +01:00
|
|
|
theme="material"
|
2018-05-11 01:51:26 +02:00
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
</form>
|
2020-11-03 00:41:28 +01:00
|
|
|
{this.renderSubmitButton()}
|
2018-05-11 01:51:26 +02:00
|
|
|
<div className="unlock-page__links">
|
2021-04-23 00:31:13 +02:00
|
|
|
{t('importAccountText', [
|
|
|
|
<button
|
|
|
|
key="import-account"
|
|
|
|
className="unlock-page__link unlock-page__link--import"
|
|
|
|
onClick={() => onRestore()}
|
|
|
|
>
|
|
|
|
{t('importAccountLinkText')}
|
|
|
|
</button>,
|
|
|
|
])}
|
|
|
|
</div>
|
|
|
|
<div className="unlock-page__support">
|
|
|
|
{t('needHelp', [
|
|
|
|
<a
|
|
|
|
href="https://support.metamask.io"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
key="need-help-link"
|
|
|
|
>
|
|
|
|
{t('needHelpLinkText')}
|
|
|
|
</a>,
|
|
|
|
])}
|
2018-05-11 01:51:26 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2018-05-11 01:51:26 +02:00
|
|
|
}
|
|
|
|
}
|