Skip to content

Commit 29372db

Browse files
committed
feat(neuron-ui): add dynamic prmopt in wallet wizard
1 parent 630c6f1 commit 29372db

5 files changed

Lines changed: 77 additions & 24 deletions

File tree

packages/neuron-ui/src/components/WalletWizard/index.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import React, { useCallback, useEffect } from 'react'
1+
import React, { useCallback, useEffect, useMemo } from 'react'
22
import { useTranslation } from 'react-i18next'
3-
import { Stack, Text, Label, Image, PrimaryButton, DefaultButton, TextField, FontSizes } from 'office-ui-fabric-react'
3+
import {
4+
Stack,
5+
Icon,
6+
Text,
7+
Label,
8+
Image,
9+
PrimaryButton,
10+
DefaultButton,
11+
TextField,
12+
FontSizes,
13+
} from 'office-ui-fabric-react'
414

515
import withWizard, { WizardElementProps, WithWizardState } from 'components/withWizard'
616
import { generateMnemonic, validateMnemonic, showErrorMessage } from 'services/remote'
717
import { createWalletWithMnemonic, importWalletWithMnemonic } from 'states/stateProvider/actionCreators'
818

919
import { Routes, MnemonicAction, BUTTON_GAP } from 'utils/const'
1020
import { buttonGrommetIconStyles } from 'utils/icons'
11-
import { verifyWalletSubmission } from 'utils/validators'
21+
import { verifyPasswordComplexity } from 'utils/validators'
1222

1323
export enum WalletWizardPath {
1424
Welcome = '/welcome',
@@ -243,7 +253,10 @@ const Submission = ({
243253
}
244254
}, [type, name, password, imported, history, dispatch])
245255

246-
const disableNext = !verifyWalletSubmission({ name, password, confirmPassword })
256+
const isNameUnused = useMemo(() => name && !wallets.find(w => w.name === name), [name, wallets])
257+
const isPwdComplex = useMemo(() => verifyPasswordComplexity(password) === true, [password])
258+
const isPwdSame = useMemo(() => password && password === confirmPassword, [password, confirmPassword])
259+
const disableNext = !(isNameUnused && isPwdComplex && isPwdSame)
247260

248261
return (
249262
<Stack verticalFill verticalAlign="center" horizontalAlign="stretch" tokens={{ childrenGap: 15 }}>
@@ -261,6 +274,21 @@ const Submission = ({
261274
</div>
262275
))}
263276

277+
<Stack>
278+
<Stack horizontal tokens={{ childrenGap: 3 }}>
279+
{isNameUnused ? <Icon iconName="Matched" /> : <Icon iconName="Unmatched" />}
280+
<Text variant="xSmall">{t('wizard.new-name')}</Text>
281+
</Stack>
282+
<Stack horizontal tokens={{ childrenGap: 3 }}>
283+
{isPwdComplex ? <Icon iconName="Matched" /> : <Icon iconName="Unmatched" />}
284+
<Text variant="xSmall">{t('wizard.complex-password')}</Text>
285+
</Stack>
286+
<Stack horizontal tokens={{ childrenGap: 3 }}>
287+
{isPwdSame ? <Icon iconName="Matched" /> : <Icon iconName="Unmatched" />}
288+
<Text variant="xSmall">{t('wizard.same-password')}</Text>
289+
</Stack>
290+
</Stack>
291+
264292
<Stack horizontal horizontalAlign="end" tokens={{ childrenGap: BUTTON_GAP }}>
265293
<DefaultButton onClick={history.goBack} text={t('wizard.back')} />
266294
<PrimaryButton onClick={onNext} disabled={disableNext} text={t('wizard.next')} />

