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.
156 lines
4.2 KiB
156 lines
4.2 KiB
6 years ago
|
import React, { PureComponent } from 'react'
|
||
|
import PropTypes from 'prop-types'
|
||
|
import classnames from 'classnames'
|
||
|
import shuffle from 'lodash.shuffle'
|
||
6 years ago
|
import Button from '../../../../components/ui/button'
|
||
6 years ago
|
import {
|
||
|
INITIALIZE_END_OF_FLOW_ROUTE,
|
||
|
INITIALIZE_SEED_PHRASE_ROUTE,
|
||
6 years ago
|
} from '../../../../helpers/constants/routes'
|
||
|
import { exportAsFile } from '../../../../helpers/utils/util'
|
||
6 years ago
|
import { selectSeedWord, deselectSeedWord } from './confirm-seed-phrase.state'
|
||
|
|
||
|
export default class ConfirmSeedPhrase extends PureComponent {
|
||
|
static contextTypes = {
|
||
6 years ago
|
metricsEvent: PropTypes.func,
|
||
6 years ago
|
t: PropTypes.func,
|
||
|
}
|
||
|
|
||
|
static defaultProps = {
|
||
|
seedPhrase: '',
|
||
|
}
|
||
|
|
||
|
static propTypes = {
|
||
|
history: PropTypes.object,
|
||
|
onSubmit: PropTypes.func,
|
||
|
seedPhrase: PropTypes.string,
|
||
|
}
|
||
|
|
||
|
state = {
|
||
|
selectedSeedWords: [],
|
||
|
shuffledSeedWords: [],
|
||
|
// Hash of shuffledSeedWords index {Number} to selectedSeedWords index {Number}
|
||
|
selectedSeedWordsHash: {},
|
||
|
}
|
||
|
|
||
|
componentDidMount () {
|
||
|
const { seedPhrase = '' } = this.props
|
||
|
const shuffledSeedWords = shuffle(seedPhrase.split(' ')) || []
|
||
|
this.setState({ shuffledSeedWords })
|
||
|
}
|
||
|
|
||
|
handleExport = () => {
|
||
|
exportAsFile('MetaMask Secret Backup Phrase', this.props.seedPhrase, 'text/plain')
|
||
|
}
|
||
|
|
||
|
handleSubmit = async () => {
|
||
6 years ago
|
const { history } = this.props
|
||
6 years ago
|
|
||
|
if (!this.isValid()) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
try {
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Onboarding',
|
||
|
action: 'Seed Phrase Setup',
|
||
|
name: 'Verify Complete',
|
||
|
},
|
||
|
})
|
||
6 years ago
|
history.push(INITIALIZE_END_OF_FLOW_ROUTE)
|
||
6 years ago
|
} catch (error) {
|
||
|
console.error(error.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
handleSelectSeedWord = (word, shuffledIndex) => {
|
||
|
this.setState(selectSeedWord(word, shuffledIndex))
|
||
|
}
|
||
|
|
||
|
handleDeselectSeedWord = shuffledIndex => {
|
||
|
this.setState(deselectSeedWord(shuffledIndex))
|
||
|
}
|
||
|
|
||
|
isValid () {
|
||
|
const { seedPhrase } = this.props
|
||
|
const { selectedSeedWords } = this.state
|
||
|
return seedPhrase === selectedSeedWords.join(' ')
|
||
|
}
|
||
|
|
||
|
render () {
|
||
|
const { t } = this.context
|
||
6 years ago
|
const { history } = this.props
|
||
6 years ago
|
const { selectedSeedWords, shuffledSeedWords, selectedSeedWordsHash } = this.state
|
||
|
|
||
|
return (
|
||
6 years ago
|
<div className="confirm-seed-phrase">
|
||
6 years ago
|
<div className="confirm-seed-phrase__back-button">
|
||
|
<a
|
||
|
onClick={e => {
|
||
|
e.preventDefault()
|
||
|
history.push(INITIALIZE_SEED_PHRASE_ROUTE)
|
||
|
}}
|
||
|
href="#"
|
||
|
>
|
||
|
{`< Back`}
|
||
|
</a>
|
||
|
</div>
|
||
|
<div className="first-time-flow__header">
|
||
|
{ t('confirmSecretBackupPhrase') }
|
||
|
</div>
|
||
|
<div className="first-time-flow__text-block">
|
||
|
{ t('selectEachPhrase') }
|
||
|
</div>
|
||
|
<div className="confirm-seed-phrase__selected-seed-words">
|
||
|
{
|
||
|
selectedSeedWords.map((word, index) => (
|
||
|
<div
|
||
|
key={index}
|
||
|
className="confirm-seed-phrase__seed-word"
|
||
|
>
|
||
|
{ word }
|
||
|
</div>
|
||
|
))
|
||
|
}
|
||
|
</div>
|
||
|
<div className="confirm-seed-phrase__shuffled-seed-words">
|
||
|
{
|
||
|
shuffledSeedWords.map((word, index) => {
|
||
|
const isSelected = index in selectedSeedWordsHash
|
||
|
|
||
|
return (
|
||
|
<div
|
||
|
key={index}
|
||
|
className={classnames(
|
||
|
'confirm-seed-phrase__seed-word',
|
||
|
'confirm-seed-phrase__seed-word--shuffled',
|
||
|
{ 'confirm-seed-phrase__seed-word--selected': isSelected }
|
||
|
)}
|
||
|
onClick={() => {
|
||
|
if (!isSelected) {
|
||
|
this.handleSelectSeedWord(word, index)
|
||
|
} else {
|
||
|
this.handleDeselectSeedWord(index)
|
||
|
}
|
||
|
}}
|
||
|
>
|
||
|
{ word }
|
||
|
</div>
|
||
|
)
|
||
|
})
|
||
|
}
|
||
|
</div>
|
||
|
<Button
|
||
6 years ago
|
type="confirm"
|
||
6 years ago
|
className="first-time-flow__button"
|
||
|
onClick={this.handleSubmit}
|
||
|
disabled={!this.isValid()}
|
||
|
>
|
||
|
{ t('confirm') }
|
||
|
</Button>
|
||
|
</div>
|
||
|
)
|
||
|
}
|
||
|
}
|