import React, { Component } from 'react'
import PropTypes from 'prop-types'
import Isvg from 'react-inlinesvg'
import paperPlane from 'icons/paper_plane.svg'
import link from 'icons/link.svg'
import FaAngleDown from 'react-icons/lib/fa/angle-down'
import { btc } from 'lib/utils'
import AmountInput from 'components/AmountInput'
import styles from './Pay.scss'
class Pay extends Component {
componentDidUpdate(prevProps) {
const {
isLn,
payform: { payInput },
fetchInvoice
} = this.props
// If LN go retrieve invoice details
if (prevProps.payform.payInput !== payInput && isLn) {
fetchInvoice(payInput)
}
}
render() {
const {
payform: { payInput, showErrors, invoice, showCurrencyFilters },
nodes,
ticker,
isOnchain,
isLn,
currentAmount,
fiatAmount,
payFormIsValid: { errors, isValid },
currentCurrencyFilters,
currencyName,
setPayAmount,
onPayAmountBlur,
setPayInput,
onPayInputBlur,
setCurrencyFilters,
onPaySubmit,
setCurrency
} = this.props
const displayNodeName = pubkey => {
const node = nodes.find(n => n.pub_key === pubkey)
if (node && node.alias.length) {
return node.alias
}
return pubkey ? pubkey.substring(0, 10) : ''
}
const onCurrencyFilterClick = currency => {
if (!isLn) {
// change the input amount
setPayAmount(btc.convert(ticker.currency, currency, currentAmount))
}
setCurrency(currency)
setCurrencyFilters(false)
}
return (
{isOnchain && (
On-Chain (~10 minutes)
)}
{isLn && (
{displayNodeName(invoice.destination)} ({invoice.description})
)}
setCurrencyFilters(!showCurrencyFilters)}
>
{currencyName}
{currentCurrencyFilters.map(filter => (
- onCurrencyFilterClick(filter.key)}>
{filter.name}
))}
{`≈ ${fiatAmount || 0} ${ticker.fiatTicker}`}
{showErrors.amount && {errors.amount}}
)
}
}
Pay.propTypes = {
payform: PropTypes.shape({
amount: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
payInput: PropTypes.string.isRequired,
invoice: PropTypes.object.isRequired,
showErrors: PropTypes.object.isRequired
}).isRequired,
currencyName: PropTypes.string.isRequired,
isOnchain: PropTypes.bool.isRequired,
isLn: PropTypes.bool.isRequired,
currentAmount: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
fiatAmount: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
payFormIsValid: PropTypes.shape({
errors: PropTypes.object,
isValid: PropTypes.bool
}).isRequired,
setPayAmount: PropTypes.func.isRequired,
onPayAmountBlur: PropTypes.func.isRequired,
setPayInput: PropTypes.func.isRequired,
onPayInputBlur: PropTypes.func.isRequired,
fetchInvoice: PropTypes.func.isRequired,
onPaySubmit: PropTypes.func.isRequired,
setCurrencyFilters: PropTypes.func.isRequired,
setCurrency: PropTypes.func.isRequired,
ticker: PropTypes.object.isRequired,
nodes: PropTypes.array.isRequired,
currentCurrencyFilters: PropTypes.array.isRequired
}
export default Pay