2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2020-06-08 20:42:29 +02:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
|
|
|
import * as ICONS from 'constants/icons';
|
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';
|
2019-11-13 19:11:51 +01:00
|
|
|
import { MINIMUM_PUBLISH_BID } from 'constants/claim';
|
2020-03-13 20:53:53 +01:00
|
|
|
import useIsMobile from 'effects/use-is-mobile';
|
|
|
|
import CreditAmount from 'component/common/credit-amount';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
2020-04-25 01:53:57 +02:00
|
|
|
import { Lbryio } from 'lbryinc';
|
2020-06-08 20:42:29 +02:00
|
|
|
import Card from 'component/common/card';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
const DEFAULT_TIP_AMOUNTS = [5, 10, 50];
|
2017-09-17 22:33:52 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
|
|
|
uri: string,
|
2020-06-08 20:42:29 +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-10-26 23:16:51 +02:00
|
|
|
sendSupport: (number, string, boolean) => void,
|
2020-04-30 00:02:53 +02:00
|
|
|
closeModal: () => void,
|
2018-05-14 18:19:07 +02:00
|
|
|
balance: number,
|
2019-07-19 22:21:41 +02:00
|
|
|
isSupport: boolean,
|
2020-04-23 10:18:17 +02:00
|
|
|
openModal: (id: string, { tipAmount: number, claimId: string, isSupport: boolean }) => void,
|
2020-04-25 01:53:57 +02:00
|
|
|
instantTipEnabled: boolean,
|
|
|
|
instantTipMax: { amount: number, currency: string },
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2020-03-13 20:53:53 +01:00
|
|
|
function WalletSendTip(props: Props) {
|
2020-04-25 01:53:57 +02:00
|
|
|
const {
|
2020-05-11 21:20:37 +02:00
|
|
|
uri,
|
2020-04-25 01:53:57 +02:00
|
|
|
title,
|
|
|
|
isPending,
|
2020-06-08 20:42:29 +02:00
|
|
|
// claimIsMine,
|
2020-04-25 01:53:57 +02:00
|
|
|
balance,
|
2020-06-08 20:42:29 +02:00
|
|
|
claim = {},
|
2020-04-25 01:53:57 +02:00
|
|
|
instantTipEnabled,
|
|
|
|
instantTipMax,
|
|
|
|
openModal,
|
|
|
|
sendSupport,
|
2020-04-30 00:02:53 +02:00
|
|
|
closeModal,
|
2020-04-25 01:53:57 +02:00
|
|
|
} = props;
|
2020-06-08 20:42:29 +02:00
|
|
|
const [tipAmount, setTipAmount] = React.useState(DEFAULT_TIP_AMOUNTS[0]);
|
2020-03-13 20:53:53 +01:00
|
|
|
const [tipError, setTipError] = React.useState();
|
2020-06-08 20:42:29 +02:00
|
|
|
const [isSupport, setIsSupport] = React.useState(false);
|
|
|
|
const [showMore, setShowMore] = React.useState(false);
|
2020-03-13 20:53:53 +01:00
|
|
|
const { claim_id: claimId } = claim;
|
|
|
|
const isMobile = useIsMobile();
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2020-04-25 01:53:57 +02:00
|
|
|
function sendSupportOrConfirm(instantTipMaxAmount = null) {
|
|
|
|
if (!isSupport && (!instantTipMaxAmount || !instantTipEnabled || tipAmount > instantTipMaxAmount)) {
|
2020-05-13 23:43:50 +02:00
|
|
|
const modalProps = { uri, tipAmount, claimId, title, isSupport };
|
2020-04-25 01:53:57 +02:00
|
|
|
openModal(MODALS.CONFIRM_SEND_TIP, modalProps);
|
|
|
|
} else {
|
|
|
|
sendSupport(tipAmount, claimId, isSupport);
|
2020-04-30 00:02:53 +02:00
|
|
|
closeModal();
|
2020-04-25 01:53:57 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 18:44:18 +02:00
|
|
|
|
2020-04-25 01:53:57 +02:00
|
|
|
function handleSubmit() {
|
2020-04-23 10:18:17 +02:00
|
|
|
if (tipAmount && claimId) {
|
2020-04-25 01:53:57 +02:00
|
|
|
if (instantTipEnabled) {
|
|
|
|
if (instantTipMax.currency === 'LBC') {
|
|
|
|
sendSupportOrConfirm(instantTipMax.amount);
|
|
|
|
} else {
|
|
|
|
// Need to convert currency of instant purchase maximum before trying to send support
|
|
|
|
Lbryio.getExchangeRates().then(({ LBC_USD }) => {
|
|
|
|
sendSupportOrConfirm(instantTipMax.amount / LBC_USD);
|
|
|
|
});
|
|
|
|
}
|
2020-04-23 18:44:18 +02:00
|
|
|
} else {
|
2020-04-25 01:53:57 +02:00
|
|
|
sendSupportOrConfirm();
|
2020-04-23 18:44:18 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
2017-09-17 22:33:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-13 20:53:53 +01:00
|
|
|
function handleSupportPriceChange(event: SyntheticInputEvent<*>) {
|
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) {
|
2019-11-13 19:11:51 +01:00
|
|
|
tipError = __('Amount must be a number');
|
2018-09-12 21:27:22 +02:00
|
|
|
} else if (tipAmount <= 0) {
|
2019-11-13 19:11:51 +01:00
|
|
|
tipError = __('Amount must be a positive number');
|
|
|
|
} else if (tipAmount < MINIMUM_PUBLISH_BID) {
|
|
|
|
tipError = __('Amount must be higher');
|
2018-09-12 21:27:22 +02:00
|
|
|
} else if (!validTipInput) {
|
2019-11-13 19:11:51 +01:00
|
|
|
tipError = __('Amount must have no more than 8 decimal places');
|
2018-09-12 21:27:22 +02:00
|
|
|
} else if (tipAmount === balance) {
|
2019-11-13 19:11:51 +01:00
|
|
|
tipError = __('Please decrease the amount 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
|
|
|
}
|
|
|
|
|
2020-03-13 20:53:53 +01:00
|
|
|
setTipAmount(tipAmount);
|
|
|
|
setTipError(tipError);
|
2017-09-17 22:33:52 +02:00
|
|
|
}
|
|
|
|
|
2020-06-08 20:42:29 +02:00
|
|
|
// const label =
|
|
|
|
// tipAmount && tipAmount !== 0
|
|
|
|
// ? __(isSupport ? 'Support %amount% LBC' : 'Tip %amount% LBC', {
|
|
|
|
// amount: tipAmount.toFixed(8).replace(/\.?0+$/, ''),
|
|
|
|
// })
|
|
|
|
// : __('Amount');
|
2017-09-17 22:33:52 +02:00
|
|
|
|
2020-03-13 20:53:53 +01:00
|
|
|
return (
|
2020-06-08 20:42:29 +02:00
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
<Card
|
|
|
|
title={__('Support This Content')}
|
|
|
|
subtitle={
|
|
|
|
<React.Fragment>
|
|
|
|
{__(
|
|
|
|
'This will increase the overall bid amount for this content, which will boost its ability to be discovered while active.'
|
|
|
|
)}{' '}
|
|
|
|
<Button label={__('Learn more')} button="link" href="https://lbry.com/faq/tipping" />.
|
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<>
|
|
|
|
<div className="section">
|
|
|
|
{DEFAULT_TIP_AMOUNTS.map(amount => (
|
|
|
|
<Button
|
|
|
|
key={amount}
|
|
|
|
button="alt"
|
|
|
|
className={classnames('button-toggle', { 'button-toggle--active': tipAmount === amount })}
|
|
|
|
label={`${amount} LBC`}
|
|
|
|
onClick={() => setTipAmount(amount)}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
className={classnames('button-toggle', {
|
|
|
|
'button-toggle--active': !DEFAULT_TIP_AMOUNTS.includes(tipAmount),
|
|
|
|
})}
|
|
|
|
label={__('Custom')}
|
|
|
|
onClick={() => setShowMore(true)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{showMore && (
|
|
|
|
<div className="section">
|
|
|
|
<FormField
|
|
|
|
autoFocus
|
|
|
|
name="tip-input"
|
|
|
|
label={
|
|
|
|
<React.Fragment>
|
|
|
|
{'Custom support amount'}{' '}
|
|
|
|
{isMobile && (
|
|
|
|
<I18nMessage tokens={{ lbc_balance: <CreditAmount badge={false} amount={balance} /> }}>
|
|
|
|
(%lbc_balance% available)
|
|
|
|
</I18nMessage>
|
|
|
|
)}
|
|
|
|
</React.Fragment>
|
|
|
|
}
|
|
|
|
className="form-field--price-amount"
|
|
|
|
error={tipError}
|
|
|
|
min="0"
|
|
|
|
step="any"
|
|
|
|
type="number"
|
|
|
|
placeholder="1.23"
|
|
|
|
onChange={event => handleSupportPriceChange(event)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button
|
|
|
|
autoFocus
|
|
|
|
icon={isSupport ? undefined : ICONS.SUPPORT}
|
|
|
|
button="primary"
|
|
|
|
type="submit"
|
|
|
|
label={
|
|
|
|
isSupport
|
|
|
|
? __('Send Revokable Support')
|
|
|
|
: __('Send a %amount% Tip', { amount: tipAmount ? `${tipAmount} LBC` : '' })
|
|
|
|
}
|
|
|
|
disabled={isPending || tipError || !tipAmount}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
name="toggle-is-support"
|
|
|
|
type="checkbox"
|
|
|
|
label={__('Make this support permanent')}
|
|
|
|
checked={!isSupport}
|
|
|
|
onChange={() => setIsSupport(!isSupport)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Form>
|
2020-03-13 20:53:53 +01:00
|
|
|
);
|
2017-09-17 22:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default WalletSendTip;
|
2020-06-08 20:42:29 +02:00
|
|
|
|
|
|
|
// <Button
|
|
|
|
// button="primary"
|
|
|
|
// type="submit"
|
|
|
|
// label={__('Confirm')}
|
|
|
|
// disabled={isPending || tipError || !tipAmount}
|
|
|
|
// />;
|
|
|
|
|
|
|
|
// <div className="section__actions">
|
|
|
|
// <FormField
|
|
|
|
// autoFocus
|
|
|
|
// name="tip-input"
|
|
|
|
// label={
|
|
|
|
// <React.Fragment>
|
|
|
|
// {label}{' '}
|
|
|
|
// {isMobile && (
|
|
|
|
// <I18nMessage tokens={{ lbc_balance: <CreditAmount badge={false} amount={balance} /> }}>
|
|
|
|
// (%lbc_balance% available)
|
|
|
|
// </I18nMessage>
|
|
|
|
// )}
|
|
|
|
// </React.Fragment>
|
|
|
|
// }
|
|
|
|
// className="form-field--price-amount"
|
|
|
|
// error={tipError}
|
|
|
|
// min="0"
|
|
|
|
// step="any"
|
|
|
|
// type="number"
|
|
|
|
// placeholder="1.23"
|
|
|
|
// onChange={event => handleSupportPriceChange(event)}
|
|
|
|
// />
|
|
|
|
// <FormField
|
|
|
|
// name="toggle-is-support"
|
|
|
|
// type="checkbox"
|
|
|
|
// label={__('Send this as a tip instead')}
|
|
|
|
// checked={!isSupport}
|
|
|
|
// onChange={() => setIsSupport(!isSupport)}
|
|
|
|
// />
|
|
|
|
// </div>;
|