Skip to content

Commit 8508965

Browse files
committed
feat(neuron-ui): add loading effects on creating/import wallets
1 parent b7f1b5a commit 8508965

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteCo
2929
settings: { wallets },
3030
} = props
3131
const [fields, setFields] = useState(defaultFields)
32+
const [loading, setLoading] = useState(false)
3233
const goBack = useGoBack(history)
3334

3435
useEffect(() => {
@@ -66,12 +67,18 @@ const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteCo
6667
}, [fields])
6768

6869
const onSubmit = useCallback(() => {
69-
importWalletWithKeystore({
70-
name: fields.name || '',
71-
keystorePath: fields.path,
72-
password: fields.password,
73-
})(dispatch, history)
74-
}, [fields.name, fields.password, fields.path, history, dispatch])
70+
if (loading) {
71+
return
72+
}
73+
setLoading(true)
74+
setTimeout(() => {
75+
importWalletWithKeystore({
76+
name: fields.name || '',
77+
keystorePath: fields.path,
78+
password: fields.password,
79+
})(dispatch, history).finally(() => setLoading(false))
80+
}, 200)
81+
}, [fields.name, fields.password, fields.path, history, dispatch, loading])
7582

7683
return (
7784
<Stack verticalFill verticalAlign="center" tokens={{ childrenGap: 15 }}>
@@ -117,7 +124,10 @@ const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteCo
117124
</Stack>
118125
<Stack horizontal horizontalAlign="end" tokens={{ childrenGap: 15 }}>
119126
<DefaultButton onClick={goBack}>{t('import-keystore.button.back')}</DefaultButton>
120-
<PrimaryButton disabled={!(fields.name && fields.path && fields.password && !isNameUsed)} onClick={onSubmit}>
127+
<PrimaryButton
128+
disabled={loading || !(fields.name && fields.path && fields.password && !isNameUsed)}
129+
onClick={onSubmit}
130+
>
121131
{t('import-keystore.button.submit')}
122132
</PrimaryButton>
123133
</Stack>

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect, useMemo } from 'react'
1+
import React, { useState, useCallback, useEffect, useMemo } from 'react'
22
import { useTranslation } from 'react-i18next'
33
import {
44
Stack,
@@ -224,9 +224,13 @@ const Submission = ({
224224
}: WizardElementProps<{ type: string }>) => {
225225
const { name, password, confirmPassword, imported } = state
226226
const [t] = useTranslation()
227+
const [loading, setLoading] = useState(false)
227228
const message = 'wizard.set-wallet-name-and-password'
228229

229230
useEffect(() => {
231+
if (loading) {
232+
return
233+
}
230234
dispatch({
231235
type: 'name',
232236
payload: generateWalletName(wallets, wallets.length + 1, t),
@@ -239,7 +243,7 @@ const Submission = ({
239243
type: 'confirmPassword',
240244
payload: '',
241245
})
242-
}, [dispatch, wallets, t])
246+
}, [loading, dispatch, wallets, t])
243247

244248
const onChange = useCallback(
245249
(e: any, value?: string) => {
@@ -258,22 +262,28 @@ const Submission = ({
258262
)
259263

260264
const onNext = useCallback(() => {
265+
if (loading) {
266+
return
267+
}
261268
const p = {
262269
name,
263270
password,
264271
mnemonic: imported,
265272
}
266-
if (type === MnemonicAction.Create) {
267-
createWalletWithMnemonic(p)(dispatch, history)
268-
} else {
269-
importWalletWithMnemonic(p)(dispatch, history)
270-
}
271-
}, [type, name, password, imported, history, dispatch])
273+
setLoading(true)
274+
setTimeout(() => {
275+
if (type === MnemonicAction.Create) {
276+
createWalletWithMnemonic(p)(dispatch, history).finally(() => setLoading(false))
277+
} else {
278+
importWalletWithMnemonic(p)(dispatch, history).finally(() => setLoading(false))
279+
}
280+
}, 0)
281+
}, [type, name, password, imported, history, dispatch, loading])
272282

273283
const isNameUnused = useMemo(() => name && !wallets.find(w => w.name === name), [name, wallets])
274284
const isPwdComplex = useMemo(() => verifyPasswordComplexity(password) === true, [password])
275285
const isPwdSame = useMemo(() => password && password === confirmPassword, [password, confirmPassword])
276-
const disableNext = !(isNameUnused && isPwdComplex && isPwdSame)
286+
const disableNext = loading || !(isNameUnused && isPwdComplex && isPwdSame)
277287

278288
return (
279289
<Stack verticalFill verticalAlign="center" horizontalAlign="stretch" tokens={{ childrenGap: 15 }}>

packages/neuron-ui/src/states/stateProvider/actionCreators/wallets.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const createWalletWithMnemonic = (params: Controller.ImportMnemonicParams
4545
_dispatch: StateDispatch,
4646
history: any
4747
) => {
48-
createWallet(params).then(res => {
48+
return createWallet(params).then(res => {
4949
if (res.status === 1) {
5050
history.push(Routes.Overview)
5151
} else if (res.status > 0) {
@@ -63,7 +63,7 @@ export const importWalletWithMnemonic = (params: Controller.ImportMnemonicParams
6363
_dispatch: StateDispatch,
6464
history: any
6565
) => {
66-
importMnemonic(params).then(res => {
66+
return importMnemonic(params).then(res => {
6767
if (res.status === 1) {
6868
history.push(Routes.Overview)
6969
} else if (res.status > 0) {
@@ -81,7 +81,7 @@ export const importWalletWithKeystore = (params: Controller.ImportKeystoreParams
8181
_dispatch: StateDispatch,
8282
history: any
8383
) => {
84-
importKeystore(params).then(res => {
84+
return importKeystore(params).then(res => {
8585
if (res.status === 1) {
8686
history.push(Routes.Overview)
8787
} else if (res.status > 0) {

0 commit comments

Comments
 (0)