|
| 1 | +// @flow |
| 2 | +import React, { useState } from 'react'; |
| 3 | +import Form from 'react-bootstrap/Form'; |
| 4 | +import Button from 'react-bootstrap/Button'; |
| 5 | +import Container from 'react-bootstrap/Container'; |
| 6 | +import Row from 'react-bootstrap/Row'; |
| 7 | +import typeof { setAccountFromMnemonic as SetAccountFromMnemonic } from '../actions/account'; |
| 8 | +import { isValidMnemonic } from '../utils/mnemonic'; |
| 9 | + |
| 10 | +type Props = { |
| 11 | + setAccountFromMnemonic: SetAccountFromMnemonic |
| 12 | +}; |
| 13 | + |
| 14 | +// FIXME: this has no error handling, neither while parsing the address |
| 15 | +// nor when fetching the balance. |
| 16 | +export default ({ setAccountFromMnemonic }: Props) => { |
| 17 | + const handleSubmitCreateSpending = function handleSubmitCreateSpending( |
| 18 | + event |
| 19 | + ) { |
| 20 | + event.preventDefault(); |
| 21 | + if (isValidMnemonic(newMnemonicPhrase)) { |
| 22 | + return Promise.all([ |
| 23 | + setAccountFromMnemonic(newMnemonicPhrase, newMnemonicPassword) |
| 24 | + ]); |
| 25 | + } |
| 26 | + setIsMnemonicValid(false); |
| 27 | + }; |
| 28 | + |
| 29 | + const [isMnemonicValid, setIsMnemonicValid] = useState(true); |
| 30 | + |
| 31 | + const [newMnemonicPhrase, setNewMnemonicPhrase] = useState(''); |
| 32 | + |
| 33 | + const [newMnemonicPassword, setNewMnemonicPassword] = useState(''); |
| 34 | + |
| 35 | + return ( |
| 36 | + <Container> |
| 37 | + <Form onSubmit={handleSubmitCreateSpending} className="mt-5"> |
| 38 | + <Form.Group> |
| 39 | + <Form.Label>Create your new wallet password:</Form.Label> |
| 40 | + <Form.Control |
| 41 | + required |
| 42 | + type="password" |
| 43 | + id="mnemonicPhrase" |
| 44 | + name="mnemonicPhrase" |
| 45 | + placeholder="New password (min 8 chars)" |
| 46 | + value={newMnemonicPhrase} |
| 47 | + isInvalid={!isMnemonicValid} |
| 48 | + onChange={event => setNewMnemonicPhrase(event.target.value)} |
| 49 | + /> |
| 50 | + <Form.Label className="text-danger" hidden={isMnemonicValid}> |
| 51 | + <code>You should not share your password with others.</code> |
| 52 | + </Form.Label> |
| 53 | + <Form.Control |
| 54 | + type="password" |
| 55 | + name="mnemonicPassword" |
| 56 | + placeholder="Confirm password" |
| 57 | + value={newMnemonicPassword} |
| 58 | + onChange={event => setNewMnemonicPassword(event.target.value)} |
| 59 | + className="mt-3" |
| 60 | + /> |
| 61 | + </Form.Group> |
| 62 | + <Row className="justify-content-center"> |
| 63 | + <Button variant="primary" type="submit"> |
| 64 | + Create |
| 65 | + </Button> |
| 66 | + </Row> |
| 67 | + </Form> |
| 68 | + </Container> |
| 69 | + ); |
| 70 | +}; |
0 commit comments