2018-01-10 19:25:38 +01:00
|
|
|
const { Component } = require('react')
|
|
|
|
const PropTypes = require('prop-types')
|
|
|
|
const h = require('react-hyperscript')
|
2018-03-29 17:00:44 +02:00
|
|
|
const connect = require('react-redux').connect
|
2019-03-22 00:03:30 +01:00
|
|
|
const actions = require('../../store/actions')
|
|
|
|
const { DEFAULT_ROUTE } = require('../../helpers/constants/routes')
|
|
|
|
import Button from '../../components/ui/button'
|
2018-01-10 19:25:38 +01:00
|
|
|
|
|
|
|
class NewAccountCreateForm extends Component {
|
2018-03-29 17:00:44 +02:00
|
|
|
constructor (props, context) {
|
2018-01-10 19:25:38 +01:00
|
|
|
super(props)
|
2018-01-31 22:40:14 +01:00
|
|
|
|
2018-01-10 19:25:38 +01:00
|
|
|
const { numberOfExistingAccounts = 0 } = props
|
|
|
|
const newAccountNumber = numberOfExistingAccounts + 1
|
|
|
|
|
|
|
|
this.state = {
|
2018-01-31 22:40:14 +01:00
|
|
|
newAccountName: '',
|
2018-03-29 17:00:44 +02:00
|
|
|
defaultAccountName: context.t('newAccountNumberName', [newAccountNumber]),
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-01-31 22:40:14 +01:00
|
|
|
const { newAccountName, defaultAccountName } = this.state
|
2018-02-01 03:08:49 +01:00
|
|
|
const { history, createAccount } = this.props
|
2018-01-10 19:25:38 +01:00
|
|
|
|
|
|
|
return h('div.new-account-create-form', [
|
|
|
|
|
|
|
|
h('div.new-account-create-form__input-label', {}, [
|
2018-03-29 17:00:44 +02:00
|
|
|
this.context.t('accountName'),
|
2018-01-10 19:25:38 +01:00
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.new-account-create-form__input-wrapper', {}, [
|
|
|
|
h('input.new-account-create-form__input', {
|
2018-01-31 22:40:14 +01:00
|
|
|
value: newAccountName,
|
|
|
|
placeholder: defaultAccountName,
|
2018-01-10 19:25:38 +01:00
|
|
|
onChange: event => this.setState({ newAccountName: event.target.value }),
|
|
|
|
}, []),
|
|
|
|
]),
|
|
|
|
|
|
|
|
h('div.new-account-create-form__buttons', {}, [
|
|
|
|
|
2018-08-28 05:58:40 +02:00
|
|
|
h(Button, {
|
|
|
|
type: 'default',
|
|
|
|
large: true,
|
|
|
|
className: 'new-account-create-form__button',
|
2018-02-01 03:08:49 +01:00
|
|
|
onClick: () => history.push(DEFAULT_ROUTE),
|
2018-08-28 05:58:40 +02:00
|
|
|
}, [this.context.t('cancel')]),
|
2018-01-10 19:25:38 +01:00
|
|
|
|
2018-08-28 05:58:40 +02:00
|
|
|
h(Button, {
|
2019-04-16 21:35:22 +02:00
|
|
|
type: 'secondary',
|
2018-08-28 05:58:40 +02:00
|
|
|
large: true,
|
2018-10-10 07:12:43 +02:00
|
|
|
className: 'new-account-create-form__button',
|
2018-02-01 03:08:49 +01:00
|
|
|
onClick: () => {
|
2018-02-01 03:24:20 +01:00
|
|
|
createAccount(newAccountName || defaultAccountName)
|
2019-03-05 16:45:01 +01:00
|
|
|
.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,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2018-02-01 03:08:49 +01:00
|
|
|
},
|
2018-08-28 05:58:40 +02:00
|
|
|
}, [this.context.t('create')]),
|
2018-01-10 19:25:38 +01:00
|
|
|
|
|
|
|
]),
|
|
|
|
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NewAccountCreateForm.propTypes = {
|
|
|
|
hideModal: PropTypes.func,
|
|
|
|
showImportPage: PropTypes.func,
|
2018-06-10 09:52:32 +02:00
|
|
|
showConnectPage: PropTypes.func,
|
2018-01-10 19:25:38 +01:00
|
|
|
createAccount: PropTypes.func,
|
|
|
|
numberOfExistingAccounts: PropTypes.number,
|
2018-02-01 03:08:49 +01:00
|
|
|
history: PropTypes.object,
|
2018-03-22 16:06:13 +01:00
|
|
|
t: PropTypes.func,
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => {
|
|
|
|
const { metamask: { network, selectedAddress, identities = {} } } = state
|
|
|
|
const numberOfExistingAccounts = Object.keys(identities).length
|
|
|
|
|
|
|
|
return {
|
|
|
|
network,
|
|
|
|
address: selectedAddress,
|
|
|
|
numberOfExistingAccounts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
return {
|
2018-02-01 03:08:49 +01:00
|
|
|
toCoinbase: address => dispatch(actions.buyEth({ network: '1', address, amount: 0 })),
|
|
|
|
hideModal: () => dispatch(actions.hideModal()),
|
|
|
|
createAccount: newAccountName => {
|
|
|
|
return dispatch(actions.addNewAccount())
|
|
|
|
.then(newAccountAddress => {
|
2018-01-10 19:25:38 +01:00
|
|
|
if (newAccountName) {
|
2018-04-19 05:33:51 +02:00
|
|
|
dispatch(actions.setAccountLabel(newAccountAddress, newAccountName))
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
showImportPage: () => dispatch(actions.showImportPage()),
|
2018-06-10 09:52:32 +02:00
|
|
|
showConnectPage: () => dispatch(actions.showConnectPage()),
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
NewAccountCreateForm.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2019-03-05 16:45:01 +01:00
|
|
|
metricsEvent: PropTypes.func,
|
2018-03-29 17:00:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-10 19:25:38 +01:00
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(NewAccountCreateForm)
|
2018-03-29 17:00:44 +02:00
|
|
|
|