import React from 'react' import PropTypes from 'prop-types' import { Box, Flex } from 'rebass' import { FormattedMessage, FormattedRelative, injectIntl } from 'react-intl' import { decodePayReq } from 'lib/utils/crypto' import { showNotification } from 'lib/utils/notifications' import copy from 'copy-to-clipboard' import { Bar, DataRow, Button, Dropdown, QRCode, Text, Truncate, Value } from 'components/UI' import messages from './messages' class RequestSummary extends React.Component { state = { isExpired: null, timer: null } static propTypes = { /** Currently selected cryptocurrency (key). */ cryptoCurrency: PropTypes.string.isRequired, /** List of supported cryptocurrencies. */ cryptoCurrencies: PropTypes.arrayOf( PropTypes.shape({ key: PropTypes.string.isRequired, name: PropTypes.string.isRequired }) ).isRequired, /** Boolean indicating wether the invoice has already been paid. */ isPaid: PropTypes.bool, /** Lightning Payment request. */ payReq: PropTypes.string.isRequired, /** Set the current cryptocurrency. */ setCryptoCurrency: PropTypes.func.isRequired } static defaultProps = { isPaid: false } componentDidMount() { const { payReq } = this.props let invoice try { invoice = decodePayReq(payReq) const expiresIn = invoice.timeExpireDate * 1000 - Date.now() if (expiresIn >= 0) { this.setState({ isExpired: false }) const timer = setInterval(() => this.setState({ isExpired: true }), expiresIn) this.setState({ timer }) } else { this.setState({ isExpired: true }) } } catch (e) { return null } } componentWillUnmount() { const { timer } = this.state clearInterval(timer) } copyPaymentRequest = () => { const { intl, payReq } = this.props copy(payReq) showNotification( intl.formatMessage({ ...messages.address_notification_title }), intl.formatMessage({ ...messages.copied_notification_description }) ) } render() { const { cryptoCurrency, cryptoCurrencies, isPaid, payReq, setCryptoCurrency, ...rest } = this.props const { isExpired } = this.state let invoice try { invoice = decodePayReq(payReq) } catch (e) { return null } const { satoshis } = invoice const descriptionTag = invoice.tags.find(tag => tag.tagName === 'description') || {} const memo = descriptionTag.data return ( {memo && ( } right={memo} /> {' '} )} } right={ } /> } right={ } /> } right= /> } right={ {isExpired ? 'Expired ' : 'Expires '} {isPaid ? ( ) : ( )} } /> ) } } export default injectIntl(RequestSummary)