-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #874 from nervosnetwork/rc/v0.18.0-beta.0
[ᚬmaster] Rc/v0.18.0 beta.0
- Loading branch information
Showing
42 changed files
with
791 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/neuron-ui/src/components/ImportKeystore/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import React, { useState, useCallback, useMemo } from 'react' | ||
import { RouteComponentProps } from 'react-router-dom' | ||
import { Stack, DefaultButton, PrimaryButton, TextField } from 'office-ui-fabric-react' | ||
import { useTranslation } from 'react-i18next' | ||
import { showOpenDialog } from 'services/remote' | ||
import { importWalletWithKeystore } from 'states/stateProvider/actionCreators' | ||
import { StateWithDispatch } from 'states/stateProvider/reducer' | ||
import { useGoBack } from 'utils/hooks' | ||
|
||
const defaultFields = { | ||
path: '', | ||
name: '', | ||
password: '', | ||
} | ||
|
||
const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => { | ||
const [t] = useTranslation() | ||
const { | ||
history, | ||
dispatch, | ||
settings: { wallets }, | ||
} = props | ||
const [fields, setFields] = useState(defaultFields) | ||
const goBack = useGoBack(history) | ||
|
||
const exsitingNames = useMemo(() => { | ||
return wallets.map(w => w.name) | ||
}, [wallets]) | ||
|
||
const isNameUsed = useMemo(() => { | ||
return exsitingNames.includes(fields.name || '') | ||
}, [exsitingNames, fields.name]) | ||
|
||
const onFileClick = useCallback(() => { | ||
showOpenDialog({ | ||
title: 'import keystore', | ||
onUpload: (filePaths: string[]) => { | ||
if (!filePaths || filePaths.length === 0) { | ||
return | ||
} | ||
const filePath = filePaths[0] | ||
const filename = filePath.split('/').pop() || 'Imported wallet' | ||
setFields({ | ||
...fields, | ||
path: filePath, | ||
name: filename, | ||
}) | ||
}, | ||
}) | ||
}, [fields]) | ||
|
||
const onSubmit = useCallback(() => { | ||
importWalletWithKeystore({ | ||
name: fields.name, | ||
keystorePath: fields.path, | ||
password: fields.password, | ||
})(dispatch, history) | ||
}, [fields.name, fields.password, fields.path, history, dispatch]) | ||
|
||
return ( | ||
<Stack verticalFill verticalAlign="center" tokens={{ childrenGap: 15 }}> | ||
<Stack tokens={{ childrenGap: 15 }}> | ||
{Object.entries(fields).map(([key, value]) => { | ||
return ( | ||
<TextField | ||
key={key} | ||
onClick={key === 'path' ? onFileClick : undefined} | ||
label={t(`import-keystore.label.${key}`)} | ||
placeholder={t(`import-keystore.placeholder.${key}`)} | ||
type={key === 'password' ? 'password' : 'text'} | ||
readOnly={key === 'path'} | ||
value={value} | ||
validateOnLoad={false} | ||
onGetErrorMessage={(text?: string) => { | ||
if (text === '') { | ||
return t('messages.is-required', { field: t(`import-keystore.label.${key}`) }) | ||
} | ||
if (key === 'name' && isNameUsed) { | ||
return t('messages.is-used', { field: t(`import-keystore.label.${key}`) }) | ||
} | ||
return '' | ||
}} | ||
onChange={(_e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string) => { | ||
if (newValue !== undefined) { | ||
setFields({ | ||
...fields, | ||
[key]: newValue, | ||
}) | ||
} | ||
}} | ||
/> | ||
) | ||
})} | ||
</Stack> | ||
<Stack horizontal horizontalAlign="end" tokens={{ childrenGap: 15 }}> | ||
<DefaultButton onClick={goBack}>{t('import-keystore.button.back')}</DefaultButton> | ||
<PrimaryButton disabled={!(fields.name && fields.path && fields.password && !isNameUsed)} onClick={onSubmit}> | ||
{t('import-keystore.button.submit')} | ||
</PrimaryButton> | ||
</Stack> | ||
</Stack> | ||
) | ||
} | ||
|
||
ImportKeystore.displayName = 'ImportKeystore' | ||
export default ImportKeystore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.