Update handling of file prices

- Until Lighthouse results come back, display just the key fee
 - Add support for displaying prices without special formatting
 - Refactor and simplify FilePrice and CreditAmount
This commit is contained in:
Alex Liebowitz 2017-04-17 05:10:45 -04:00
parent 7644566ef5
commit 5fe9f076eb
2 changed files with 23 additions and 24 deletions

View file

@ -63,23 +63,32 @@ export let CreditAmount = React.createClass({
amount: React.PropTypes.number.isRequired, amount: React.PropTypes.number.isRequired,
precision: React.PropTypes.number, precision: React.PropTypes.number,
label: React.PropTypes.bool, label: React.PropTypes.bool,
showFree: React.PropTypes.bool,
look: React.PropTypes.oneOf(['indicator', 'plain']), look: React.PropTypes.oneOf(['indicator', 'plain']),
}, },
getDefaultProps: function() { getDefaultProps: function() {
return { return {
precision: 1, precision: 1,
label: true, label: true,
showFree: false,
look: 'indicator', look: 'indicator',
} }
}, },
render: function() { render: function() {
var formattedAmount = lbry.formatCredits(this.props.amount, this.props.precision); const formattedAmount = lbry.formatCredits(this.props.amount, this.props.precision);
let amountText;
if (this.props.showFree && parseFloat(formattedAmount) == 0) {
amountText = 'free';
} else if (this.props.label) {
amountText = formattedAmount + (parseFloat(formattedAmount) == 1 ? ' credit' : ' credits');
} else {
amountText = formattedAmount;
}
return ( return (
<span className={`credit-amount credit-amount--${this.props.look}`}> <span className={`credit-amount credit-amount--${this.props.look}`}>
<span> <span>
{formattedAmount} {amountText}
{this.props.label ?
(parseFloat(formattedAmount) == 1.0 ? ' credit' : ' credits') : '' }
</span> </span>
{ this.props.isEstimate ? <span className="credit-amount__estimate" title="This is an estimate and does not include data fees">*</span> : null } { this.props.isEstimate ? <span className="credit-amount__estimate" title="This is an estimate and does not include data fees">*</span> : null }
</span> </span>
@ -102,11 +111,11 @@ export let FilePrice = React.createClass({
} }
}, },
getInitialState: function() { componentWillMount: function() {
return { this.setState({
cost: null, cost: this.props.metadata ? this.props.metadata.fee : null,
isEstimate: null, isEstimate: this.props.metadata ? true : null,
} });
}, },
componentDidMount: function() { componentDidMount: function() {
@ -128,22 +137,11 @@ export let FilePrice = React.createClass({
}, },
render: function() { render: function() {
if (this.state.cost === null && this.props.metadata) { if (this.state.cost === null) {
if (!this.props.metadata.fee) { return <span className={`credit-amount credit-amount--${this.props.look}`}>???</span>;
return <span className={`credit-amount credit-amount--${this.props.look}`}>free*</span>;
} else {
if (this.props.metadata.fee.currency === "LBC") {
return <CreditAmount label={false} amount={this.props.metadata.fee.amount} isEstimate={true} />
} else if (this.props.metadata.fee.currency === "USD") {
return <span className={`credit-amount credit-amount--${this.props.look}`}>???</span>;
}
}
} }
return (
this.state.cost !== null ? return <CreditAmount label={false} amount={this.props.metadata.fee.amount} isEstimate={true} showFree={true} />
<CreditAmount amount={this.state.cost} label={false} isEstimate={!this.state.isEstimate} look={this.props.look} /> :
<span className={`credit-amount credit-amount--${this.props.look}`}>???</span>
);
} }
}); });

View file

@ -1,3 +1,4 @@
import lbryio from './lbryio.js';
import lighthouse from './lighthouse.js'; import lighthouse from './lighthouse.js';
import jsonrpc from './jsonrpc.js'; import jsonrpc from './jsonrpc.js';
import uri from './uri.js'; import uri from './uri.js';