aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/pages/create-account/connect-hardware/index.js
diff options
context:
space:
mode:
authorbrunobar79 <brunobar79@gmail.com>2018-07-06 05:45:28 +0800
committerbrunobar79 <brunobar79@gmail.com>2018-07-06 05:45:28 +0800
commit6c2730f24300449010bd3552d4d746bcb5dd176a (patch)
tree674ad6bdf972a613207cdcfbe0b871224a4db7ef /ui/app/components/pages/create-account/connect-hardware/index.js
parentba5cde0995f956fb22825d604fe7d664677abaaa (diff)
downloadtangerine-wallet-browser-6c2730f24300449010bd3552d4d746bcb5dd176a.tar
tangerine-wallet-browser-6c2730f24300449010bd3552d4d746bcb5dd176a.tar.gz
tangerine-wallet-browser-6c2730f24300449010bd3552d4d746bcb5dd176a.tar.bz2
tangerine-wallet-browser-6c2730f24300449010bd3552d4d746bcb5dd176a.tar.lz
tangerine-wallet-browser-6c2730f24300449010bd3552d4d746bcb5dd176a.tar.xz
tangerine-wallet-browser-6c2730f24300449010bd3552d4d746bcb5dd176a.tar.zst
tangerine-wallet-browser-6c2730f24300449010bd3552d4d746bcb5dd176a.zip
Refactor UI
Diffstat (limited to 'ui/app/components/pages/create-account/connect-hardware/index.js')
-rw-r--r--ui/app/components/pages/create-account/connect-hardware/index.js131
1 files changed, 131 insertions, 0 deletions
diff --git a/ui/app/components/pages/create-account/connect-hardware/index.js b/ui/app/components/pages/create-account/connect-hardware/index.js
new file mode 100644
index 000000000..04e69162f
--- /dev/null
+++ b/ui/app/components/pages/create-account/connect-hardware/index.js
@@ -0,0 +1,131 @@
+const { Component } = require('react')
+const PropTypes = require('prop-types')
+const h = require('react-hyperscript')
+const connect = require('react-redux').connect
+const actions = require('../../../actions')
+const ConnectScreen = require('./connect-screen')
+const AccountList = require('./account-list')
+
+class ConnectHardwareForm extends Component {
+ constructor (props, context) {
+ super(props)
+ this.state = {
+ error: null,
+ response: null,
+ btnText: context.t('connectToTrezor'),
+ selectedAccount: '',
+ accounts: [],
+ }
+ }
+
+ connectToTrezor = () => {
+ if (this.state.accounts.length) {
+ return null
+ }
+ this.setState({ btnText: this.context.t('connecting')})
+ this.getPage(1)
+ }
+
+ onAccountChange = (account) => {
+ this.setState({selectedAccount: account, error: null})
+ }
+
+ getPage = (page) => {
+ this.props
+ .connectHardware('trezor', page)
+ .then(accounts => {
+ if (accounts.length) {
+ this.setState({ accounts: accounts })
+ }
+ })
+ .catch(e => {
+ this.setState({ btnText: this.context.t('connectToTrezor') })
+ })
+ }
+
+ unlockAccount () {
+ if (this.state.selectedAccount === '') {
+ return Promise.reject({ error: this.context.t('accountSelectionRequired') })
+ }
+ return this.props.unlockTrezorAccount(this.state.selectedAccount)
+ }
+
+
+ renderError () {
+ return this.state.error
+ ? h('span.error', { style: { marginBottom: 40 } }, this.state.error)
+ : null
+ }
+
+ renderContent () {
+ if (!this.state.accounts.length) {
+ return h(ConnectScreen, {
+ connectToTrezor: this.connectToTrezor,
+ btnText: this.state.btnText,
+ })
+ }
+
+ return h(AccountList, {
+ accounts: this.state.accounts,
+ onAccountChange: this.onAccountChange,
+ network: this.props.network,
+ getPage: this.getPage,
+ history: this.props.history,
+ })
+ }
+
+ render () {
+ return h('div.new-account-create-form', [
+ this.renderError(),
+ this.renderContent(),
+ ])
+ }
+}
+
+ConnectHardwareForm.propTypes = {
+ hideModal: PropTypes.func,
+ showImportPage: PropTypes.func,
+ showConnectPage: PropTypes.func,
+ connectHardware: PropTypes.func,
+ unlockTrezorAccount: PropTypes.func,
+ numberOfExistingAccounts: PropTypes.number,
+ history: PropTypes.object,
+ t: PropTypes.func,
+ network: PropTypes.string,
+ accounts: PropTypes.object,
+}
+
+const mapStateToProps = state => {
+ const {
+ metamask: { network, selectedAddress, identities = {}, accounts = [] },
+ } = state
+ const numberOfExistingAccounts = Object.keys(identities).length
+
+ return {
+ network,
+ accounts,
+ address: selectedAddress,
+ numberOfExistingAccounts,
+ }
+}
+
+const mapDispatchToProps = dispatch => {
+ return {
+ connectHardware: (deviceName, page) => {
+ return dispatch(actions.connectHardware(deviceName, page))
+ },
+ unlockTrezorAccount: index => {
+ return dispatch(actions.unlockTrezorAccount(index))
+ },
+ showImportPage: () => dispatch(actions.showImportPage()),
+ showConnectPage: () => dispatch(actions.showConnectPage()),
+ }
+}
+
+ConnectHardwareForm.contextTypes = {
+ t: PropTypes.func,
+}
+
+module.exports = connect(mapStateToProps, mapDispatchToProps)(
+ ConnectHardwareForm
+)