2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Button from 'component/button';
|
2019-02-13 17:27:20 +01:00
|
|
|
import { FormField, Form } from 'component/common/form';
|
2017-09-17 22:33:52 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
|
|
|
uri: string,
|
2019-07-17 05:23:45 +02:00
|
|
|
claimIsMine: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
title: string,
|
2019-04-24 16:02:08 +02:00
|
|
|
claim: StreamClaim,
|
2018-03-26 23:32:43 +02:00
|
|
|
isPending: boolean,
|
2019-07-23 22:13:06 +02:00
|
|
|
sendSupport: (number, string) => void,
|
2018-03-26 23:32:43 +02:00
|
|
|
onCancel: () => void,
|
|
|
|
sendTipCallback?: () => void,
|
2018-05-14 18:19:07 +02:00
|
|
|
balance: number,
|
2019-07-19 22:21:41 +02:00
|
|
|
isSupport: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
type State = {
|
2018-06-18 08:34:59 +02:00
|
|
|
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 = {
|
2018-06-18 08:34:59 +02:00
|
|
|
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() {
|
2019-07-24 15:41:05 +02:00
|
|
|
const { claim, sendSupport, isSupport, sendTipCallback } = this.props;
|
2018-04-04 01:58:58 +02:00
|
|
|
const { claim_id: claimId } = claim;
|
2018-06-18 08:34:59 +02:00
|
|
|
const { tipAmount } = this.state;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-07-24 15:41:05 +02:00
|
|
|
sendSupport(tipAmount, claimId, isSupport);
|
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<*>) {
|
2018-06-18 08:34:59 +02:00
|
|
|
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);
|
2018-06-18 08:34:59 +02:00
|
|
|
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');
|
2018-06-18 08:34:59 +02:00
|
|
|
} else if (tipAmount > balance) {
|
2018-09-12 21:27:22 +02:00
|
|
|
tipError = __('Not enough credits');
|
2018-06-18 08:34:59 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 22:33:52 +02:00
|
|
|
this.setState({
|
2018-06-18 08:34:59 +02:00
|
|
|
tipAmount,
|
2018-09-12 21:27:22 +02:00
|
|
|
tipError,
|
2017-09-17 22:33:52 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-07-23 22:13:06 +02:00
|
|
|
const { title, isPending, onCancel, claimIsMine, isSupport } = this.props;
|
2018-09-12 21:27:22 +02:00
|
|
|
const { tipAmount, tipError } = this.state;
|
2017-09-17 22:33:52 +02:00
|
|
|
|
|
|
|
return (
|
2019-01-11 17:34:36 +01:00
|
|
|
<React.Fragment>
|
2019-07-21 23:31:22 +02:00
|
|
|
<Form>
|
2019-01-11 17:34:36 +01:00
|
|
|
<FormField
|
|
|
|
autoFocus
|
2019-02-20 06:20:29 +01:00
|
|
|
name="tip-input"
|
2019-01-11 17:34:36 +01:00
|
|
|
label={
|
2019-09-04 23:43:37 +02:00
|
|
|
tipAmount && tipAmount !== 0
|
|
|
|
? __(isSupport ? 'Support %amount% LBC' : 'Tip %amount% LBC', {
|
|
|
|
amount: tipAmount.toFixed(8).replace(/\.?0+$/, ''),
|
|
|
|
})
|
|
|
|
: __('Amount')
|
2019-01-11 17:34:36 +01:00
|
|
|
}
|
2019-02-13 17:27:20 +01:00
|
|
|
className="form-field--price-amount"
|
2019-01-11 17:34:36 +01:00
|
|
|
error={tipError}
|
|
|
|
min="0"
|
|
|
|
step="any"
|
|
|
|
type="number"
|
|
|
|
placeholder="1.23"
|
|
|
|
onChange={event => this.handleSupportPriceChange(event)}
|
2019-02-13 17:27:20 +01:00
|
|
|
inputButton={
|
|
|
|
<Button
|
|
|
|
button="primary"
|
|
|
|
label={__('Send')}
|
2019-05-06 19:22:10 +02:00
|
|
|
disabled={isPending || tipError || !tipAmount}
|
2019-02-13 17:27:20 +01:00
|
|
|
onClick={this.handleSendButtonClicked}
|
|
|
|
/>
|
|
|
|
}
|
2019-01-11 17:34:36 +01:00
|
|
|
helper={
|
2019-07-21 23:31:22 +02:00
|
|
|
<React.Fragment>
|
2019-07-24 00:55:34 +02:00
|
|
|
{claimIsMine || isSupport
|
|
|
|
? __('This will increase the overall bid amount for ')
|
2019-07-17 05:23:45 +02:00
|
|
|
: __('This will appear as a tip for ')}
|
|
|
|
{`"${title}" which will boost its ability to be discovered while active.`}{' '}
|
2019-07-19 22:21:41 +02:00
|
|
|
<Button label={__('Learn more')} button="link" href="https://lbry.com/faq/tipping" />.
|
2019-07-21 23:31:22 +02:00
|
|
|
</React.Fragment>
|
2019-01-11 17:34:36 +01:00
|
|
|
}
|
|
|
|
/>
|
2019-02-13 17:27:20 +01:00
|
|
|
</Form>
|
2019-01-11 17:34:36 +01:00
|
|
|
<div className="card__actions">
|
2019-07-23 22:13:06 +02:00
|
|
|
<Button button="link" label={__('Cancel')} onClick={onCancel} />
|
2017-09-17 22:33:52 +02:00
|
|
|
</div>
|
2019-01-11 17:34:36 +01:00
|
|
|
</React.Fragment>
|
2017-09-17 22:33:52 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WalletSendTip;
|