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.
192 lines
5.0 KiB
192 lines
5.0 KiB
7 years ago
|
import React, { Component } from 'react'
|
||
|
import PropTypes from 'prop-types'
|
||
7 years ago
|
import Button from '@material-ui/core/Button'
|
||
6 years ago
|
import TextField from '../../components/ui/text-field'
|
||
6 years ago
|
import getCaretCoordinates from 'textarea-caret'
|
||
|
import { EventEmitter } from 'events'
|
||
6 years ago
|
import Mascot from '../../components/ui/mascot'
|
||
|
import { DEFAULT_ROUTE } from '../../helpers/constants/routes'
|
||
6 years ago
|
|
||
|
export default class UnlockPage extends Component {
|
||
7 years ago
|
static contextTypes = {
|
||
6 years ago
|
metricsEvent: PropTypes.func,
|
||
7 years ago
|
t: PropTypes.func,
|
||
|
}
|
||
|
|
||
6 years ago
|
static propTypes = {
|
||
|
history: PropTypes.object,
|
||
|
isUnlocked: PropTypes.bool,
|
||
6 years ago
|
onImport: PropTypes.func,
|
||
|
onRestore: PropTypes.func,
|
||
|
onSubmit: PropTypes.func,
|
||
6 years ago
|
forceUpdateMetamaskState: PropTypes.func,
|
||
|
showOptInModal: PropTypes.func,
|
||
6 years ago
|
}
|
||
|
|
||
7 years ago
|
constructor (props) {
|
||
|
super(props)
|
||
|
|
||
|
this.state = {
|
||
|
password: '',
|
||
|
error: null,
|
||
|
}
|
||
|
|
||
6 years ago
|
this.submitting = false
|
||
7 years ago
|
this.animationEventEmitter = new EventEmitter()
|
||
|
}
|
||
|
|
||
|
componentWillMount () {
|
||
|
const { isUnlocked, history } = this.props
|
||
|
|
||
|
if (isUnlocked) {
|
||
|
history.push(DEFAULT_ROUTE)
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
handleSubmit = async event => {
|
||
7 years ago
|
event.preventDefault()
|
||
|
event.stopPropagation()
|
||
|
|
||
|
const { password } = this.state
|
||
6 years ago
|
const { onSubmit, forceUpdateMetamaskState, showOptInModal } = this.props
|
||
7 years ago
|
|
||
6 years ago
|
if (password === '' || this.submitting) {
|
||
7 years ago
|
return
|
||
|
}
|
||
|
|
||
|
this.setState({ error: null })
|
||
6 years ago
|
this.submitting = true
|
||
7 years ago
|
|
||
7 years ago
|
try {
|
||
6 years ago
|
await onSubmit(password)
|
||
6 years ago
|
const newState = await forceUpdateMetamaskState()
|
||
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Navigation',
|
||
|
action: 'Unlock',
|
||
|
name: 'Success',
|
||
|
},
|
||
|
isNewVisit: true,
|
||
|
})
|
||
|
|
||
|
if (newState.participateInMetaMetrics === null || newState.participateInMetaMetrics === undefined) {
|
||
|
showOptInModal()
|
||
|
}
|
||
7 years ago
|
} catch ({ message }) {
|
||
6 years ago
|
if (message === 'Incorrect password') {
|
||
|
const newState = await forceUpdateMetamaskState()
|
||
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Navigation',
|
||
|
action: 'Unlock',
|
||
|
name: 'Incorrect Passowrd',
|
||
|
},
|
||
|
customVariables: {
|
||
|
numberOfTokens: newState.tokens.length,
|
||
|
numberOfAccounts: Object.keys(newState.accounts).length,
|
||
|
},
|
||
|
})
|
||
|
}
|
||
|
|
||
7 years ago
|
this.setState({ error: message })
|
||
6 years ago
|
this.submitting = false
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
|
handleInputChange ({ target }) {
|
||
|
this.setState({ password: target.value, error: null })
|
||
|
|
||
|
// tell mascot to look at page action
|
||
|
const element = target
|
||
|
const boundingRect = element.getBoundingClientRect()
|
||
|
const coordinates = getCaretCoordinates(element, element.selectionEnd)
|
||
|
this.animationEventEmitter.emit('point', {
|
||
|
x: boundingRect.left + coordinates.left - element.scrollLeft,
|
||
|
y: boundingRect.top + coordinates.top - element.scrollTop,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
renderSubmitButton () {
|
||
|
const style = {
|
||
|
backgroundColor: '#f7861c',
|
||
|
color: 'white',
|
||
|
marginTop: '20px',
|
||
|
height: '60px',
|
||
|
fontWeight: '400',
|
||
|
boxShadow: 'none',
|
||
|
borderRadius: '4px',
|
||
|
}
|
||
|
|
||
|
return (
|
||
|
<Button
|
||
|
type="submit"
|
||
|
style={style}
|
||
|
disabled={!this.state.password}
|
||
|
fullWidth
|
||
|
variant="raised"
|
||
|
size="large"
|
||
6 years ago
|
onClick={this.handleSubmit}
|
||
7 years ago
|
disableRipple
|
||
|
>
|
||
|
{ this.context.t('login') }
|
||
|
</Button>
|
||
|
)
|
||
|
}
|
||
|
|
||
|
render () {
|
||
6 years ago
|
const { password, error } = this.state
|
||
|
const { t } = this.context
|
||
6 years ago
|
const { onImport, onRestore } = this.props
|
||
7 years ago
|
|
||
|
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>
|
||
|
<h1 className="unlock-page__title">
|
||
6 years ago
|
{ t('welcomeBack') }
|
||
7 years ago
|
</h1>
|
||
6 years ago
|
<div>{ t('unlockMessage') }</div>
|
||
7 years ago
|
<form
|
||
|
className="unlock-page__form"
|
||
6 years ago
|
onSubmit={this.handleSubmit}
|
||
7 years ago
|
>
|
||
|
<TextField
|
||
|
id="password"
|
||
6 years ago
|
label={t('password')}
|
||
7 years ago
|
type="password"
|
||
6 years ago
|
value={password}
|
||
7 years ago
|
onChange={event => this.handleInputChange(event)}
|
||
|
error={error}
|
||
|
autoFocus
|
||
|
autoComplete="current-password"
|
||
7 years ago
|
material
|
||
7 years ago
|
fullWidth
|
||
|
/>
|
||
|
</form>
|
||
|
{ this.renderSubmitButton() }
|
||
|
<div className="unlock-page__links">
|
||
|
<div
|
||
|
className="unlock-page__link"
|
||
6 years ago
|
onClick={() => onRestore()}
|
||
7 years ago
|
>
|
||
6 years ago
|
{ t('restoreFromSeed') }
|
||
7 years ago
|
</div>
|
||
|
<div
|
||
|
className="unlock-page__link unlock-page__link--import"
|
||
6 years ago
|
onClick={() => onImport()}
|
||
7 years ago
|
>
|
||
6 years ago
|
{ t('importUsingSeed') }
|
||
7 years ago
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
)
|
||
|
}
|
||
|
}
|