diff --git a/services/web/frontend/js/features/subscription/components/preview-subscription-change/root.tsx b/services/web/frontend/js/features/subscription/components/preview-subscription-change/root.tsx index 157cc735f9..09a5824f96 100644 --- a/services/web/frontend/js/features/subscription/components/preview-subscription-change/root.tsx +++ b/services/web/frontend/js/features/subscription/components/preview-subscription-change/root.tsx @@ -1,12 +1,31 @@ +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 { t } = useTranslation() 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 ( @@ -19,6 +38,20 @@ function PreviewSubscriptionChange() { })} )} + + {payNowTask.isError && ( + + {t('generic_something_went_wrong')}. {t('try_again')}.{' '} + {t('generic_if_problem_continues_contact_us')}. + + } + /> + )} +

{t('payment_summary')}

@@ -106,7 +139,12 @@ function PreviewSubscriptionChange() {
-
@@ -117,4 +155,13 @@ function PreviewSubscriptionChange() { ) } +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