You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.8 KiB
131 lines
3.8 KiB
7 years ago
|
const { Component } = require('react')
|
||
|
const PropTypes = require('prop-types')
|
||
|
const h = require('react-hyperscript')
|
||
7 years ago
|
const connect = require('react-redux').connect
|
||
6 years ago
|
const actions = require('../../store/actions')
|
||
|
const { DEFAULT_ROUTE } = require('../../helpers/constants/routes')
|
||
|
import Button from '../../components/ui/button'
|
||
7 years ago
|
|
||
|
class NewAccountCreateForm extends Component {
|
||
7 years ago
|
constructor (props, context) {
|
||
7 years ago
|
super(props)
|
||
7 years ago
|
|
||
7 years ago
|
const { numberOfExistingAccounts = 0 } = props
|
||
|
const newAccountNumber = numberOfExistingAccounts + 1
|
||
|
|
||
|
this.state = {
|
||
7 years ago
|
newAccountName: '',
|
||
7 years ago
|
defaultAccountName: context.t('newAccountNumberName', [newAccountNumber]),
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
render () {
|
||
7 years ago
|
const { newAccountName, defaultAccountName } = this.state
|
||
7 years ago
|
const { history, createAccount } = this.props
|
||
7 years ago
|
|
||
|
return h('div.new-account-create-form', [
|
||
|
|
||
|
h('div.new-account-create-form__input-label', {}, [
|
||
7 years ago
|
this.context.t('accountName'),
|
||
7 years ago
|
]),
|
||
|
|
||
|
h('div.new-account-create-form__input-wrapper', {}, [
|
||
|
h('input.new-account-create-form__input', {
|
||
7 years ago
|
value: newAccountName,
|
||
|
placeholder: defaultAccountName,
|
||
7 years ago
|
onChange: event => this.setState({ newAccountName: event.target.value }),
|
||
|
}, []),
|
||
|
]),
|
||
|
|
||
|
h('div.new-account-create-form__buttons', {}, [
|
||
|
|
||
6 years ago
|
h(Button, {
|
||
|
type: 'default',
|
||
|
large: true,
|
||
|
className: 'new-account-create-form__button',
|
||
7 years ago
|
onClick: () => history.push(DEFAULT_ROUTE),
|
||
6 years ago
|
}, [this.context.t('cancel')]),
|
||
7 years ago
|
|
||
6 years ago
|
h(Button, {
|
||
|
type: 'primary',
|
||
|
large: true,
|
||
6 years ago
|
className: 'new-account-create-form__button',
|
||
7 years ago
|
onClick: () => {
|
||
7 years ago
|
createAccount(newAccountName || defaultAccountName)
|
||
6 years ago
|
.then(() => {
|
||
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Accounts',
|
||
|
action: 'Add New Account',
|
||
|
name: 'Added New Account',
|
||
|
},
|
||
|
})
|
||
|
history.push(DEFAULT_ROUTE)
|
||
|
})
|
||
|
.catch((e) => {
|
||
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Accounts',
|
||
|
action: 'Add New Account',
|
||
|
name: 'Error',
|
||
|
},
|
||
|
customVariables: {
|
||
|
errorMessage: e.message,
|
||
|
},
|
||
|
})
|
||
|
})
|
||
7 years ago
|
},
|
||
6 years ago
|
}, [this.context.t('create')]),
|
||
7 years ago
|
|
||
|
]),
|
||
|
|
||
|
])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
NewAccountCreateForm.propTypes = {
|
||
|
hideModal: PropTypes.func,
|
||
|
showImportPage: PropTypes.func,
|
||
7 years ago
|
showConnectPage: PropTypes.func,
|
||
7 years ago
|
createAccount: PropTypes.func,
|
||
|
numberOfExistingAccounts: PropTypes.number,
|
||
7 years ago
|
history: PropTypes.object,
|
||
7 years ago
|
t: PropTypes.func,
|
||
7 years ago
|
}
|
||
|
|
||
|
const mapStateToProps = state => {
|
||
|
const { metamask: { network, selectedAddress, identities = {} } } = state
|
||
|
const numberOfExistingAccounts = Object.keys(identities).length
|
||
|
|
||
|
return {
|
||
|
network,
|
||
|
address: selectedAddress,
|
||
|
numberOfExistingAccounts,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const mapDispatchToProps = dispatch => {
|
||
|
return {
|
||
7 years ago
|
toCoinbase: address => dispatch(actions.buyEth({ network: '1', address, amount: 0 })),
|
||
|
hideModal: () => dispatch(actions.hideModal()),
|
||
|
createAccount: newAccountName => {
|
||
|
return dispatch(actions.addNewAccount())
|
||
|
.then(newAccountAddress => {
|
||
7 years ago
|
if (newAccountName) {
|
||
7 years ago
|
dispatch(actions.setAccountLabel(newAccountAddress, newAccountName))
|
||
7 years ago
|
}
|
||
|
})
|
||
|
},
|
||
|
showImportPage: () => dispatch(actions.showImportPage()),
|
||
7 years ago
|
showConnectPage: () => dispatch(actions.showConnectPage()),
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
NewAccountCreateForm.contextTypes = {
|
||
|
t: PropTypes.func,
|
||
6 years ago
|
metricsEvent: PropTypes.func,
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(NewAccountCreateForm)
|
||
7 years ago
|
|