lbry-desktop/src/renderer/component/common/credit-amount.jsx

105 lines
2.1 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import React from 'react';
import classnames from 'classnames';
2018-11-21 22:20:55 +01:00
import { formatCredits, formatFullPrice } from 'util/format-credits';
2018-03-26 23:32:43 +02:00
type Props = {
amount: number,
precision: number,
showFree: boolean,
showFullPrice: boolean,
showPlus: boolean,
isEstimate?: boolean,
large?: boolean,
showLBC?: boolean,
2018-03-26 23:32:43 +02:00
fee?: boolean,
2018-10-19 22:38:07 +02:00
badge?: boolean,
2018-03-26 23:32:43 +02:00
};
class CreditAmount extends React.PureComponent<Props> {
static defaultProps = {
precision: 2,
showFree: false,
showFullPrice: false,
showPlus: false,
showLBC: true,
2018-03-26 23:32:43 +02:00
};
render() {
const {
amount,
precision,
showFullPrice,
showFree,
showPlus,
large,
isEstimate,
fee,
showLBC,
2018-10-19 22:38:07 +02:00
badge,
2018-03-26 23:32:43 +02:00
} = this.props;
const minimumRenderableAmount = 10 ** (-1 * precision);
const fullPrice = formatFullPrice(amount, 2);
const isFree = parseFloat(amount) === 0;
let formattedAmount;
if (showFullPrice) {
formattedAmount = fullPrice;
} else {
formattedAmount =
amount > 0 && amount < minimumRenderableAmount
? `<${minimumRenderableAmount}`
: formatCredits(amount, precision);
}
let amountText;
if (showFree && isFree) {
amountText = __('FREE');
} else {
amountText = formattedAmount;
if (showPlus && amount > 0) {
amountText = `+${amountText}`;
}
if (showLBC) {
amountText = (
<span>
{amountText} {__('LBC')}
</span>
);
2018-03-26 23:32:43 +02:00
}
if (fee) {
amountText = `${amountText} ${__('fee')}`;
}
}
return (
<span
title={fullPrice}
className={classnames('badge', {
2018-10-31 17:33:45 +01:00
badge,
'badge--cost': (badge && !isFree) || amount > 0,
2018-10-19 22:38:07 +02:00
'badge--free': badge && isFree,
'badge--large': large,
2018-03-26 23:32:43 +02:00
})}
>
{amountText}
{isEstimate ? (
<span
className="credit-amount__estimate"
title={__('This is an estimate and does not include data fees')}
>
*
</span>
) : null}
</span>
);
}
}
export default CreditAmount;