Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 5 additions & 32 deletions packages/neuron-ui/src/components/WalletEditor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,22 +16,10 @@ export const useWalletEditor = () => {
value: name,
onChange: (e: React.FormEvent<Pick<any, string>>) => setName(e.currentTarget.value),
},
password: {
value: password,
onChange: (e: React.FormEvent<Pick<any, string>>) => setPassword(e.currentTarget.value),
},
newPassword: {
value: newPassword,
onChange: (e: React.FormEvent<Pick<any, string>>) => setNewPassword(e.currentTarget.value),
},
confirmNewPassword: {
value: confirmNewPassword,
onChange: (e: React.FormEvent<Pick<any, string>>) => setConfirmNewPassword(e.currentTarget.value),
},
}
}

export const useInputs = ({ name, newPassword, confirmNewPassword }: ReturnType<typeof useWalletEditor>) => {
export const useInputs = ({ name }: ReturnType<typeof useWalletEditor>) => {
return useMemo(
() => [
{
Expand All @@ -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<any>) =>
Expand Down
65 changes: 10 additions & 55 deletions packages/neuron-ui/src/components/WalletEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -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 },
Expand All @@ -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 (
<Card>
Expand All @@ -74,37 +56,10 @@ export default ({
<InlineInput {...inputProps} key={inputProps.label} />
))}
</Form>
<Button type="submit" variant="primary" size="lg" block onClick={handleSubmit} disabled={!areParamsValid}>
<Button type="submit" variant="primary" size="lg" block onClick={handleConfirm} disabled={!areParamsValid}>
{t('common.save')}
</Button>
</Card.Body>
<Dialog open={dialog.open} onClick={onCancel}>
<Card
onClick={(e: React.SyntheticEvent<HTMLDivElement>) => {
e.preventDefault()
e.stopPropagation()
}}
>
<>
<Card.Header>{t('settings.wallet-manager.delete-wallet-title', { name: wallet.name })}</Card.Header>
<Card.Body>
<Form.Group as={Row} controlId="formPlaintextPassword">
<Col>
<Form.Control type="password" placeholder="password" onChange={editor.password.onChange} />
</Col>
</Form.Group>
</Card.Body>
<Card.Footer className="text-muted">
<Button variant="danger" onClick={handleConfirm} disabled={editor.password.value === ''}>
{t('common.confirm')}
</Button>
<Button variant="light" onClick={onCancel}>
{t('common.cancel')}
</Button>
</Card.Footer>
</>
</Card>
</Dialog>
</Card>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/services/UILayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 19 additions & 11 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down