1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 15:33:21 +01:00
metamask-extension/ui/app/components/buy-button-subview.js

268 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-08-10 22:43:01 +02:00
const Component = require('react').Component
const PropTypes = require('prop-types')
2016-08-10 22:43:01 +02:00
const h = require('react-hyperscript')
const inherits = require('util').inherits
const connect = require('react-redux').connect
2016-08-10 22:43:01 +02:00
const actions = require('../actions')
const CoinbaseForm = require('./coinbase-form')
const ShapeshiftForm = require('./shapeshift-form')
const Loading = require('./loading')
const AccountPanel = require('./account-panel')
const RadioList = require('./custom-radio-list')
const networkNames = require('../../../app/scripts/config.js').networkNames
2016-08-10 22:43:01 +02:00
BuyButtonSubview.contextTypes = {
t: PropTypes.func,
}
2016-08-10 22:43:01 +02:00
module.exports = connect(mapStateToProps)(BuyButtonSubview)
2016-08-10 22:43:01 +02:00
function mapStateToProps (state) {
return {
identity: state.appState.identity,
account: state.metamask.accounts[state.appState.buyView.buyAddress],
2016-08-10 22:43:01 +02:00
warning: state.appState.warning,
buyView: state.appState.buyView,
2016-08-10 22:43:01 +02:00
network: state.metamask.network,
provider: state.metamask.provider,
context: state.appState.currentView.context,
isSubLoading: state.appState.isSubLoading,
2016-08-10 22:43:01 +02:00
}
}
inherits(BuyButtonSubview, Component)
function BuyButtonSubview () {
Component.call(this)
}
BuyButtonSubview.prototype.render = function () {
2017-08-09 09:36:18 +02:00
return (
h('div', {
style: {
width: '100%',
},
}, [
this.headerSubview(),
this.primarySubview(),
])
)
}
BuyButtonSubview.prototype.headerSubview = function () {
2016-08-10 22:43:01 +02:00
const props = this.props
const isLoading = props.isSubLoading
2016-08-10 22:43:01 +02:00
return (
2017-08-09 09:36:18 +02:00
h('.flex-column', {
style: {
alignItems: 'center',
},
}, [
2017-08-09 09:36:18 +02:00
// header bar (back button, label)
h('.flex-row', {
style: {
alignItems: 'center',
justifyContent: 'center',
},
}, [
h('i.fa.fa-arrow-left.fa-lg.cursor-pointer.color-orange', {
onClick: this.backButtonContext.bind(this),
style: {
position: 'absolute',
left: '10px',
},
}),
h('h2.text-transform-uppercase.flex-center', {
style: {
width: '100vw',
background: 'rgb(235, 235, 235)',
color: 'rgb(174, 174, 174)',
paddingTop: '4px',
paddingBottom: '4px',
},
}, this.context.t('depositEth')),
]),
2017-08-09 09:36:18 +02:00
// loading indication
h('div', {
style: {
position: 'absolute',
top: '57vh',
left: '49vw',
},
}, [
isLoading && h(Loading),
]),
2017-08-09 09:36:18 +02:00
// account panel
h('div', {
style: {
width: '80%',
},
}, [
h(AccountPanel, {
showFullAddress: true,
identity: props.identity,
account: props.account,
}),
]),
2017-08-09 09:36:18 +02:00
h('.flex-row', {
style: {
2017-08-09 09:36:18 +02:00
alignItems: 'center',
justifyContent: 'center',
},
2017-08-09 09:36:18 +02:00
}, [
h('h3.text-transform-uppercase.flex-center', {
style: {
paddingLeft: '15px',
width: '100vw',
background: 'rgb(235, 235, 235)',
color: 'rgb(174, 174, 174)',
paddingTop: '4px',
paddingBottom: '4px',
},
}, this.context.t('selectService')),
2017-08-09 09:36:18 +02:00
]),
])
)
}
BuyButtonSubview.prototype.primarySubview = function () {
const props = this.props
const network = props.network
switch (network) {
case 'loading':
return
2017-08-09 09:36:18 +02:00
case '1':
return this.mainnetSubview()
// Ropsten, Rinkeby, Kovan
2017-08-09 09:36:18 +02:00
case '3':
case '4':
case '42':
const networkName = networkNames[network]
const label = `${networkName} ${this.context.t('testFaucet')}`
return (
h('div.flex-column', {
2017-08-09 09:36:18 +02:00
style: {
alignItems: 'center',
margin: '20px 50px',
2017-08-09 09:36:18 +02:00
},
}, [
2017-08-09 09:36:18 +02:00
h('button.text-transform-uppercase', {
onClick: () => this.props.dispatch(actions.buyEth({ network })),
2017-08-09 09:36:18 +02:00
style: {
marginTop: '15px',
},
}, label),
// Kovan only: Dharma loans beta
network === '42' ? (
h('button.text-transform-uppercase', {
onClick: () => this.navigateTo('https://borrow.dharma.io/'),
style: {
marginTop: '15px',
},
}, this.context.t('borrowDharma'))
) : null,
])
)
2017-08-09 09:36:18 +02:00
default:
return (
h('h2.error', this.context.t('unknownNetworkId'))
2017-08-09 09:36:18 +02:00
)
}
}
BuyButtonSubview.prototype.mainnetSubview = function () {
const props = this.props
return (
h('.flex-column', {
style: {
alignItems: 'center',
},
}, [
h('.flex-row.selected-exchange', {
style: {
position: 'relative',
right: '35px',
marginTop: '20px',
marginBottom: '20px',
},
}, [
h(RadioList, {
defaultFocus: props.buyView.subview,
2017-04-24 20:56:31 +02:00
labels: [
'Coinbase',
'ShapeShift',
],
subtext: {
'Coinbase': `${this.context.t('crypto')}/${this.context.t('fiat')} (${this.context.t('usaOnly')})`,
'ShapeShift': this.context.t('crypto'),
},
onClick: this.radioHandler.bind(this),
}),
]),
2017-08-09 09:36:18 +02:00
h('h3.text-transform-uppercase', {
style: {
paddingLeft: '15px',
fontFamily: 'Montserrat Light',
width: '100vw',
background: 'rgb(235, 235, 235)',
color: 'rgb(174, 174, 174)',
paddingTop: '4px',
paddingBottom: '4px',
2016-08-10 22:43:01 +02:00
},
}, props.buyView.subview),
2017-08-09 09:36:18 +02:00
2016-08-10 22:43:01 +02:00
this.formVersionSubview(),
])
2017-08-09 09:36:18 +02:00
2016-08-10 22:43:01 +02:00
)
}
BuyButtonSubview.prototype.formVersionSubview = function () {
const network = this.props.network
if (network === '1') {
if (this.props.buyView.formView.coinbase) {
2016-08-10 22:43:01 +02:00
return h(CoinbaseForm, this.props)
} else if (this.props.buyView.formView.shapeshift) {
2016-08-10 22:43:01 +02:00
return h(ShapeshiftForm, this.props)
}
}
}
BuyButtonSubview.prototype.navigateTo = function (url) {
global.platform.openWindow({ url })
}
BuyButtonSubview.prototype.backButtonContext = function () {
if (this.props.context === 'confTx') {
this.props.dispatch(actions.showConfTxPage({transForward: false}))
} else {
this.props.dispatch(actions.goHome())
}
}
BuyButtonSubview.prototype.radioHandler = function (event) {
switch (event.target.title) {
case 'Coinbase':
return this.props.dispatch(actions.coinBaseSubview())
case 'ShapeShift':
return this.props.dispatch(actions.shapeShiftSubview(this.props.provider.type))
}
}