-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
286 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { useEffect } from 'react'; | ||
import { Form, useForm } from 'react-hook-form'; | ||
|
||
import { | ||
Button, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
Text, | ||
} from '@chakra-ui/react'; | ||
|
||
import usePassword from '../store/usePassword'; | ||
import useWallet from '../store/useWallet'; | ||
|
||
import FormInput from './FormInput'; | ||
|
||
type RenameKeyModalProps = { | ||
keyIndex: number; | ||
isOpen: boolean; | ||
onClose: () => void; | ||
}; | ||
|
||
type FormValues = { | ||
displayName: string; | ||
}; | ||
|
||
function RenameKeyModal({ | ||
keyIndex, | ||
isOpen, | ||
onClose, | ||
}: RenameKeyModalProps): JSX.Element { | ||
const { renameKey, wallet } = useWallet(); | ||
const { withPassword } = usePassword(); | ||
const { | ||
register, | ||
reset, | ||
control, | ||
handleSubmit, | ||
setValue, | ||
formState: { errors, isSubmitted }, | ||
} = useForm<FormValues>(); | ||
|
||
const close = () => { | ||
reset(); | ||
onClose(); | ||
}; | ||
|
||
const key = wallet?.keychain[keyIndex]; | ||
if (!key) { | ||
throw new Error(`Key with index ${keyIndex} not found`); | ||
} | ||
|
||
const submit = handleSubmit(async ({ displayName }) => { | ||
const success = await withPassword( | ||
async (password) => { | ||
await renameKey(keyIndex, displayName, password); | ||
return true; | ||
}, | ||
'Rename', | ||
// eslint-disable-next-line max-len | ||
`Please enter the password to change the name of key "${key.displayName}" (${key.path}) to "${displayName}":` | ||
); | ||
if (success) { | ||
close(); | ||
} | ||
}); | ||
|
||
useEffect(() => { | ||
setValue('displayName', key.displayName); | ||
}, [key, setValue]); | ||
|
||
return ( | ||
<Modal isOpen={isOpen} onClose={close} isCentered size="lg"> | ||
<Form control={control}> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalCloseButton /> | ||
<ModalHeader textAlign="center"> | ||
Change the name of Key Pair | ||
</ModalHeader> | ||
<ModalBody> | ||
<Text mb={4}> | ||
Please specify the new name for the key pair with the path:{' '} | ||
{key.path} | ||
</Text> | ||
<FormInput | ||
label="Name" | ||
register={register('displayName', { | ||
required: 'Name is required', | ||
minLength: { | ||
value: 2, | ||
message: 'Give your account a meaningful name', | ||
}, | ||
})} | ||
errors={errors} | ||
isSubmitted={isSubmitted} | ||
/> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button | ||
colorScheme="blue" | ||
onClick={submit} | ||
ml={2} | ||
variant="whiteModal" | ||
px={10} | ||
> | ||
Rename | ||
</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Form> | ||
</Modal> | ||
); | ||
} | ||
|
||
export default RenameKeyModal; |
Oops, something went wrong.