2023-01-26 11:48:30 -05:00
|
|
|
import { createContext, ReactNode, useContext, useMemo, useState } from 'react'
|
|
|
|
|
|
|
|
type SubscriptionDashboardContextValue = {
|
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)
|
|
|
|
|
|
|
|
const value = useMemo<SubscriptionDashboardContextValue>(
|
|
|
|
() => ({
|
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
|
|
|
[
|
|
|
|
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
|
|
|
|
}
|