diff --git a/services/web/frontend/js/features/subscription/components/dashboard/states/active/change-plan/modals/change-to-group-modal.tsx b/services/web/frontend/js/features/subscription/components/dashboard/states/active/change-plan/modals/change-to-group-modal.tsx index 14bee2fb20..b9a18366fc 100644 --- a/services/web/frontend/js/features/subscription/components/dashboard/states/active/change-plan/modals/change-to-group-modal.tsx +++ b/services/web/frontend/js/features/subscription/components/dashboard/states/active/change-plan/modals/change-to-group-modal.tsx @@ -1,13 +1,16 @@ -import { useEffect } from 'react' +import { useEffect, useState } from 'react' import { Modal } from 'react-bootstrap' import { useTranslation, Trans } from 'react-i18next' import { GroupPlans } from '../../../../../../../../../../types/subscription/dashboard/group-plans' import { Subscription } from '../../../../../../../../../../types/subscription/dashboard/subscription' import { PriceForDisplayData } from '../../../../../../../../../../types/subscription/plan' +import { postJSON } from '../../../../../../../../infrastructure/fetch-json' import AccessibleModal from '../../../../../../../../shared/components/accessible-modal' import getMeta from '../../../../../../../../utils/meta' import { useSubscriptionDashboardContext } from '../../../../../../context/subscription-dashboard-context' import GenericErrorAlert from '../../../../generic-error-alert' +import { subscriptionUpdateUrl } from '../../../../../../data/subscription-url' +import { getRecurlyGroupPlanCode } from '../../../../../../util/recurly-group-plan-code' const educationalPercentDiscount = 40 const groupSizeForEducationalDiscount = 10 @@ -138,14 +141,34 @@ export function ChangeToGroupModal() { } = useSubscriptionDashboardContext() const groupPlans: GroupPlans = getMeta('ol-groupPlans') const personalSubscription: Subscription = getMeta('ol-subscription') + const [error, setError] = useState(false) + const [inflight, setInflight] = useState(false) + + async function upgrade() { + setError(false) + setInflight(true) + + try { + await postJSON(subscriptionUpdateUrl, { + body: { + plan_code: getRecurlyGroupPlanCode( + groupPlanToChangeToCode, + groupPlanToChangeToSize, + groupPlanToChangeToUsage + ), + }, + }) + window.location.reload() + } catch (e) { + setError(true) + setInflight(false) + } + } useEffect(() => { - const defaultPlanOption = personalSubscription.plan.planCode.includes( - 'professional' - ) - ? 'professional' - : 'collaborator' - setGroupPlanToChangeToCode(defaultPlanOption) + if (personalSubscription.plan.planCode.includes('professional')) { + setGroupPlanToChangeToCode('professional') + } }, [personalSubscription, setGroupPlanToChangeToCode]) function handleGetInTouchButton() { @@ -331,13 +354,22 @@ export function ChangeToGroupModal() { {t('new_subscription_will_be_billed_immediately')}


+ {error && ( +
+ {t('generic_something_went_wrong')}. {t('try_again')}.{' '} + {t('generic_if_problem_continues_contact_us')}. +
+ )}