2019-01-23 16:25:34 +01:00
|
|
|
import React, { PureComponent } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classnames from 'classnames'
|
2019-03-22 00:03:30 +01:00
|
|
|
import Button from '../../../../components/ui/button'
|
2019-03-05 16:45:01 +01:00
|
|
|
import {
|
|
|
|
INITIALIZE_END_OF_FLOW_ROUTE,
|
|
|
|
INITIALIZE_SEED_PHRASE_ROUTE,
|
2019-08-02 05:57:26 +02:00
|
|
|
DEFAULT_ROUTE,
|
2019-03-22 00:03:30 +01:00
|
|
|
} from '../../../../helpers/constants/routes'
|
|
|
|
import { exportAsFile } from '../../../../helpers/utils/util'
|
2019-05-07 17:03:27 +02:00
|
|
|
import DraggableSeed from './draggable-seed.component'
|
|
|
|
|
|
|
|
const EMPTY_SEEDS = Array(12).fill(null)
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
export default class ConfirmSeedPhrase extends PureComponent {
|
|
|
|
static contextTypes = {
|
2019-03-05 16:45:01 +01:00
|
|
|
metricsEvent: PropTypes.func,
|
2019-01-23 16:25:34 +01:00
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
seedPhrase: '',
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
2019-12-06 16:32:03 +01:00
|
|
|
hideSeedPhraseBackupAfterOnboarding: PropTypes.func,
|
2019-01-23 16:25:34 +01:00
|
|
|
history: PropTypes.object,
|
|
|
|
seedPhrase: PropTypes.string,
|
2019-09-16 19:11:01 +02:00
|
|
|
initializeThreeBox: PropTypes.func,
|
2019-12-06 16:32:03 +01:00
|
|
|
setSeedPhraseBackedUp: PropTypes.func,
|
|
|
|
showingSeedPhraseBackupAfterOnboarding: PropTypes.bool,
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
2019-05-07 17:03:27 +02:00
|
|
|
selectedSeedIndices: [],
|
2020-01-29 20:42:46 +01:00
|
|
|
sortedSeedWords: [],
|
2019-05-07 17:03:27 +02:00
|
|
|
pendingSeedIndices: [],
|
|
|
|
draggingSeedIndex: -1,
|
|
|
|
hoveringIndex: -1,
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
const { seedPhrase = '' } = this.props
|
2020-01-29 20:42:46 +01:00
|
|
|
const sortedSeedWords = (seedPhrase.split(' ') || []).sort()
|
|
|
|
this.setState({ sortedSeedWords })
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
setDraggingSeedIndex = (draggingSeedIndex) => this.setState({ draggingSeedIndex })
|
2019-05-07 17:03:27 +02:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
setHoveringIndex = (hoveringIndex) => this.setState({ hoveringIndex })
|
2019-05-07 17:03:27 +02:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
onDrop = (targetIndex) => {
|
2019-05-07 17:03:27 +02:00
|
|
|
const {
|
|
|
|
selectedSeedIndices,
|
|
|
|
draggingSeedIndex,
|
|
|
|
} = this.state
|
|
|
|
|
|
|
|
const indices = insert(selectedSeedIndices, draggingSeedIndex, targetIndex, true)
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
selectedSeedIndices: indices,
|
|
|
|
pendingSeedIndices: indices,
|
|
|
|
draggingSeedIndex: -1,
|
|
|
|
hoveringIndex: -1,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:25:34 +01:00
|
|
|
handleExport = () => {
|
2020-01-22 02:22:55 +01:00
|
|
|
exportAsFile('', this.props.seedPhrase, 'text/plain')
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit = async () => {
|
2019-09-16 19:11:01 +02:00
|
|
|
const {
|
|
|
|
history,
|
|
|
|
setSeedPhraseBackedUp,
|
|
|
|
showingSeedPhraseBackupAfterOnboarding,
|
|
|
|
hideSeedPhraseBackupAfterOnboarding,
|
|
|
|
initializeThreeBox,
|
|
|
|
} = this.props
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
if (!this.isValid()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-03-05 16:45:01 +01:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Onboarding',
|
|
|
|
action: 'Seed Phrase Setup',
|
|
|
|
name: 'Verify Complete',
|
|
|
|
},
|
|
|
|
})
|
2019-08-02 05:57:26 +02:00
|
|
|
|
|
|
|
setSeedPhraseBackedUp(true).then(() => {
|
|
|
|
if (showingSeedPhraseBackupAfterOnboarding) {
|
|
|
|
hideSeedPhraseBackupAfterOnboarding()
|
|
|
|
history.push(DEFAULT_ROUTE)
|
|
|
|
} else {
|
2019-09-16 19:11:01 +02:00
|
|
|
initializeThreeBox()
|
2019-08-02 05:57:26 +02:00
|
|
|
history.push(INITIALIZE_END_OF_FLOW_ROUTE)
|
|
|
|
}
|
|
|
|
})
|
2019-01-23 16:25:34 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 20:42:46 +01:00
|
|
|
handleSelectSeedWord = (index) => {
|
2019-05-07 17:03:27 +02:00
|
|
|
this.setState({
|
2020-01-29 20:42:46 +01:00
|
|
|
selectedSeedIndices: [...this.state.selectedSeedIndices, index],
|
|
|
|
pendingSeedIndices: [...this.state.pendingSeedIndices, index],
|
2019-05-07 17:03:27 +02:00
|
|
|
})
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 20:42:46 +01:00
|
|
|
handleDeselectSeedWord = (index) => {
|
2019-05-07 17:03:27 +02:00
|
|
|
this.setState({
|
2020-02-15 21:34:12 +01:00
|
|
|
selectedSeedIndices: this.state.selectedSeedIndices.filter((i) => index !== i),
|
|
|
|
pendingSeedIndices: this.state.pendingSeedIndices.filter((i) => index !== i),
|
2019-05-07 17:03:27 +02:00
|
|
|
})
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isValid () {
|
|
|
|
const { seedPhrase } = this.props
|
2020-01-29 20:42:46 +01:00
|
|
|
const { selectedSeedIndices, sortedSeedWords } = this.state
|
2020-02-15 21:34:12 +01:00
|
|
|
const selectedSeedWords = selectedSeedIndices.map((i) => sortedSeedWords[i])
|
2019-01-23 16:25:34 +01:00
|
|
|
return seedPhrase === selectedSeedWords.join(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { t } = this.context
|
2019-02-27 15:46:41 +01:00
|
|
|
const { history } = this.props
|
2019-05-07 17:03:27 +02:00
|
|
|
const {
|
|
|
|
selectedSeedIndices,
|
2020-01-29 20:42:46 +01:00
|
|
|
sortedSeedWords,
|
2019-05-07 17:03:27 +02:00
|
|
|
draggingSeedIndex,
|
|
|
|
} = this.state
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
return (
|
2019-02-27 15:46:41 +01:00
|
|
|
<div className="confirm-seed-phrase">
|
2019-01-23 16:25:34 +01:00
|
|
|
<div className="confirm-seed-phrase__back-button">
|
|
|
|
<a
|
2020-02-15 21:34:12 +01:00
|
|
|
onClick={(e) => {
|
2019-01-23 16:25:34 +01:00
|
|
|
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>
|
2019-05-07 17:03:27 +02:00
|
|
|
<div
|
|
|
|
className={classnames('confirm-seed-phrase__selected-seed-words', {
|
|
|
|
'confirm-seed-phrase__selected-seed-words--dragging': draggingSeedIndex > -1,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{ this.renderPendingSeeds() }
|
|
|
|
{ this.renderSelectedSeeds() }
|
2019-01-23 16:25:34 +01:00
|
|
|
</div>
|
2020-01-29 20:42:46 +01:00
|
|
|
<div className="confirm-seed-phrase__sorted-seed-words" data-testid="seed-phrase-sorted">
|
2019-01-23 16:25:34 +01:00
|
|
|
{
|
2020-01-29 20:42:46 +01:00
|
|
|
sortedSeedWords.map((word, index) => {
|
2019-05-07 17:03:27 +02:00
|
|
|
const isSelected = selectedSeedIndices.includes(index)
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
return (
|
2019-05-07 17:03:27 +02:00
|
|
|
<DraggableSeed
|
2019-01-23 16:25:34 +01:00
|
|
|
key={index}
|
2019-05-07 17:03:27 +02:00
|
|
|
seedIndex={index}
|
|
|
|
index={index}
|
|
|
|
setHoveringIndex={this.setHoveringIndex}
|
|
|
|
onDrop={this.onDrop}
|
2020-01-29 20:42:46 +01:00
|
|
|
className="confirm-seed-phrase__seed-word--sorted"
|
2019-05-07 17:03:27 +02:00
|
|
|
selected={isSelected}
|
2019-01-23 16:25:34 +01:00
|
|
|
onClick={() => {
|
|
|
|
if (!isSelected) {
|
2019-05-08 21:51:33 +02:00
|
|
|
this.handleSelectSeedWord(index)
|
2019-01-23 16:25:34 +01:00
|
|
|
} else {
|
|
|
|
this.handleDeselectSeedWord(index)
|
|
|
|
}
|
|
|
|
}}
|
2019-05-07 17:03:27 +02:00
|
|
|
word={word}
|
|
|
|
/>
|
2019-01-23 16:25:34 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
<Button
|
2019-04-16 21:35:22 +02:00
|
|
|
type="primary"
|
2019-01-23 16:25:34 +01:00
|
|
|
className="first-time-flow__button"
|
|
|
|
onClick={this.handleSubmit}
|
|
|
|
disabled={!this.isValid()}
|
|
|
|
>
|
|
|
|
{ t('confirm') }
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2019-05-07 17:03:27 +02:00
|
|
|
|
|
|
|
renderSelectedSeeds () {
|
2020-01-29 20:42:46 +01:00
|
|
|
const { sortedSeedWords, selectedSeedIndices, draggingSeedIndex } = this.state
|
2019-05-07 17:03:27 +02:00
|
|
|
return EMPTY_SEEDS.map((_, index) => {
|
|
|
|
const seedIndex = selectedSeedIndices[index]
|
2020-01-29 20:42:46 +01:00
|
|
|
const word = sortedSeedWords[seedIndex]
|
2019-05-07 17:03:27 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<DraggableSeed
|
|
|
|
key={`selected-${seedIndex}-${index}`}
|
|
|
|
className="confirm-seed-phrase__selected-seed-words__selected-seed"
|
|
|
|
index={index}
|
|
|
|
seedIndex={seedIndex}
|
|
|
|
word={word}
|
|
|
|
draggingSeedIndex={draggingSeedIndex}
|
|
|
|
setDraggingSeedIndex={this.setDraggingSeedIndex}
|
|
|
|
setHoveringIndex={this.setHoveringIndex}
|
|
|
|
onDrop={this.onDrop}
|
|
|
|
draggable
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPendingSeeds () {
|
|
|
|
const {
|
|
|
|
pendingSeedIndices,
|
2020-01-29 20:42:46 +01:00
|
|
|
sortedSeedWords,
|
2019-05-07 17:03:27 +02:00
|
|
|
draggingSeedIndex,
|
|
|
|
hoveringIndex,
|
|
|
|
} = this.state
|
|
|
|
|
|
|
|
const indices = insert(pendingSeedIndices, draggingSeedIndex, hoveringIndex)
|
|
|
|
|
|
|
|
return EMPTY_SEEDS.map((_, index) => {
|
|
|
|
const seedIndex = indices[index]
|
2020-01-29 20:42:46 +01:00
|
|
|
const word = sortedSeedWords[seedIndex]
|
2019-05-07 17:03:27 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<DraggableSeed
|
|
|
|
key={`pending-${seedIndex}-${index}`}
|
|
|
|
index={index}
|
|
|
|
className={classnames('confirm-seed-phrase__selected-seed-words__pending-seed', {
|
|
|
|
'confirm-seed-phrase__seed-word--hidden': draggingSeedIndex === seedIndex && index !== hoveringIndex,
|
|
|
|
})}
|
|
|
|
seedIndex={seedIndex}
|
|
|
|
word={word}
|
|
|
|
draggingSeedIndex={draggingSeedIndex}
|
|
|
|
setDraggingSeedIndex={this.setDraggingSeedIndex}
|
|
|
|
setHoveringIndex={this.setHoveringIndex}
|
|
|
|
onDrop={this.onDrop}
|
|
|
|
droppable={!!word}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function insert (list, value, target, removeOld) {
|
|
|
|
let nextList = [...list]
|
|
|
|
|
|
|
|
if (typeof list[target] === 'number') {
|
|
|
|
nextList = [...list.slice(0, target), value, ...list.slice(target)]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removeOld) {
|
|
|
|
nextList = nextList.filter((seed, i) => {
|
|
|
|
return seed !== value || i === target
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextList
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|