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

124 lines
3.2 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 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,
2018-09-12 21:27:22 +02:00
tipError: 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,
2018-09-12 21:27:22 +02:00
tipError: '',
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;
2018-09-12 21:27:22 +02:00
const regexp = RegExp(/^(\d*([.]\d{0,8})?)$/);
const validTipInput = regexp.test(event.target.value);
const tipAmount = parseFloat(event.target.value);
2018-09-12 21:27:22 +02:00
let tipError;
if (!tipAmount) {
tipError = __('Tip must be a number');
} else if (tipAmount <= 0) {
tipError = __('Tip must be a positive number');
} else if (!validTipInput) {
tipError = __('Tip must have no more than 8 decimal places');
} else if (tipAmount === balance) {
tipError = __('Please decrease your tip to account for transaction fees');
} else if (tipAmount > balance) {
2018-09-12 21:27:22 +02:00
tipError = __('Not enough credits');
}
2017-09-17 22:33:52 +02:00
this.setState({
tipAmount,
2018-09-12 21:27:22 +02:00
tipError,
2017-09-17 22:33:52 +02:00
});
}
render() {
2018-09-26 19:48:07 +02:00
const { title, isPending, uri, onCancel } = this.props;
2018-09-12 21:27:22 +02:00
const { tipAmount, tipError } = this.state;
2017-09-17 22:33:52 +02:00
return (
2018-09-26 19:48:07 +02:00
<section className="card__content">
<FormField
autoFocus
label={
(tipAmount &&
tipAmount !== 0 &&
`Tip ${tipAmount.toFixed(8).replace(/\.?0+$/, '')} LBC`) ||
__('Amount')
}
postfix={__('LBC')}
className="input--price-amount"
error={tipError}
min="0"
step="any"
type="number"
placeholder="1.23"
onChange={event => this.handleSupportPriceChange(event)}
helper={
<p>
2018-09-26 19:48:07 +02:00
{__(`This will appear as a tip for "${title}".`)}{' '}
<Button label={__('Learn more')} button="link" href="https://lbry.io/faq/tipping" />
</p>
2018-09-26 19:48:07 +02:00
}
/>
<div className="card__content">
<div className="card__actions">
<Button
button="primary"
label={__('Send')}
disabled={isPending || tipError}
onClick={this.handleSendButtonClicked}
/>
<Button
button="link"
label={__('Cancel')}
onClick={onCancel}
navigateParams={{ uri }}
/>
</div>
2017-09-17 22:33:52 +02:00
</div>
2018-09-26 19:48:07 +02:00
</section>
2017-09-17 22:33:52 +02:00
);
}
}
export default WalletSendTip;