2021-04-23 21:59:48 +02:00
|
|
|
// @flow
|
2021-10-28 22:25:34 +02:00
|
|
|
import 'scss/component/_wallet-tip-selector.scss';
|
|
|
|
import { FormField } from 'component/common/form';
|
|
|
|
import { Lbryio } from 'lbryinc';
|
|
|
|
import { MINIMUM_PUBLISH_BID } from 'constants/claim';
|
|
|
|
import { useIsMobile } from 'effects/use-screensize';
|
2021-04-23 21:59:48 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
|
|
|
import * as PAGES from 'constants/pages';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import classnames from 'classnames';
|
2021-10-28 22:25:34 +02:00
|
|
|
import React from 'react';
|
2021-04-23 21:59:48 +02:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
|
|
|
import WalletSpendableBalanceHelp from 'component/walletSpendableBalanceHelp';
|
2021-10-28 22:25:34 +02:00
|
|
|
|
2021-08-18 00:34:16 +02:00
|
|
|
import { getStripeEnvironment } from 'util/stripe';
|
2021-10-28 22:25:34 +02:00
|
|
|
const stripeEnvironment = getStripeEnvironment();
|
2021-04-23 21:59:48 +02:00
|
|
|
|
|
|
|
const DEFAULT_TIP_AMOUNTS = [1, 5, 25, 100];
|
2021-07-06 22:28:29 +02:00
|
|
|
const TAB_FIAT = 'TabFiat';
|
|
|
|
const TAB_LBC = 'TabLBC';
|
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
type Props = {
|
2021-10-28 22:25:34 +02:00
|
|
|
activeTab: string,
|
2021-04-23 21:59:48 +02:00
|
|
|
amount: number,
|
2021-10-28 22:25:34 +02:00
|
|
|
balance: number,
|
2021-07-06 22:28:29 +02:00
|
|
|
claim: StreamClaim,
|
2021-10-28 22:25:34 +02:00
|
|
|
convertedAmount?: number,
|
|
|
|
customTipAmount?: number,
|
2021-11-05 20:31:51 +01:00
|
|
|
exchangeRate?: any,
|
2021-10-28 22:25:34 +02:00
|
|
|
fiatConversion?: boolean,
|
|
|
|
tipError: boolean,
|
|
|
|
tipError: string,
|
2021-07-06 22:28:29 +02:00
|
|
|
uri: string,
|
2021-10-28 22:25:34 +02:00
|
|
|
onChange: (number) => void,
|
|
|
|
setConvertedAmount?: (number) => void,
|
|
|
|
setDisableSubmitButton: (boolean) => void,
|
|
|
|
setTipError: (any) => void,
|
2021-04-23 21:59:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function WalletTipAmountSelector(props: Props) {
|
2021-10-28 22:25:34 +02:00
|
|
|
const {
|
|
|
|
activeTab,
|
|
|
|
amount,
|
|
|
|
balance,
|
|
|
|
claim,
|
|
|
|
convertedAmount,
|
|
|
|
customTipAmount,
|
2021-11-05 20:31:51 +01:00
|
|
|
exchangeRate,
|
2021-10-28 22:25:34 +02:00
|
|
|
fiatConversion,
|
|
|
|
tipError,
|
|
|
|
onChange,
|
|
|
|
setConvertedAmount,
|
|
|
|
setDisableSubmitButton,
|
|
|
|
setTipError,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
const isMobile = useIsMobile();
|
|
|
|
const [useCustomTip, setUseCustomTip] = usePersistedState('comment-support:useCustomTip', true);
|
2021-07-06 22:28:29 +02:00
|
|
|
const [hasCardSaved, setHasSavedCard] = usePersistedState('comment-support:hasCardSaved', false);
|
2021-10-28 22:25:34 +02:00
|
|
|
const [canReceiveFiatTip, setCanReceiveFiatTip] = React.useState(); // dont persist because it needs to be calc'd per creator
|
2021-11-05 20:31:51 +01:00
|
|
|
|
|
|
|
const convertToTwoDecimalsOrMore = (number: number, decimals: number = 2) =>
|
|
|
|
Number((Math.round(number * 10 ** decimals) / 10 ** decimals).toFixed(decimals));
|
2021-10-28 22:25:34 +02:00
|
|
|
|
|
|
|
const tipAmountsToDisplay =
|
2021-11-05 20:31:51 +01:00
|
|
|
customTipAmount && fiatConversion && activeTab === TAB_FIAT
|
|
|
|
? [customTipAmount]
|
|
|
|
: customTipAmount && exchangeRate
|
|
|
|
? [convertToTwoDecimalsOrMore(customTipAmount / exchangeRate)]
|
|
|
|
: DEFAULT_TIP_AMOUNTS;
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2021-07-19 03:49:21 +02:00
|
|
|
// if it's fiat but there's no card saved OR the creator can't receive fiat tips
|
2021-07-22 23:29:57 +02:00
|
|
|
const shouldDisableFiatSelectors = activeTab === TAB_FIAT && (!hasCardSaved || !canReceiveFiatTip);
|
2021-10-28 22:25:34 +02:00
|
|
|
if (setDisableSubmitButton) setDisableSubmitButton(shouldDisableFiatSelectors);
|
|
|
|
|
|
|
|
// setup variables for tip API
|
|
|
|
const channelClaimId = claim.signing_channel ? claim.signing_channel.claim_id : claim.claim_id;
|
|
|
|
const tipChannelName = claim.signing_channel ? claim.signing_channel.name : claim.name;
|
2021-07-19 03:49:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* whether tip amount selection/review functionality should be disabled
|
|
|
|
* @param [amount] LBC amount (optional)
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2021-10-28 22:25:34 +02:00
|
|
|
function shouldDisableAmountSelector(amount: number) {
|
2021-07-19 03:49:21 +02:00
|
|
|
// if it's LBC but the balance isn't enough, or fiat conditions met
|
|
|
|
// $FlowFixMe
|
2021-10-28 22:25:34 +02:00
|
|
|
return (
|
|
|
|
((amount > balance || balance === 0) && activeTab !== TAB_FIAT) ||
|
|
|
|
shouldDisableFiatSelectors ||
|
|
|
|
(customTipAmount && fiatConversion && activeTab !== TAB_FIAT && exchangeRate
|
2021-11-05 20:31:51 +01:00
|
|
|
? convertToTwoDecimalsOrMore(amount * exchangeRate) < customTipAmount
|
2021-10-28 22:25:34 +02:00
|
|
|
: customTipAmount && amount < customTipAmount)
|
|
|
|
);
|
2021-07-06 22:28:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
// parse number as float and sets it in the parent component
|
|
|
|
function handleCustomPriceChange(amount: number) {
|
|
|
|
const tipAmountValue = parseFloat(amount);
|
|
|
|
onChange(tipAmountValue);
|
|
|
|
if (fiatConversion && exchangeRate && setConvertedAmount && convertedAmount !== tipAmountValue * exchangeRate) {
|
|
|
|
setConvertedAmount(tipAmountValue * exchangeRate);
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 22:28:29 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-11-05 20:31:51 +01:00
|
|
|
if (setConvertedAmount && exchangeRate && (!convertedAmount || convertedAmount !== amount * exchangeRate)) {
|
2021-10-28 22:25:34 +02:00
|
|
|
setConvertedAmount(amount * exchangeRate);
|
2021-08-18 00:34:16 +02:00
|
|
|
}
|
2021-10-28 22:25:34 +02:00
|
|
|
}, [amount, convertedAmount, exchangeRate, setConvertedAmount]);
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
// check if user has a payment method saved
|
2021-04-23 21:59:48 +02:00
|
|
|
React.useEffect(() => {
|
2021-10-28 22:25:34 +02:00
|
|
|
if (!stripeEnvironment) return;
|
|
|
|
|
|
|
|
Lbryio.call(
|
|
|
|
'customer',
|
|
|
|
'status',
|
|
|
|
{
|
|
|
|
environment: stripeEnvironment,
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
).then((customerStatusResponse) => {
|
|
|
|
const defaultPaymentMethodId =
|
|
|
|
customerStatusResponse.Customer &&
|
|
|
|
customerStatusResponse.Customer.invoice_settings &&
|
|
|
|
customerStatusResponse.Customer.invoice_settings.default_payment_method &&
|
|
|
|
customerStatusResponse.Customer.invoice_settings.default_payment_method.id;
|
|
|
|
|
|
|
|
setHasSavedCard(Boolean(defaultPaymentMethodId));
|
|
|
|
});
|
|
|
|
}, [setHasSavedCard]);
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2021-11-05 20:31:51 +01:00
|
|
|
// check if creator has a tip account saved
|
2021-07-06 22:28:29 +02:00
|
|
|
React.useEffect(() => {
|
2021-10-28 22:25:34 +02:00
|
|
|
if (!stripeEnvironment) return;
|
|
|
|
|
|
|
|
Lbryio.call(
|
|
|
|
'account',
|
|
|
|
'check',
|
|
|
|
{
|
|
|
|
channel_claim_id: channelClaimId,
|
|
|
|
channel_name: tipChannelName,
|
|
|
|
environment: stripeEnvironment,
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
)
|
|
|
|
.then((accountCheckResponse) => {
|
|
|
|
if (accountCheckResponse === true && canReceiveFiatTip !== true) {
|
|
|
|
setCanReceiveFiatTip(true);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
}, [canReceiveFiatTip, channelClaimId, tipChannelName]);
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
let regexp;
|
2021-04-23 21:59:48 +02:00
|
|
|
|
2021-07-06 22:28:29 +02:00
|
|
|
if (amount === 0) {
|
2021-10-28 22:25:34 +02:00
|
|
|
setTipError(__('Amount cannot be zero.'));
|
2021-07-06 22:28:29 +02:00
|
|
|
} else if (!amount || typeof amount !== 'number') {
|
2021-10-28 22:25:34 +02:00
|
|
|
setTipError(__('Amount must be a number.'));
|
2021-07-06 22:28:29 +02:00
|
|
|
} else {
|
2021-10-28 22:25:34 +02:00
|
|
|
// if it's not fiat, aka it's boost or lbc tip
|
|
|
|
if (activeTab !== TAB_FIAT) {
|
|
|
|
regexp = RegExp(/^(\d*([.]\d{0,8})?)$/);
|
|
|
|
const validTipInput = regexp.test(String(amount));
|
|
|
|
|
|
|
|
if (!validTipInput) {
|
|
|
|
setTipError(__('Amount must have no more than 8 decimal places'));
|
|
|
|
} else if (amount === balance) {
|
|
|
|
setTipError(__('Please decrease the amount to account for transaction fees'));
|
|
|
|
} else if (amount > balance || balance === 0) {
|
|
|
|
setTipError(__('Not enough Credits'));
|
|
|
|
} else if (amount < MINIMUM_PUBLISH_BID) {
|
|
|
|
setTipError(__('Amount must be higher'));
|
2021-11-05 20:31:51 +01:00
|
|
|
} else if (
|
|
|
|
convertedAmount &&
|
|
|
|
exchangeRate &&
|
|
|
|
customTipAmount &&
|
|
|
|
amount < convertToTwoDecimalsOrMore(customTipAmount / exchangeRate)
|
|
|
|
) {
|
|
|
|
regexp = RegExp(/^(\d*([.]\d{0,2})?)$/);
|
|
|
|
const validCustomTipInput = regexp.test(String(amount));
|
|
|
|
|
|
|
|
if (validCustomTipInput) {
|
|
|
|
setTipError(
|
|
|
|
__('Amount of $%input_amount% LBC in USB is lower than price of $%price_amount%', {
|
|
|
|
input_amount: convertToTwoDecimalsOrMore(convertedAmount, 4),
|
|
|
|
price_amount: convertToTwoDecimalsOrMore(customTipAmount),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
setTipError(__('Amount must have no more than 2 decimal places'));
|
|
|
|
}
|
2021-10-28 22:25:34 +02:00
|
|
|
} else {
|
|
|
|
setTipError(false);
|
|
|
|
}
|
|
|
|
// if tip fiat tab
|
|
|
|
} else {
|
|
|
|
regexp = RegExp(/^(\d*([.]\d{0,2})?)$/);
|
|
|
|
const validTipInput = regexp.test(String(amount));
|
|
|
|
|
|
|
|
if (!validTipInput) {
|
|
|
|
setTipError(__('Amount must have no more than 2 decimal places'));
|
|
|
|
} else if (amount < 1) {
|
|
|
|
setTipError(__('Amount must be at least one dollar'));
|
|
|
|
} else if (amount > 1000) {
|
|
|
|
setTipError(__('Amount cannot be over 1000 dollars'));
|
2021-11-05 20:31:51 +01:00
|
|
|
} else if (customTipAmount && amount < customTipAmount) {
|
|
|
|
setTipError(
|
|
|
|
__('Amount is lower than price of $%price_amount%', {
|
|
|
|
price_amount: convertToTwoDecimalsOrMore(customTipAmount),
|
|
|
|
})
|
|
|
|
);
|
2021-10-28 22:25:34 +02:00
|
|
|
} else {
|
|
|
|
setTipError(false);
|
|
|
|
}
|
2021-07-06 22:28:29 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-05 20:31:51 +01:00
|
|
|
}, [activeTab, amount, balance, convertedAmount, customTipAmount, exchangeRate, setTipError]);
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
const getHelpMessage = (helpMessage: any) => <div className="help">{helpMessage}</div>;
|
2021-04-23 21:59:48 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="section">
|
2021-11-05 20:31:51 +01:00
|
|
|
{tipAmountsToDisplay &&
|
|
|
|
tipAmountsToDisplay.map((defaultAmount) => (
|
|
|
|
<Button
|
|
|
|
key={defaultAmount}
|
|
|
|
disabled={shouldDisableAmountSelector(defaultAmount)}
|
|
|
|
button="alt"
|
|
|
|
className={classnames('button-toggle button-toggle--expandformobile', {
|
|
|
|
'button-toggle--active':
|
|
|
|
convertToTwoDecimalsOrMore(defaultAmount) === convertToTwoDecimalsOrMore(amount) && !useCustomTip,
|
|
|
|
'button-toggle--disabled': amount > balance,
|
|
|
|
})}
|
|
|
|
label={defaultAmount}
|
|
|
|
icon={activeTab === TAB_LBC ? ICONS.LBC : ICONS.FINANCE}
|
|
|
|
onClick={() => {
|
|
|
|
handleCustomPriceChange(defaultAmount);
|
|
|
|
setUseCustomTip(false);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
))}
|
2021-10-28 22:25:34 +02:00
|
|
|
|
2021-04-23 21:59:48 +02:00
|
|
|
<Button
|
|
|
|
button="alt"
|
2021-10-28 22:25:34 +02:00
|
|
|
disabled={shouldDisableFiatSelectors}
|
2021-04-23 21:59:48 +02:00
|
|
|
className={classnames('button-toggle button-toggle--expandformobile', {
|
2021-07-06 22:28:29 +02:00
|
|
|
'button-toggle--active': useCustomTip,
|
2021-04-23 21:59:48 +02:00
|
|
|
})}
|
2021-07-06 22:28:29 +02:00
|
|
|
icon={activeTab === TAB_LBC ? ICONS.LBC : ICONS.FINANCE}
|
2021-04-23 21:59:48 +02:00
|
|
|
label={__('Custom')}
|
|
|
|
onClick={() => setUseCustomTip(true)}
|
|
|
|
/>
|
2021-07-06 22:28:29 +02:00
|
|
|
{activeTab === TAB_LBC && DEFAULT_TIP_AMOUNTS.some((val) => val > balance) && (
|
2021-04-23 21:59:48 +02:00
|
|
|
<Button
|
|
|
|
button="secondary"
|
|
|
|
className="button-toggle-group-action"
|
|
|
|
icon={ICONS.BUY}
|
2021-05-31 05:19:13 +02:00
|
|
|
title={__('Buy or swap more LBRY Credits')}
|
2021-04-23 21:59:48 +02:00
|
|
|
navigate={`/$/${PAGES.BUY}`}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
2021-10-28 22:25:34 +02:00
|
|
|
{customTipAmount &&
|
|
|
|
fiatConversion &&
|
|
|
|
activeTab !== TAB_FIAT &&
|
|
|
|
getHelpMessage(
|
2021-11-05 20:31:51 +01:00
|
|
|
__('This support is priced in $USD.') +
|
|
|
|
(convertedAmount
|
|
|
|
? ' ' +
|
|
|
|
__('The current exchange rate for the submitted LBC amount is ~ $%exchange_amount%.', {
|
|
|
|
exchange_amount: convertToTwoDecimalsOrMore(convertedAmount),
|
|
|
|
})
|
|
|
|
: '')
|
2021-10-28 22:25:34 +02:00
|
|
|
)}
|
2021-07-06 22:28:29 +02:00
|
|
|
|
2021-07-22 18:44:30 +02:00
|
|
|
{/* custom number input form */}
|
2021-04-23 21:59:48 +02:00
|
|
|
{useCustomTip && (
|
2021-10-28 22:25:34 +02:00
|
|
|
<div className="walletTipSelector__input">
|
2021-04-23 21:59:48 +02:00
|
|
|
<FormField
|
2021-10-28 22:25:34 +02:00
|
|
|
autoFocus={!isMobile}
|
2021-04-23 21:59:48 +02:00
|
|
|
name="tip-input"
|
2021-10-28 22:25:34 +02:00
|
|
|
disabled={!customTipAmount && shouldDisableAmountSelector(0)}
|
2021-04-23 21:59:48 +02:00
|
|
|
error={tipError}
|
|
|
|
min="0"
|
|
|
|
step="any"
|
|
|
|
type="number"
|
|
|
|
placeholder="1.23"
|
|
|
|
value={amount}
|
|
|
|
onChange={(event) => handleCustomPriceChange(event.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
2021-07-06 22:28:29 +02:00
|
|
|
{/* lbc tab */}
|
|
|
|
{activeTab === TAB_LBC && <WalletSpendableBalanceHelp />}
|
2021-10-28 22:25:34 +02:00
|
|
|
{activeTab === TAB_FIAT &&
|
|
|
|
(!hasCardSaved
|
|
|
|
? getHelpMessage(
|
|
|
|
<>
|
|
|
|
<Button navigate={`/$/${PAGES.SETTINGS_STRIPE_CARD}`} label={__('Add a Card')} button="link" />
|
2021-11-05 20:31:51 +01:00
|
|
|
{' ' + __('To Tip Creators')}
|
2021-10-28 22:25:34 +02:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
: !canReceiveFiatTip
|
|
|
|
? getHelpMessage(__('Only creators that verify cash accounts can receive tips'))
|
|
|
|
: getHelpMessage(__('Send a tip directly from your attached card')))}
|
2021-04-23 21:59:48 +02:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default WalletTipAmountSelector;
|