2023-02-07 10:38:12 -05:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
ReactNode,
|
|
|
|
useContext,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
2023-02-07 10:38:04 -05:00
|
|
|
import {
|
|
|
|
ManagedGroupSubscription,
|
|
|
|
Subscription,
|
|
|
|
} from '../../../../../types/subscription/dashboard/subscription'
|
|
|
|
import { Plan } from '../../../../../types/subscription/plan'
|
|
|
|
import { Institution } from '../../../../../types/institution'
|
|
|
|
import getMeta from '../../../utils/meta'
|
2023-02-07 10:38:30 -05:00
|
|
|
import { loadDisplayPriceWithTaxPromise } from '../util/recurly-pricing'
|
|
|
|
import { isRecurlyLoaded } from '../util/is-recurly-loaded'
|
2023-01-26 11:48:30 -05:00
|
|
|
|
|
|
|
type SubscriptionDashboardContextValue = {
|
2023-02-07 10:38:04 -05:00
|
|
|
hasDisplayedSubscription: boolean
|
|
|
|
institutionMemberships?: Array<Institution>
|
|
|
|
managedGroupSubscriptions: Array<ManagedGroupSubscription>
|
|
|
|
personalSubscription?: Subscription
|
|
|
|
plans: Array<Plan>
|
2023-02-07 10:38:30 -05:00
|
|
|
queryingIndividualPlansData: boolean
|
2023-01-26 12:01:54 -05:00
|
|
|
recurlyLoadError: boolean
|
|
|
|
setRecurlyLoadError: React.Dispatch<React.SetStateAction<boolean>>
|
|
|
|
showCancellation: boolean
|
|
|
|
setShowCancellation: React.Dispatch<React.SetStateAction<boolean>>
|
2023-01-26 11:48:30 -05:00
|
|
|
showChangePersonalPlan: boolean
|
|
|
|
setShowChangePersonalPlan: React.Dispatch<React.SetStateAction<boolean>>
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SubscriptionDashboardContext = createContext<
|
|
|
|
SubscriptionDashboardContextValue | undefined
|
|
|
|
>(undefined)
|
|
|
|
|
|
|
|
export function SubscriptionDashboardProvider({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: ReactNode
|
|
|
|
}) {
|
2023-01-26 12:01:54 -05:00
|
|
|
const [recurlyLoadError, setRecurlyLoadError] = useState(false)
|
|
|
|
const [showCancellation, setShowCancellation] = useState(false)
|
2023-01-26 11:48:30 -05:00
|
|
|
const [showChangePersonalPlan, setShowChangePersonalPlan] = useState(false)
|
2023-02-07 10:38:30 -05:00
|
|
|
const [plans, setPlans] = useState([])
|
|
|
|
const [queryingIndividualPlansData, setQueryingIndividualPlansData] =
|
|
|
|
useState(true)
|
2023-01-26 11:48:30 -05:00
|
|
|
|
2023-02-07 10:38:30 -05:00
|
|
|
const plansWithoutDisplayPrice = getMeta('ol-plans')
|
2023-02-07 10:38:04 -05:00
|
|
|
const institutionMemberships = getMeta('ol-currentInstitutionsWithLicence')
|
|
|
|
const personalSubscription = getMeta('ol-subscription')
|
|
|
|
const managedGroupSubscriptions = getMeta('ol-managedGroupSubscriptions')
|
2023-02-07 10:38:30 -05:00
|
|
|
const recurlyApiKey = getMeta('ol-recurlyApiKey')
|
2023-02-07 10:38:04 -05:00
|
|
|
|
|
|
|
const hasDisplayedSubscription =
|
|
|
|
institutionMemberships?.length > 0 ||
|
|
|
|
personalSubscription ||
|
2023-02-07 11:14:23 -05:00
|
|
|
managedGroupSubscriptions?.length > 0
|
2023-02-07 10:38:04 -05:00
|
|
|
|
2023-02-07 10:38:12 -05:00
|
|
|
useEffect(() => {
|
2023-02-07 10:38:30 -05:00
|
|
|
if (!isRecurlyLoaded()) {
|
2023-02-07 10:38:12 -05:00
|
|
|
setRecurlyLoadError(true)
|
2023-02-07 11:14:23 -05:00
|
|
|
} else if (recurlyApiKey) {
|
2023-02-07 10:38:30 -05:00
|
|
|
recurly.configure(recurlyApiKey)
|
2023-02-07 10:38:12 -05:00
|
|
|
}
|
2023-02-07 10:38:30 -05:00
|
|
|
}, [recurlyApiKey, setRecurlyLoadError])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isRecurlyLoaded() && plansWithoutDisplayPrice && personalSubscription) {
|
|
|
|
const { currency, taxRate } = personalSubscription.recurly
|
|
|
|
const fetchPlansDisplayPrices = async () => {
|
|
|
|
for (const plan of plansWithoutDisplayPrice) {
|
|
|
|
try {
|
|
|
|
const priceData = await loadDisplayPriceWithTaxPromise(
|
|
|
|
plan.planCode,
|
|
|
|
currency,
|
|
|
|
taxRate
|
|
|
|
)
|
|
|
|
if (priceData?.totalForDisplay) {
|
|
|
|
plan.displayPrice = priceData.totalForDisplay
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setPlans(plansWithoutDisplayPrice)
|
|
|
|
setQueryingIndividualPlansData(false)
|
|
|
|
}
|
|
|
|
fetchPlansDisplayPrices().catch(console.error)
|
|
|
|
}
|
|
|
|
}, [personalSubscription, plansWithoutDisplayPrice])
|
2023-02-07 10:38:12 -05:00
|
|
|
|
2023-01-26 11:48:30 -05:00
|
|
|
const value = useMemo<SubscriptionDashboardContextValue>(
|
|
|
|
() => ({
|
2023-02-07 10:38:04 -05:00
|
|
|
hasDisplayedSubscription,
|
|
|
|
institutionMemberships,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
personalSubscription,
|
|
|
|
plans,
|
2023-02-07 10:38:30 -05:00
|
|
|
queryingIndividualPlansData,
|
2023-01-26 12:01:54 -05:00
|
|
|
recurlyLoadError,
|
|
|
|
setRecurlyLoadError,
|
|
|
|
showCancellation,
|
|
|
|
setShowCancellation,
|
2023-01-26 11:48:30 -05:00
|
|
|
showChangePersonalPlan,
|
|
|
|
setShowChangePersonalPlan,
|
|
|
|
}),
|
2023-01-26 12:01:54 -05:00
|
|
|
[
|
2023-02-07 10:38:04 -05:00
|
|
|
hasDisplayedSubscription,
|
|
|
|
institutionMemberships,
|
|
|
|
managedGroupSubscriptions,
|
|
|
|
personalSubscription,
|
|
|
|
plans,
|
2023-02-07 10:38:30 -05:00
|
|
|
queryingIndividualPlansData,
|
2023-01-26 12:01:54 -05:00
|
|
|
recurlyLoadError,
|
|
|
|
setRecurlyLoadError,
|
|
|
|
showCancellation,
|
|
|
|
setShowCancellation,
|
|
|
|
showChangePersonalPlan,
|
|
|
|
setShowChangePersonalPlan,
|
|
|
|
]
|
2023-01-26 11:48:30 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|