From 23f6f2750ecb9dc41b6301d861f72897f3b8e095 Mon Sep 17 00:00:00 2001 From: Bao Jun Date: Mon, 13 May 2019 15:05:11 +0800 Subject: [PATCH 1/2] refactor: wallet update Remove checking password for editing wallet data in neuron-wallet --- .../neuron-wallet/src/controllers/wallets.ts | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/packages/neuron-wallet/src/controllers/wallets.ts b/packages/neuron-wallet/src/controllers/wallets.ts index 5a1e612b17..8e4d61c5dc 100644 --- a/packages/neuron-wallet/src/controllers/wallets.ts +++ b/packages/neuron-wallet/src/controllers/wallets.ts @@ -171,22 +171,30 @@ class WalletsController { try { const wallet = WalletsController.service.get(id) if (wallet) { - if (WalletsController.service.validate({ id, password })) { - const props: WalletProperties = { name, addresses: wallet.addresses, keystore: null } - if (newPassword) { + const props: WalletProperties = { + name: wallet.name, + addresses: wallet.addresses, + keystore: wallet.loadKeystore(), + } + if (newPassword) { + if (WalletsController.service.validate({ id, password })) { const key = Key.fromKeystore(JSON.stringify(wallet!.loadKeystore()), password) props.keystore = key.toKeystore(JSON.stringify(key.keysData!), newPassword) - } - WalletsController.service.update(id, props) - windowManage.broadcast(Channel.Wallets, WalletsMethod.GetAll, WalletsController.getAll()) - return { - status: ResponseCode.Success, - result: WalletsController.service.get(id), + } else { + return { + status: ResponseCode.Fail, + msg: 'Incorrect password', + } } } + if (name) { + props.name = name + } + WalletsController.service.update(id, props) + windowManage.broadcast(Channel.Wallets, WalletsMethod.GetAll, WalletsController.getAll()) return { - status: ResponseCode.Fail, - msg: 'Incorrect password', + status: ResponseCode.Success, + result: WalletsController.service.get(id), } } return { From a2dbcdf2697f9e554f5d54ee55f72a7f651536c6 Mon Sep 17 00:00:00 2001 From: Bao Jun Date: Mon, 13 May 2019 15:46:52 +0800 Subject: [PATCH 2/2] refactor: remove update name with password in ui remove update name with asking password in neuron-ui, but keep the logic about changing password --- .../src/components/WalletEditor/hooks.ts | 37 ++--------- .../src/components/WalletEditor/index.tsx | 65 +++---------------- .../MainContent/actionCreators/wallets.ts | 2 +- packages/neuron-ui/src/services/UILayer.ts | 2 +- 4 files changed, 17 insertions(+), 89 deletions(-) diff --git a/packages/neuron-ui/src/components/WalletEditor/hooks.ts b/packages/neuron-ui/src/components/WalletEditor/hooks.ts index 9a1b2a653a..0dfd082b36 100644 --- a/packages/neuron-ui/src/components/WalletEditor/hooks.ts +++ b/packages/neuron-ui/src/components/WalletEditor/hooks.ts @@ -4,9 +4,6 @@ import i18n from 'utils/i18n' export const useWalletEditor = () => { const [name, setName] = useState('') - const [password, setPassword] = useState('') - const [newPassword, setNewPassword] = useState('') - const [confirmNewPassword, setConfirmNewPassword] = useState('') const initialize = useCallback( (initName: string = '') => { setName(initName) @@ -19,22 +16,10 @@ export const useWalletEditor = () => { value: name, onChange: (e: React.FormEvent>) => setName(e.currentTarget.value), }, - password: { - value: password, - onChange: (e: React.FormEvent>) => setPassword(e.currentTarget.value), - }, - newPassword: { - value: newPassword, - onChange: (e: React.FormEvent>) => setNewPassword(e.currentTarget.value), - }, - confirmNewPassword: { - value: confirmNewPassword, - onChange: (e: React.FormEvent>) => setConfirmNewPassword(e.currentTarget.value), - }, } } -export const useInputs = ({ name, newPassword, confirmNewPassword }: ReturnType) => { +export const useInputs = ({ name }: ReturnType) => { return useMemo( () => [ { @@ -43,27 +28,15 @@ export const useInputs = ({ name, newPassword, confirmNewPassword }: ReturnType< placeholder: i18n.t('settings.wallet-manager.edit-wallet.wallet-name'), maxLength: 20, }, - { - ...newPassword, - label: i18n.t('settings.wallet-manager.edit-wallet.new-password'), - placeholder: i18n.t('settings.wallet-manager.edit-wallet.password'), - inputType: 'password', - }, - { - ...confirmNewPassword, - label: i18n.t('settings.wallet-manager.edit-wallet.confirm-password'), - placeholder: i18n.t('settings.wallet-manager.edit-wallet.confirm-password'), - inputType: 'password', - }, ], - [name, newPassword, confirmNewPassword], + [name], ) } -export const useAreParamsValid = (name: string, password: string, confirmPassword: string) => { +export const useAreParamsValid = (name: string) => { return useMemo(() => { - return !(password === '' || confirmPassword === '' || password !== confirmPassword || name === '') - }, [name, password, confirmPassword]) + return !(name === '') + }, [name]) } export const useToggleDialog = (dispatch: React.Dispatch) => diff --git a/packages/neuron-ui/src/components/WalletEditor/index.tsx b/packages/neuron-ui/src/components/WalletEditor/index.tsx index 9440f82945..5a12702f55 100644 --- a/packages/neuron-ui/src/components/WalletEditor/index.tsx +++ b/packages/neuron-ui/src/components/WalletEditor/index.tsx @@ -1,19 +1,14 @@ -import React, { useEffect, useMemo, useCallback } from 'react' -import { RouteComponentProps } from 'react-router-dom' -import { Card, Form, Button, Col, Row } from 'react-bootstrap' -import { useTranslation } from 'react-i18next' - import { ContentProps } from 'containers/MainContent' -import { useOnDialogCancel } from 'containers/MainContent/hooks' -import InlineInput, { InputProps } from 'widgets/InlineInput' -import { MainActions, actionCreators } from 'containers/MainContent/reducer' +import { actionCreators } from 'containers/MainContent/reducer' +import React, { useCallback, useEffect, useMemo } from 'react' +import { Button, Card, Form } from 'react-bootstrap' +import { useTranslation } from 'react-i18next' +import { RouteComponentProps } from 'react-router-dom' import { useNeuronWallet } from 'utils/hooks' -import Dialog from 'widgets/Dialog' - -import { useWalletEditor, useInputs, useAreParamsValid, useToggleDialog } from './hooks' +import InlineInput, { InputProps } from 'widgets/InlineInput' +import { useAreParamsValid, useInputs, useToggleDialog, useWalletEditor } from './hooks' export default ({ - dialog, dispatch, match: { params: { id }, @@ -39,31 +34,18 @@ export default ({ }, [id, initialize, wallet.name]) const inputs: InputProps[] = useInputs(editor) - const areParamsValid = useAreParamsValid(editor.name.value, editor.newPassword.value, editor.confirmNewPassword.value) + const areParamsValid = useAreParamsValid(editor.name.value) const toggleDialog = useToggleDialog(dispatch) - const handleSubmit = useCallback(() => { - dispatch({ - type: MainActions.SetDialog, - payload: { - open: true, - }, - }) - }, [dispatch]) - const handleConfirm = useCallback(() => { toggleDialog(false) dispatch( actionCreators.updateWallet({ id: wallet.id, - password: editor.password.value, - newPassword: editor.newPassword.value, name: editor.name.value, }), ) - }, [editor.name.value, editor.newPassword.value, editor.password.value, wallet.id, dispatch, toggleDialog]) - - const onCancel = useOnDialogCancel(dispatch) + }, [editor.name.value, wallet.id, dispatch, toggleDialog]) return ( @@ -74,37 +56,10 @@ export default ({ ))} - - - ) => { - e.preventDefault() - e.stopPropagation() - }} - > - <> - {t('settings.wallet-manager.delete-wallet-title', { name: wallet.name })} - - - - - - - - - - - - - - ) } diff --git a/packages/neuron-ui/src/containers/MainContent/actionCreators/wallets.ts b/packages/neuron-ui/src/containers/MainContent/actionCreators/wallets.ts index 109b874b8b..72ee59671b 100644 --- a/packages/neuron-ui/src/containers/MainContent/actionCreators/wallets.ts +++ b/packages/neuron-ui/src/containers/MainContent/actionCreators/wallets.ts @@ -37,7 +37,7 @@ export default { payload: id, } }, - updateWallet: (params: { id: string; password: string; newPassword?: string; name?: string }) => { + updateWallet: (params: { id: string; password?: string; newPassword?: string; name?: string }) => { walletsCall.update(params) return { type: MainActions.Wallet, diff --git a/packages/neuron-ui/src/services/UILayer.ts b/packages/neuron-ui/src/services/UILayer.ts index b2f331bcdc..95589f44d5 100644 --- a/packages/neuron-ui/src/services/UILayer.ts +++ b/packages/neuron-ui/src/services/UILayer.ts @@ -146,7 +146,7 @@ export const walletsCall = instantiateMethodCall(wallets) as { importKeystore: (params: { name: string; keystore: string; password: string }) => void importMnemonic: (params: { name: string; mnemonic: string; password: string }) => void create: (params: { name: string; mnemonic: string; password: string }) => void - update: (params: { id: string; password: string; newPassword?: string; name?: string }) => void + update: (params: { id: string; password?: string; newPassword?: string; name?: string }) => void delete: (params: { id: string; password: string }) => void export: (id: string) => void getActive: () => void