This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Recover.js
149 lines (134 loc) · 4.16 KB
/
Recover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* eslint-disable react/no-multi-comp */
import React from 'react'
import PropTypes from 'prop-types'
import { FormattedMessage, intlShape, injectIntl } from 'react-intl'
import bip39 from 'bip39-en'
import { Flex } from 'rebass'
import parseSeed from '@zap/utils/parseSeed'
import { Bar, Form, Header, Input, Label } from 'components/UI'
import messages from './messages'
class SeedWord extends React.Component {
static propTypes = {
index: PropTypes.number.isRequired,
intl: intlShape.isRequired,
onPaste: PropTypes.func,
word: PropTypes.string,
}
validateWord = value => {
return !value || !bip39.includes(value) ? 'incorrect' : undefined
}
render() {
const { index, intl, onPaste, word } = this.props
return (
<Flex key={`word${index}`} alignItems="center" as="li" justifyContent="flex-start" my={1}>
<Label htmlFor={`word${index}`} mb={0} width={35}>
{index}
</Label>
<Input
field={`word${index}`}
hasMessage={false}
initialValue={word}
onPaste={onPaste}
placeholder={intl.formatMessage({ ...messages.word_placeholder })}
validate={this.validateWord}
validateOnChange
variant="thin"
willAutoFocus={index === 1}
/>
</Flex>
)
}
}
const SeedWordWithIntl = injectIntl(SeedWord)
class Recover extends React.Component {
static propTypes = {
intl: intlShape.isRequired,
seed: PropTypes.array.isRequired,
setSeed: PropTypes.func.isRequired,
wizardApi: PropTypes.object,
wizardState: PropTypes.object,
}
static defaultProps = {
wizardApi: {},
wizardState: {},
}
handleSubmit = values => {
const { setSeed } = this.props
setSeed(Object.values(values))
}
/**
* Parse valid seed from the clipboard and use to set values on all inputs.
* @param {Event} event onPaste event
*/
handlePaste = event => {
const seedWords = parseSeed(event.clipboardData.getData('text'))
if (seedWords.length === 24) {
event.preventDefault()
seedWords.forEach((word, index) => {
this.formApi.setValue(`word${index + 1}`, word)
this.formApi.setTouched(`word${index + 1}`, true)
})
}
}
setFormApi = formApi => {
this.formApi = formApi
}
render() {
const { wizardApi, wizardState, seed, setSeed, intl, ...rest } = this.props
const { getApi, onSubmit, onSubmitFailure } = wizardApi
const indexes = Array.from(Array(24).keys())
return (
<Form
{...rest}
getApi={formApi => {
this.setFormApi(formApi)
if (getApi) {
getApi(formApi)
}
}}
onSubmit={values => {
this.handleSubmit(values)
if (onSubmit) {
onSubmit(values)
}
}}
onSubmitFailure={onSubmitFailure}
>
<Header
align="left"
subtitle={<FormattedMessage {...messages.import_description} />}
title={<FormattedMessage {...messages.import_title} />}
/>
<Bar my={4} />
<Flex justifyContent="space-between">
<Flex as="ul" flexDirection="column" mr={2}>
{indexes.slice(0, 6).map(word => (
<SeedWordWithIntl
key={word + 1}
index={word + 1}
onPaste={word === 0 ? this.handlePaste : null}
word={seed[word]}
/>
))}
</Flex>
<Flex as="ul" flexDirection="column" mx={2}>
{indexes.slice(6, 12).map(word => (
<SeedWordWithIntl key={word + 1} index={word + 1} word={seed[word]} />
))}
</Flex>
<Flex as="ul" flexDirection="column" mx={2}>
{indexes.slice(12, 18).map(word => (
<SeedWordWithIntl key={word + 1} index={word + 1} word={seed[word]} />
))}
</Flex>
<Flex as="ul" flexDirection="column" ml={2}>
{indexes.slice(18, 24).map(word => (
<SeedWordWithIntl key={word + 1} index={word + 1} word={seed[word]} />
))}
</Flex>
</Flex>
</Form>
)
}
}
export default injectIntl(Recover)