2022-06-24 02:58:32 +02:00
|
|
|
// @flow
|
|
|
|
import { Form } from 'component/common/form';
|
|
|
|
import { Lbryio } from 'lbryinc';
|
|
|
|
import * as PAGES from 'constants/pages';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import Card from 'component/common/card';
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { getStripeEnvironment } from 'util/stripe';
|
|
|
|
const stripeEnvironment = getStripeEnvironment();
|
|
|
|
|
|
|
|
type TipParams = { tipAmount: number, tipChannelName: string, channelClaimId: string };
|
|
|
|
type UserParams = { activeChannelName: ?string, activeChannelId: ?string };
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
activeChannelId?: string,
|
|
|
|
activeChannelName?: string,
|
|
|
|
claimId: string,
|
|
|
|
claimType?: string,
|
|
|
|
channelClaimId?: string,
|
|
|
|
tipChannelName?: string,
|
|
|
|
claimIsMine: boolean,
|
|
|
|
isSupport: boolean,
|
|
|
|
isTipOnly?: boolean,
|
|
|
|
customText?: string,
|
|
|
|
doHideModal: () => void,
|
|
|
|
setAmount?: (number) => void,
|
|
|
|
preferredCurrency: string,
|
|
|
|
preOrderPurchase: (
|
|
|
|
TipParams,
|
|
|
|
anonymous: boolean,
|
|
|
|
UserParams,
|
|
|
|
claimId: string,
|
|
|
|
stripe: ?string,
|
|
|
|
preferredCurrency: string,
|
|
|
|
?(any) => Promise<void>,
|
|
|
|
?(any) => void
|
|
|
|
) => void,
|
|
|
|
preorderTag: string,
|
|
|
|
checkIfAlreadyPurchased: () => void,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function PreorderContent(props: Props) {
|
|
|
|
const {
|
|
|
|
activeChannelId,
|
|
|
|
activeChannelName,
|
|
|
|
claimId,
|
|
|
|
channelClaimId,
|
|
|
|
tipChannelName,
|
|
|
|
doHideModal,
|
|
|
|
preOrderPurchase,
|
|
|
|
preferredCurrency,
|
|
|
|
preorderTag,
|
|
|
|
checkIfAlreadyPurchased,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
// set the purchase amount once the preorder tag is selected
|
|
|
|
React.useEffect(() => {
|
|
|
|
setTipAmount(Number(preorderTag));
|
|
|
|
}, [preorderTag]);
|
|
|
|
|
|
|
|
const [tipAmount, setTipAmount] = React.useState(0);
|
|
|
|
const [waitingForBackend, setWaitingForBackend] = React.useState(false);
|
|
|
|
const [hasCardSaved, setHasSavedCard] = React.useState(true);
|
|
|
|
|
|
|
|
// check if user has a payment method saved
|
|
|
|
React.useEffect(() => {
|
|
|
|
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]);
|
|
|
|
|
2022-06-24 15:08:46 +02:00
|
|
|
const modalHeaderText = __('Preorder Your Content');
|
2022-06-28 07:25:46 +02:00
|
|
|
const subtitleText = __(
|
|
|
|
'This content is not available yet but you can pre-order it now so you can access it as soon as it goes live.'
|
2022-06-24 15:08:46 +02:00
|
|
|
);
|
2022-06-24 02:58:32 +02:00
|
|
|
|
|
|
|
function handleSubmit() {
|
|
|
|
const tipParams: TipParams = {
|
|
|
|
tipAmount,
|
|
|
|
tipChannelName: tipChannelName || '',
|
|
|
|
channelClaimId: channelClaimId || '',
|
|
|
|
};
|
|
|
|
const userParams: UserParams = { activeChannelName, activeChannelId };
|
|
|
|
|
|
|
|
async function checkIfFinished() {
|
|
|
|
await checkIfAlreadyPurchased();
|
|
|
|
doHideModal();
|
|
|
|
}
|
|
|
|
|
|
|
|
setWaitingForBackend(true);
|
|
|
|
|
|
|
|
// hit backend to send tip
|
|
|
|
preOrderPurchase(
|
|
|
|
tipParams,
|
|
|
|
!activeChannelId,
|
|
|
|
userParams,
|
|
|
|
claimId,
|
|
|
|
stripeEnvironment,
|
|
|
|
preferredCurrency,
|
|
|
|
checkIfFinished,
|
|
|
|
doHideModal
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-28 07:25:46 +02:00
|
|
|
const fiatSymbolToUse = preferredCurrency === 'EUR' ? '€' : '$';
|
2022-06-24 02:58:32 +02:00
|
|
|
|
|
|
|
function buildButtonText() {
|
2022-06-28 07:25:46 +02:00
|
|
|
return __('Preorder your content for %tip_currency%%tip_amount%', {
|
|
|
|
tip_currency: fiatSymbolToUse,
|
|
|
|
tip_amount: tipAmount.toString(),
|
2022-06-24 15:08:46 +02:00
|
|
|
});
|
2022-06-24 02:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Form onSubmit={handleSubmit}>
|
|
|
|
{!waitingForBackend && (
|
|
|
|
<Card
|
2022-06-24 15:08:46 +02:00
|
|
|
title={modalHeaderText}
|
2022-06-24 02:58:32 +02:00
|
|
|
className={'preorder-content-modal'}
|
2022-06-24 15:08:46 +02:00
|
|
|
subtitle={<div className="section__subtitle">{subtitleText}</div>}
|
2022-06-24 02:58:32 +02:00
|
|
|
actions={
|
2022-06-24 15:08:46 +02:00
|
|
|
// confirm purchase functionality
|
2022-06-24 02:58:32 +02:00
|
|
|
<>
|
|
|
|
<div className="handle-submit-area">
|
|
|
|
<Button
|
|
|
|
autoFocus
|
|
|
|
onClick={handleSubmit}
|
|
|
|
button="primary"
|
|
|
|
label={buildButtonText()}
|
|
|
|
disabled={!hasCardSaved}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{!hasCardSaved && (
|
2022-06-24 15:08:46 +02:00
|
|
|
<div className="add-card-prompt">
|
2022-06-28 07:25:46 +02:00
|
|
|
{/* FIX_THIS: no split strings please. Use <i18Message> */}
|
2022-06-24 15:08:46 +02:00
|
|
|
<Button navigate={`/$/${PAGES.SETTINGS_STRIPE_CARD}`} label={__('Add a Card')} button="link" />
|
|
|
|
{' ' + __('To Preorder Content')}
|
|
|
|
</div>
|
2022-06-24 02:58:32 +02:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{/* processing payment card */}
|
|
|
|
{waitingForBackend && (
|
|
|
|
<Card
|
2022-06-24 15:08:46 +02:00
|
|
|
title={modalHeaderText}
|
2022-06-24 02:58:32 +02:00
|
|
|
className={'preorder-content-modal-loading'}
|
2022-06-24 15:08:46 +02:00
|
|
|
subtitle={<div className="section__subtitle">{__('Processing your purchase...')}</div>}
|
2022-06-24 02:58:32 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|