|
| 1 | +import React, { useState, useCallback, useMemo } from 'react' |
| 2 | +import { RouteComponentProps } from 'react-router-dom' |
| 3 | +import { Stack, DefaultButton, PrimaryButton, TextField } from 'office-ui-fabric-react' |
| 4 | +import { useTranslation } from 'react-i18next' |
| 5 | +import { showOpenDialog } from 'services/remote' |
| 6 | +import { importWalletWithKeystore } from 'states/stateProvider/actionCreators' |
| 7 | +import { StateWithDispatch } from 'states/stateProvider/reducer' |
| 8 | +import { useGoBack } from 'utils/hooks' |
| 9 | + |
| 10 | +const defaultFields = { |
| 11 | + path: '', |
| 12 | + name: '', |
| 13 | + password: '', |
| 14 | +} |
| 15 | + |
| 16 | +const ImportKeystore = (props: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps>) => { |
| 17 | + const [t] = useTranslation() |
| 18 | + const { |
| 19 | + history, |
| 20 | + dispatch, |
| 21 | + settings: { wallets }, |
| 22 | + } = props |
| 23 | + const [fields, setFields] = useState(defaultFields) |
| 24 | + const goBack = useGoBack(history) |
| 25 | + |
| 26 | + const exsitingNames = useMemo(() => { |
| 27 | + return wallets.map(w => w.name) |
| 28 | + }, [wallets]) |
| 29 | + |
| 30 | + const onFileClick = useCallback(() => { |
| 31 | + showOpenDialog({ |
| 32 | + title: 'import keystore', |
| 33 | + onUpload: (filePaths: string[]) => { |
| 34 | + if (!filePaths || filePaths.length === 0) { |
| 35 | + return |
| 36 | + } |
| 37 | + const filePath = filePaths[0] |
| 38 | + const filename = filePath.split('/').pop() || 'Imported wallet' |
| 39 | + setFields({ |
| 40 | + ...fields, |
| 41 | + path: filePath, |
| 42 | + name: filename, |
| 43 | + }) |
| 44 | + }, |
| 45 | + }) |
| 46 | + }, [fields]) |
| 47 | + |
| 48 | + const onSubmit = useCallback(() => { |
| 49 | + importWalletWithKeystore({ |
| 50 | + name: fields.name, |
| 51 | + keystorePath: fields.path, |
| 52 | + password: fields.password, |
| 53 | + })(dispatch, history) |
| 54 | + }, [fields.name, fields.password, fields.path, history, dispatch]) |
| 55 | + |
| 56 | + return ( |
| 57 | + <Stack tokens={{ childrenGap: 15 }}> |
| 58 | + <Stack tokens={{ childrenGap: 15 }}> |
| 59 | + {Object.entries(fields).map(([key, value]) => { |
| 60 | + return ( |
| 61 | + <TextField |
| 62 | + key={key} |
| 63 | + onClick={key === 'path' ? onFileClick : undefined} |
| 64 | + label={t(`import-keystore.label.${key}`)} |
| 65 | + placeholder={t(`import-keystore.placeholder.${key}`)} |
| 66 | + type={key === 'password' ? 'password' : 'text'} |
| 67 | + readOnly={key === 'path'} |
| 68 | + value={value} |
| 69 | + validateOnLoad={false} |
| 70 | + onGetErrorMessage={(text?: string) => { |
| 71 | + if (text === '') { |
| 72 | + return t('messages.is-required', { field: t(`import-keystore.label.${key}`) }) |
| 73 | + } |
| 74 | + if (key === 'name' && exsitingNames.includes(text || '')) { |
| 75 | + return t('messages.is-used', { field: t(`import-keystore.label.${key}`) }) |
| 76 | + } |
| 77 | + return '' |
| 78 | + }} |
| 79 | + onChange={(_e: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue?: string) => { |
| 80 | + if (newValue !== undefined) { |
| 81 | + setFields({ |
| 82 | + ...fields, |
| 83 | + [key]: newValue, |
| 84 | + }) |
| 85 | + } |
| 86 | + }} |
| 87 | + /> |
| 88 | + ) |
| 89 | + })} |
| 90 | + </Stack> |
| 91 | + <Stack horizontal horizontalAlign="end" tokens={{ childrenGap: 15 }}> |
| 92 | + <DefaultButton onClick={goBack}>{t('import-keystore.button.back')}</DefaultButton> |
| 93 | + <PrimaryButton disabled={!(fields.name && fields.path && fields.password)} onClick={onSubmit}> |
| 94 | + {t('import-keystore.button.submit')} |
| 95 | + </PrimaryButton> |
| 96 | + </Stack> |
| 97 | + </Stack> |
| 98 | + ) |
| 99 | +} |
| 100 | + |
| 101 | +ImportKeystore.displayName = 'ImportKeystore' |
| 102 | +export default ImportKeystore |
0 commit comments