lbry-desktop/ui/component/filePrice/view.jsx

78 lines
2 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2020-05-21 17:38:28 +02:00
import * as ICONS from 'constants/icons';
import React from 'react';
2020-05-21 17:38:28 +02:00
import classnames from 'classnames';
2018-03-26 23:32:43 +02:00
import CreditAmount from 'component/common/credit-amount';
2020-05-21 17:38:28 +02:00
import Icon from 'component/common/icon';
2018-03-26 23:32:43 +02:00
type Props = {
showFullPrice: boolean,
costInfo: ?{ includesData: boolean, cost: number },
fetchCostInfo: string => void,
uri: string,
fetching: boolean,
claim: ?{},
2020-05-21 17:38:28 +02:00
claimWasPurchased: boolean,
claimIsMine: boolean,
type?: string,
2018-07-25 06:23:58 +02:00
// below props are just passed to <CreditAmount />
inheritStyle?: boolean,
showLBC?: boolean,
2018-08-06 19:58:33 +02:00
hideFree?: boolean, // hide the file price if it's free
2018-03-26 23:32:43 +02:00
};
class FilePrice extends React.PureComponent<Props> {
static defaultProps = {
showFullPrice: false,
};
2019-08-21 20:22:51 +02:00
componentDidMount() {
this.fetchCost(this.props);
}
2019-08-21 20:22:51 +02:00
componentDidUpdate() {
this.fetchCost(this.props);
}
fetchCost = (props: Props) => {
const { costInfo, fetchCostInfo, uri, fetching, claim } = props;
if (costInfo === undefined && !fetching && claim) {
2017-06-06 23:19:12 +02:00
fetchCostInfo(uri);
2017-05-12 19:14:06 +02:00
}
};
render() {
2020-05-21 17:38:28 +02:00
const { costInfo, showFullPrice, showLBC, hideFree, claimWasPurchased, type, claimIsMine } = this.props;
if (claimIsMine || !costInfo || !costInfo.cost || (!costInfo.cost && hideFree)) {
2018-08-06 19:58:33 +02:00
return null;
}
2017-05-12 19:14:06 +02:00
2020-05-21 17:38:28 +02:00
return claimWasPurchased ? (
<span
className={classnames('file-price__key', {
'file-price__key--filepage': type === 'filepage',
'file-price__key--modal': type === 'modal',
})}
>
<Icon icon={ICONS.PURCHASED} size={type === 'filepage' ? 22 : undefined} />
</span>
) : (
2017-06-06 23:19:12 +02:00
<CreditAmount
2020-05-21 17:38:28 +02:00
className={classnames('file-price', {
'file-price--filepage': type === 'filepage',
'file-price--modal': type === 'modal',
})}
showFree
2018-07-25 06:23:58 +02:00
showLBC={showLBC}
2017-06-06 23:19:12 +02:00
amount={costInfo.cost}
isEstimate={!costInfo.includesData}
showFullPrice={showFullPrice}
2017-06-06 23:19:12 +02:00
/>
2020-05-21 17:38:28 +02:00
);
2017-05-12 19:14:06 +02:00
}
}
2017-06-06 06:21:55 +02:00
export default FilePrice;