overleaf/services/web/frontend/js/features/subscription/context/subscription-dashboard-context.tsx
Jessica Lawshe c7d8e4867d Merge pull request #11560 from overleaf/jel-react-personal-subscription-dash-pt-5
[web] Continue migration of personal subscription dash to React

GitOrigin-RevId: fb327592ad5031bff20b2a95a83edee4735112e8
2023-02-08 09:06:29 +00:00

104 lines
2.9 KiB
TypeScript

import {
createContext,
ReactNode,
useContext,
useEffect,
useMemo,
useState,
} from 'react'
import {
ManagedGroupSubscription,
Subscription,
} from '../../../../../types/subscription/dashboard/subscription'
import { Plan } from '../../../../../types/subscription/plan'
import { Institution } from '../../../../../types/institution'
import getMeta from '../../../utils/meta'
type SubscriptionDashboardContextValue = {
hasDisplayedSubscription: boolean
institutionMemberships?: Array<Institution>
managedGroupSubscriptions: Array<ManagedGroupSubscription>
personalSubscription?: Subscription
plans: Array<Plan>
recurlyLoadError: boolean
setRecurlyLoadError: React.Dispatch<React.SetStateAction<boolean>>
showCancellation: boolean
setShowCancellation: React.Dispatch<React.SetStateAction<boolean>>
showChangePersonalPlan: boolean
setShowChangePersonalPlan: React.Dispatch<React.SetStateAction<boolean>>
}
export const SubscriptionDashboardContext = createContext<
SubscriptionDashboardContextValue | undefined
>(undefined)
export function SubscriptionDashboardProvider({
children,
}: {
children: ReactNode
}) {
const [recurlyLoadError, setRecurlyLoadError] = useState(false)
const [showCancellation, setShowCancellation] = useState(false)
const [showChangePersonalPlan, setShowChangePersonalPlan] = useState(false)
const plans = getMeta('ol-plans')
const institutionMemberships = getMeta('ol-currentInstitutionsWithLicence')
const personalSubscription = getMeta('ol-subscription')
const managedGroupSubscriptions = getMeta('ol-managedGroupSubscriptions')
const hasDisplayedSubscription =
institutionMemberships?.length > 0 ||
personalSubscription ||
managedGroupSubscriptions
useEffect(() => {
if (typeof window.recurly === 'undefined' || !window.recurly) {
setRecurlyLoadError(true)
}
}, [setRecurlyLoadError])
const value = useMemo<SubscriptionDashboardContextValue>(
() => ({
hasDisplayedSubscription,
institutionMemberships,
managedGroupSubscriptions,
personalSubscription,
plans,
recurlyLoadError,
setRecurlyLoadError,
showCancellation,
setShowCancellation,
showChangePersonalPlan,
setShowChangePersonalPlan,
}),
[
hasDisplayedSubscription,
institutionMemberships,
managedGroupSubscriptions,
personalSubscription,
plans,
recurlyLoadError,
setRecurlyLoadError,
showCancellation,
setShowCancellation,
showChangePersonalPlan,
setShowChangePersonalPlan,
]
)
return (
<SubscriptionDashboardContext.Provider value={value}>
{children}
</SubscriptionDashboardContext.Provider>
)
}
export function useSubscriptionDashboardContext() {
const context = useContext(SubscriptionDashboardContext)
if (!context) {
throw new Error(
'SubscriptionDashboardContext is only available inside SubscriptionDashboardProvider'
)
}
return context
}