1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 11:01:41 +01:00
metamask-extension/ui/app/components/tx-list.js

138 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-08-09 12:10:51 +02:00
const Component = require('react').Component
const connect = require('react-redux').connect
const h = require('react-hyperscript')
const inherits = require('util').inherits
const prefixForNetwork = require('../../lib/etherscan-prefix-for-network')
2017-08-11 04:35:10 +02:00
const selectors = require('../selectors')
2017-08-30 12:33:00 +02:00
const TxListItem = require('./tx-list-item')
const ShiftListItem = require('./shift-list-item')
2017-11-02 03:30:33 +01:00
const { formatDate } = require('../util')
const { showConfTxPage } = require('../actions')
const classnames = require('classnames')
const { tokenInfoGetter } = require('../token-util')
2017-08-09 12:10:51 +02:00
module.exports = connect(mapStateToProps, mapDispatchToProps)(TxList)
2017-08-09 12:10:51 +02:00
function mapStateToProps (state) {
2017-08-09 12:10:51 +02:00
return {
2017-08-11 04:35:10 +02:00
txsToRender: selectors.transactionsSelector(state),
conversionRate: selectors.conversionRateSelector(state),
2017-08-09 12:10:51 +02:00
}
}
function mapDispatchToProps (dispatch) {
return {
2017-11-02 13:15:59 +01:00
showConfTxPage: ({ id }) => dispatch(showConfTxPage({ id })),
}
}
2017-08-09 12:10:51 +02:00
inherits(TxList, Component)
function TxList () {
Component.call(this)
}
TxList.prototype.componentWillMount = function () {
this.tokenInfoGetter = tokenInfoGetter()
}
2017-08-09 12:10:51 +02:00
TxList.prototype.render = function () {
return h('div.flex-column', [
2017-09-07 20:38:21 +02:00
h('div.flex-row.tx-list-header-wrapper', [
h('div.flex-row.tx-list-header', [
h('div', 'transactions'),
2017-08-09 12:10:51 +02:00
]),
]),
h('div.flex-column.tx-list-container', {}, [
this.renderTransaction(),
]),
2017-08-09 12:10:51 +02:00
])
}
TxList.prototype.renderTransaction = function () {
const { txsToRender, conversionRate } = this.props
return txsToRender.length
? txsToRender.map((transaction, i) => this.renderTransactionListItem(transaction, conversionRate, i))
: [h(
'div.tx-list-item.tx-list-item--empty',
{ key: 'tx-list-none' },
[ 'No Transactions' ],
)]
}
// TODO: Consider moving TxListItem into a separate component
TxList.prototype.renderTransactionListItem = function (transaction, conversionRate, index) {
// console.log({transaction})
// refer to transaction-list.js:line 58
2017-09-29 17:40:57 +02:00
if (transaction.key === 'shapeshift') {
return h(ShiftListItem, { ...transaction, key: `shapeshift${index}` })
}
2017-08-11 04:35:10 +02:00
const props = {
dateString: formatDate(transaction.time),
address: transaction.txParams.to,
transactionStatus: transaction.status,
transactionAmount: transaction.txParams.value,
transActionId: transaction.id,
transactionHash: transaction.hash,
transactionNetworkId: transaction.metamaskNetworkId,
2017-08-11 04:35:10 +02:00
}
2017-09-05 10:48:52 +02:00
const {
2017-09-06 09:33:39 +02:00
address,
2017-09-05 10:48:52 +02:00
transactionStatus,
transactionAmount,
dateString,
transActionId,
transactionHash,
transactionNetworkId,
2017-09-05 10:48:52 +02:00
} = props
const { showConfTxPage } = this.props
2017-09-05 10:48:52 +02:00
2017-08-30 12:33:00 +02:00
const opts = {
2017-09-22 03:44:52 +02:00
key: transActionId || transactionHash,
2017-09-14 10:09:57 +02:00
txParams: transaction.txParams,
2017-08-30 12:33:00 +02:00
transactionStatus,
transActionId,
dateString,
address,
transactionAmount,
transactionHash,
conversionRate,
tokenInfoGetter: this.tokenInfoGetter,
2017-08-30 12:33:00 +02:00
}
2017-08-09 12:10:51 +02:00
2017-11-02 13:15:59 +01:00
const isUnapproved = transactionStatus === 'unapproved'
if (isUnapproved) {
2017-08-30 12:33:00 +02:00
opts.onClick = () => showConfTxPage({id: transActionId})
2017-09-07 20:38:21 +02:00
opts.transactionStatus = 'Not Started'
} else if (transactionHash) {
opts.onClick = () => this.view(transactionHash, transactionNetworkId)
2017-08-30 12:33:00 +02:00
}
opts.className = classnames('.tx-list-item', {
'.tx-list-pending-item-container': isUnapproved,
'.tx-list-clickable': Boolean(transactionHash) || isUnapproved,
})
2017-08-30 12:33:00 +02:00
return h(TxListItem, opts)
2017-08-09 12:10:51 +02:00
}
TxList.prototype.view = function (txHash, network) {
const url = etherscanLinkFor(txHash, network)
if (url) {
navigateTo(url)
}
}
function navigateTo (url) {
global.platform.openWindow({ url })
}
function etherscanLinkFor (txHash, network) {
const prefix = prefixForNetwork(network)
return `https://${prefix}etherscan.io/tx/${txHash}`
}