2018-09-26 19:48:07 +02:00
|
|
|
// @flow
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
2020-05-21 17:38:28 +02:00
|
|
|
import classnames from 'classnames';
|
2017-12-21 22:08:54 +01:00
|
|
|
import FilePrice from 'component/filePrice';
|
|
|
|
import { Modal } from 'modal/modal';
|
2020-05-11 17:54:39 +02:00
|
|
|
import Card from 'component/common/card';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
|
|
|
import Button from 'component/button';
|
2017-09-07 23:18:33 +02:00
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
// This number is tied to transitions in scss/purchase.scss
|
|
|
|
const ANIMATION_LENGTH = 2500;
|
|
|
|
|
2018-09-26 19:48:07 +02:00
|
|
|
type Props = {
|
|
|
|
closeModal: () => void,
|
2020-05-21 17:38:28 +02:00
|
|
|
loadVideo: (string, (GetResponse) => void) => void,
|
2018-09-26 19:48:07 +02:00
|
|
|
uri: string,
|
|
|
|
cancelPurchase: () => void,
|
2019-04-24 16:02:08 +02:00
|
|
|
metadata: StreamMetadata,
|
2020-05-21 17:38:28 +02:00
|
|
|
analyticsPurchaseEvent: GetResponse => void,
|
2018-09-26 19:48:07 +02:00
|
|
|
};
|
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
function ModalAffirmPurchase(props: Props) {
|
|
|
|
const {
|
|
|
|
cancelPurchase,
|
|
|
|
closeModal,
|
|
|
|
loadVideo,
|
|
|
|
metadata: { title },
|
|
|
|
uri,
|
|
|
|
analyticsPurchaseEvent,
|
|
|
|
} = props;
|
|
|
|
const [success, setSuccess] = React.useState(false);
|
|
|
|
const [purchasing, setPurchasing] = React.useState(false);
|
2018-05-31 06:32:31 +02:00
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
const modalTitle = __('Confirm Purchase');
|
2018-05-31 06:32:31 +02:00
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
function onAffirmPurchase() {
|
|
|
|
setPurchasing(true);
|
|
|
|
loadVideo(uri, fileInfo => {
|
|
|
|
setPurchasing(false);
|
|
|
|
setSuccess(true);
|
|
|
|
analyticsPurchaseEvent(fileInfo);
|
|
|
|
});
|
2017-09-08 05:15:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
let timeout;
|
|
|
|
if (success) {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
closeModal();
|
|
|
|
setSuccess(false);
|
|
|
|
}, ANIMATION_LENGTH);
|
|
|
|
}
|
2017-09-07 23:18:33 +02:00
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
return () => {
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [success, uri]);
|
2020-05-11 17:54:39 +02:00
|
|
|
|
2020-05-21 17:38:28 +02:00
|
|
|
return (
|
|
|
|
<Modal type="card" isOpen contentLabel={modalTitle} onAborted={cancelPurchase}>
|
|
|
|
<Card
|
|
|
|
title={modalTitle}
|
|
|
|
subtitle={
|
|
|
|
<div className={classnames('purchase-stuff', { 'purchase-stuff--purchased': success })}>
|
|
|
|
<div>
|
|
|
|
{success && (
|
|
|
|
<div className="purchase-stuff__text--purchased">
|
|
|
|
{__('Purchased!')}
|
|
|
|
<div className="purchase_stuff__subtext--purchased">
|
|
|
|
{__('This content will now be in your Library.')}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{/* Keep this message rendered but hidden so the width doesn't change */}
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
|
|
|
claim_title: <strong>{title ? `"${title}"` : uri}</strong>,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Are you sure you want to purchase %claim_title%?
|
|
|
|
</I18nMessage>
|
2020-05-11 17:54:39 +02:00
|
|
|
</div>
|
2020-05-21 17:38:28 +02:00
|
|
|
<div>
|
|
|
|
<FilePrice uri={uri} showFullPrice type="modal" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<div className="section__actions" style={success ? { visibility: 'hidden' } : undefined}>
|
|
|
|
<Button
|
|
|
|
button="primary"
|
|
|
|
label={purchasing ? __('Purchasing') : __('Purchase')}
|
|
|
|
onClick={onAffirmPurchase}
|
|
|
|
/>
|
|
|
|
<Button button="link" label={__('Cancel')} onClick={cancelPurchase} />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
);
|
2017-09-07 23:18:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ModalAffirmPurchase;
|