import React, { PureComponent } from 'react' import PropTypes from 'prop-types' import classnames from 'classnames' import { getActivities } from './transaction-activity-log.util' import Card from '../card' import { getEthConversionFromWeiHex, getValueFromWeiHex } from '../../helpers/conversions.util' import { ETH } from '../../constants/common' import { formatDate } from '../../util' export default class TransactionActivityLog extends PureComponent { static contextTypes = { t: PropTypes.func, } static propTypes = { transaction: PropTypes.object, className: PropTypes.string, conversionRate: PropTypes.number, } state = { activities: [], } componentDidMount () { this.setActivites() } componentDidUpdate (prevProps) { const { transaction: { history: prevHistory = [] } = {} } = prevProps const { transaction: { history = [] } = {} } = this.props if (prevHistory.length !== history.length) { this.setActivites() } } setActivites () { const activities = getActivities(this.props.transaction) this.setState({ activities }) } renderActivity (activity, index) { const { conversionRate } = this.props const { eventKey, value, timestamp } = activity const ethValue = index === 0 ? `${getValueFromWeiHex({ value, toCurrency: ETH, conversionRate, numberOfDecimals: 6, })} ${ETH}` : getEthConversionFromWeiHex({ value, toCurrency: ETH, conversionRate }) const formattedTimestamp = formatDate(timestamp) const activityText = this.context.t(eventKey, [ethValue, formattedTimestamp]) return (
{ activityText }
) } render () { const { t } = this.context const { className } = this.props const { activities } = this.state return (
{ activities.map((activity, index) => this.renderActivity(activity, index)) }
) } }