import React, { Component } from 'react'; import { Animated, StyleSheet, View, TouchableOpacity, Clipboard, Share } from 'react-native'; // import { QRCode } from 'react-native-custom-qr-codes'; import { BlueLoading, BlueText, SafeBlueArea, BlueButton, BlueNavigationStyle } from '../../BlueComponents'; import PropTypes from 'prop-types'; import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; /** @type {AppStorage} */ let BlueApp = require('../../BlueApp'); const loc = require('../../loc'); const EV = require('../../events'); const QRFast = require('react-native-qrcode'); export default class LNDViewInvoice extends Component { static navigationOptions = ({ navigation }) => ({ ...BlueNavigationStyle(navigation, true, () => navigation.dismiss()), title: loc.receive.header, headerLeft: null, }); constructor(props) { super(props); const invoice = props.navigation.getParam('invoice'); const fromWallet = props.navigation.getParam('fromWallet'); this.state = { invoice, fromWallet, addressText: typeof invoice === 'object' ? invoice.payment_request : invoice, }; this.fetchInvoiceInterval = undefined; } async componentDidMount() { if (!this.state.invoice.isLoading) { this.fetchInvoiceInterval = setInterval(async () => { const userInvoices = JSON.stringify(await this.state.fromWallet.getUserInvoices()); const updatedUserInvoice = JSON.parse(userInvoices).filter( invoice => invoice.payment_request === this.state.invoice.payment_request, )[0]; this.setState({ invoice: updatedUserInvoice }); if (updatedUserInvoice.ispaid) { ReactNativeHapticFeedback.trigger('notificationSuccess', false); clearInterval(this.fetchInvoiceInterval); EV(EV.enum.TRANSACTIONS_COUNT_CHANGED); } }, 5000); } } componentWillUnmount() { clearInterval(this.fetchInvoiceInterval) } copyToClipboard = () => { this.setState({ addressText: loc.receive.details.copiedToClipboard }, () => { Clipboard.setString(this.state.invoice); setTimeout(() => this.setState({ addressText: this.state.invoice }), 1000); }); }; render() { if (this.state.isLoading) { return ; } const { invoice } = this.state; if (typeof invoice === 'object') { const currentDate = new Date(); const now = (currentDate.getTime() / 1000) | 0; const invoiceExpiration = invoice.timestamp + invoice.expire_time; if (invoice.ispaid) { return ( This invoice has been paid for. ); } if (invoiceExpiration < now && !invoice.ispaid) { return ( This invoice was not paid for and has expired. ); } else if (invoiceExpiration > now && invoice.ispaid) { if (invoice.ispaid) { return ( 'This invoice has been paid for.' ); } } } // Invoice has not expired, nor has it been paid for. return ( {this.state.addressText} { Share.share({ message: invoice, }); }} title={loc.receive.details.share} /> ); } } const styles = StyleSheet.create({ address: { marginVertical: 32, fontSize: 15, color: '#9aa0aa', textAlign: 'center', }, }); LNDViewInvoice.propTypes = { navigation: PropTypes.shape({ goBack: PropTypes.function, getParam: PropTypes.function, }), };