packages/neuron-ui/src/locales/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@
4747
"set-wallet-name": "Give your new wallet a name",
4848
"set-a-strong-password-to-protect-your-wallet": "Create a strong password to protect your wallet",
4949
"wallet-suffix": "Wallet {{suffix}}",
50-
"write-down-seed": "Write down your wallet seed and save it in a safe place"
50+
"write-down-seed": "Write down your wallet seed and save it in a safe place",
51+
"new-name": "Use an unused name for the new wallet",
52+
"complex-password": "The password is a string of 8 to 50 characters consisting of three types of characters: uppercase letters, lowercase letters, numbers, and special symbols, and must start with a letter or a number.",
53+
"same-password": "Confirm the password"
5154
},
5255
"detail": {
5356
"more-transactions": "More"

packages/neuron-ui/src/locales/zh.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@
4747
"set-wallet-name": "为钱包设置名称",
4848
"set-a-strong-password-to-protect-your-wallet": "请设置一个强密码用于保护您的钱包",
4949
"wallet-suffix": "钱包 {{suffix}}",
50-
"write-down-seed": "请记下您的助记词并保存到安全的地方"
50+
"write-down-seed": "请记下您的助记词并保存到安全的地方",
51+
"new-name": "输入新的钱包名称",
52+
"complex-password": "密码为 8 至 50 位由大写字母、小写字母、数字、特殊符号中三类字符组成的字符串, 且必须以字母或者数字开头",
53+
"same-password": "两次输入密码应一致"
5154
},
5255
"detail": {
5356
"more-transactions": "更多交易记录"

packages/neuron-ui/src/utils/loadTheme.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
Nodes as ConnectedIcon,
2323
Scan as ScanIcon,
2424
Search as SearchIcon,
25+
StatusGood as MatchedIcon,
2526
SubtractCircle as RemoveIcon,
2627
Update as UpdateIcon,
2728
} from 'grommet-icons'
@@ -73,6 +74,8 @@ registerIcons({
7374
Disconnected: <AlertIcon size="small" color="red" />,
7475
Updating: <UpdateIcon size="16px" style={{ animation: 'rotate360 3s linear infinite' }} />,
7576
More: <MoreIcon size="16px" />,
77+
Matched: <MatchedIcon size="16px" color="green" />,
78+
Unmatched: <InfoIcon size="16px" color="#d50000" />,
7679
},
7780
})
7881

packages/neuron-ui/src/utils/validators.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,46 @@ export const verifyAddress = (address: string): boolean => {
44
// TODO: verify address, prd required
55
return address.length === ADDRESS_LENGTH
66
}
7-
export const verifyWalletSubmission = ({
8-
password,
9-
confirmPassword,
10-
name,
11-
}: {
12-
password: string
13-
confirmPassword: string
14-
name: string
15-
}) => {
16-
return (
17-
password &&
18-
name &&
19-
password === confirmPassword &&
20-
password.length >= MIN_PASSWORD_LENGTH &&
21-
password.length <= MAX_PASSWORD_LENGTH
22-
)
23-
}
247

258
export const verifyAmountRange = (amount: string) => {
269
return +amount >= MIN_AMOUNT
2710
}
2811

12+
export const verifyPasswordComplexity = (password: string) => {
13+
if (!password) {
14+
return 'password-is-empty'
15+
}
16+
if (password.length < MIN_PASSWORD_LENGTH) {
17+
return 'password-is-too-short'
18+
}
19+
if (password.length > MAX_PASSWORD_LENGTH) {
20+
return 'password-is-too-long'
21+
}
22+
let complex = 0
23+
let reg = /\d/
24+
if (reg.test(password)) {
25+
complex++
26+
}
27+
reg = /[a-z]/
28+
if (reg.test(password)) {
29+
complex++
30+
}
31+
reg = /[A-Z]/
32+
if (reg.test(password)) {
33+
complex++
34+
}
35+
reg = /[^0-9a-zA-Z]/
36+
if (reg.test(password)) {
37+
complex++
38+
}
39+
if (complex < 3) {
40+
return 'password-is-too-simple'
41+
}
42+
return true
43+
}
44+
2945
export default {
3046
verifyAddress,
31-
verifyWalletSubmission,
3247
verifyAmountRange,
48+
verifyPasswordComplexity,
3349
}

0 commit comments

Comments
 (0)