lbry-desktop/src/renderer/component/walletSendTip/view.jsx

116 lines
3 KiB
React
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
import { FormField } from 'component/common/form';
import UriIndicator from 'component/uriIndicator';
import type { Claim } from 'types/claim';
2017-09-17 22:33:52 +02:00
2018-03-26 23:32:43 +02:00
type Props = {
uri: string,
title: string,
claim: Claim,
2018-03-26 23:32:43 +02:00
isPending: boolean,
sendSupport: (number, string, string) => void,
onCancel: () => void,
sendTipCallback?: () => void,
balance: number,
2018-03-26 23:32:43 +02:00
};
type State = {
tipAmount: number,
newTipError: string,
2018-03-26 23:32:43 +02:00
};
class WalletSendTip extends React.PureComponent<Props, State> {
constructor(props: Props) {
2017-09-17 22:33:52 +02:00
super(props);
this.state = {
tipAmount: 0,
newTipError: '',
2017-09-17 22:33:52 +02:00
};
2018-03-26 23:32:43 +02:00
(this: any).handleSendButtonClicked = this.handleSendButtonClicked.bind(this);
2017-09-17 22:33:52 +02:00
}
handleSendButtonClicked() {
2018-04-04 01:58:58 +02:00
const { claim, uri, sendSupport, sendTipCallback } = this.props;
const { claim_id: claimId } = claim;
const { tipAmount } = this.state;
2018-03-26 23:32:43 +02:00
sendSupport(tipAmount, claimId, uri);
2018-03-26 23:32:43 +02:00
// ex: close modal
if (sendTipCallback) {
sendTipCallback();
}
2017-09-17 22:33:52 +02:00
}
2018-03-26 23:32:43 +02:00
handleSupportPriceChange(event: SyntheticInputEvent<*>) {
const { balance } = this.props;
const tipAmount = parseFloat(event.target.value);
let newTipError;
if (tipAmount === balance) {
newTipError = __('Please decrease your tip to account for transaction fees');
} else if (tipAmount > balance) {
newTipError = __('Not enough credits');
}
2017-09-17 22:33:52 +02:00
this.setState({
tipAmount,
newTipError,
2017-09-17 22:33:52 +02:00
});
}
render() {
const { title, isPending, uri, onCancel, balance } = this.props;
const { tipAmount, newTipError } = this.state;
2017-09-17 22:33:52 +02:00
return (
<div>
2018-03-26 23:32:43 +02:00
<div className="card__title">
2017-11-24 15:31:05 +01:00
<h1>
2018-03-26 23:32:43 +02:00
{__('Send a tip to')} <UriIndicator uri={uri} />
2017-11-24 15:31:05 +01:00
</h1>
2017-09-17 22:33:52 +02:00
</div>
<div className="card__content">
2018-03-26 23:32:43 +02:00
<FormField
label={__('Amount')}
postfix={__('LBC')}
2018-03-26 23:32:43 +02:00
className="input--price-amount"
error={newTipError}
value={tipAmount || ''}
2017-09-17 22:33:52 +02:00
min="0"
step="any"
2017-09-17 22:33:52 +02:00
type="number"
2018-03-26 23:32:43 +02:00
placeholder="1.00"
onChange={event => this.handleSupportPriceChange(event)}
2017-09-17 22:33:52 +02:00
helper={
<span>
2018-03-26 23:32:43 +02:00
{__(`This will appear as a tip for ${title} located at ${uri}.`)}{' '}
<Button label={__('Learn more')} button="link" href="https://lbry.io/faq/tipping" />
2017-09-17 22:33:52 +02:00
</span>
}
/>
2018-03-26 23:32:43 +02:00
<div className="card__actions">
<Button
2017-09-17 22:33:52 +02:00
button="primary"
2018-03-26 23:32:43 +02:00
label={__('Send')}
disabled={isPending || tipAmount <= 0 || tipAmount > balance || tipAmount === balance}
2018-03-26 23:32:43 +02:00
onClick={this.handleSendButtonClicked}
2017-09-17 22:33:52 +02:00
/>
2018-06-13 07:20:53 +02:00
<Button
button="link"
label={__('Cancel')}
onClick={onCancel}
navigateParams={{ uri }}
/>
2017-09-17 22:33:52 +02:00
</div>
</div>
</div>
);
}
}
export default WalletSendTip;