import { useCallback } from 'react' import { Grid, Row, Col, Button } from 'react-bootstrap' import moment from 'moment' import { useTranslation, Trans } from 'react-i18next' import { SubscriptionChangePreview } from '../../../../../../types/subscription/subscription-change-preview' import getMeta from '@/utils/meta' import { formatCurrencyLocalized } from '@/shared/utils/currency' import useAsync from '@/shared/hooks/use-async' import { useLocation } from '@/shared/hooks/use-location' import { debugConsole } from '@/utils/debugging' import { postJSON } from '@/infrastructure/fetch-json' import Notification from '@/shared/components/notification' function PreviewSubscriptionChange() { const preview = getMeta('ol-subscriptionChangePreview') const { t } = useTranslation() const payNowTask = useAsync() const location = useLocation() const handlePayNowClick = useCallback(() => { payNowTask .runAsync(payNow(preview)) .then(() => { location.replace('/user/subscription/thank-you') }) .catch(debugConsole.error) }, [location, payNowTask, preview]) return (
{preview.change.type === 'add-on-purchase' ? (

{t('add_add_on_to_your_plan', { addOnName: preview.change.addOn.name, })}

) : preview.change.type === 'premium-subscription' ? (

{t('subscribe_to_plan', { planName: preview.change.plan.name })}

) : null} {payNowTask.isError && ( {t('generic_something_went_wrong')}. {t('try_again')}.{' '} {t('generic_if_problem_continues_contact_us')}. } /> )}

{t('payment_summary')}

{t('due_today')}: {formatCurrencyLocalized( preview.immediateCharge, preview.currency )}
{t('future_payments')}:
{preview.nextInvoice.plan.name} {formatCurrencyLocalized( preview.nextInvoice.plan.amount, preview.currency )} {preview.nextInvoice.addOns.map(addOn => ( {addOn.name} {addOn.quantity > 1 ? ` ×${addOn.quantity}` : ''} {formatCurrencyLocalized(addOn.amount, preview.currency)} ))} {preview.nextInvoice.tax.rate > 0 && ( {t('vat')} {preview.nextInvoice.tax.rate * 100}% {formatCurrencyLocalized( preview.nextInvoice.tax.amount, preview.currency )} )} {t('total_per_month')} {formatCurrencyLocalized( preview.nextInvoice.total, preview.currency )}
}} shouldUnescape tOptions={{ interpolation: { escapeValue: true } }} />{' '} }} shouldUnescape tOptions={{ interpolation: { escapeValue: true } }} />
) } async function payNow(preview: SubscriptionChangePreview) { if (preview.change.type === 'add-on-purchase') { await postJSON(`/user/subscription/addon/${preview.change.addOn.code}/add`) } else { throw new Error( `Unknown subscription change preview type: ${preview.change.type}` ) } } export default PreviewSubscriptionChange