2023-02-07 10:38:12 -05:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
ReactNode,
|
2023-02-14 05:21:25 -05:00
|
|
|
useCallback,
|
2023-02-07 10:38:12 -05:00
|
|
|
useContext,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
2023-02-07 10:38:04 -05:00
|
|
|
import {
|
2023-02-22 07:33:17 -05:00
|
|
|
CustomSubscription,
|
2023-02-07 10:38:04 -05:00
|
|
|
ManagedGroupSubscription,
|
2023-02-22 07:10:02 -05:00
|
|
|
MemberGroupSubscription,
|
2023-02-22 07:33:17 -05:00
|
|
|
RecurlySubscription,
|
2023-02-07 10:38:04 -05:00
|
|
|
} from '../../../../../types/subscription/dashboard/subscription'
|
2023-02-21 10:24:49 -05:00
|
|
|
import {
|
|
|
|
Plan,
|
|
|
|
PriceForDisplayData,
|
|
|
|
} from '../../../../../types/subscription/plan'
|
2023-02-07 10:38:04 -05:00
|
|
|
import { Institution } from '../../../../../types/institution'
|
2023-02-22 06:50:24 -05:00
|
|
|
import { Institution as ManagedInstitution } from '../components/dashboard/managed-institutions'
|
|
|
|
import { Publisher as ManagedPublisher } from '../components/dashboard/managed-publishers'
|
2023-02-07 10:38:04 -05:00
|
|
|
import getMeta from '../../../utils/meta'
|
2023-02-21 10:24:49 -05:00
|
|
|
import {
|
|
|
|
loadDisplayPriceWithTaxPromise,
|
|
|
|
loadGroupDisplayPriceWithTaxPromise,
|
|
|
|
} from '../util/recurly-pricing'
|
2023-02-07 10:38:30 -05:00
|
|
|
import { isRecurlyLoaded } from '../util/is-recurly-loaded'
|
2023-02-21 10:24:28 -05:00
|
|
|
import { SubscriptionDashModalIds } from '../../../../../types/subscription/dashboard/modal-ids'
|
2023-01-26 11:48:30 -05:00
|
|
|
|
|
|
|
type SubscriptionDashboardContextValue = {
|
2023-02-21 10:24:57 -05:00
|
|
|
groupPlanToChangeToCode: string
|
2023-02-21 10:24:28 -05:00
|
|
|
groupPlanToChangeToSize: string
|
2023-02-21 10:24:57 -05:00
|
|
|
groupPlanToChangeToUsage: string
|
2023-02-21 10:24:49 -05:00
|
|
|
groupPlanToChangeToPrice?: PriceForDisplayData
|
|
|
|
groupPlanToChangeToPriceError?: boolean
|
2023-02-16 11:29:49 -05:00
|
|
|
handleCloseModal: () => void
|
2023-02-21 10:24:28 -05:00
|
|
|
handleOpenModal: (
|
|
|
|
modalIdToOpen: SubscriptionDashModalIds,
|
|
|
|
planCode?: string
|
|
|
|
) => void
|
2023-02-07 10:38:04 -05:00
|
|
|
hasDisplayedSubscription: boolean
|
2023-08-10 05:44:01 -04:00
|
|
|
hasValidActiveSubscription: boolean
|
2023-02-14 05:21:25 -05:00
|
|
|
institutionMemberships?: Institution[]
|
|
|
|
managedGroupSubscriptions: ManagedGroupSubscription[]
|
2023-02-22 07:10:02 -05:00
|
|
|
memberGroupSubscriptions: MemberGroupSubscription[]
|
2023-02-14 05:21:25 -05:00
|
|
|
managedInstitutions: ManagedInstitution[]
|
2023-02-22 06:50:24 -05:00
|
|
|
managedPublishers: ManagedPublisher[]
|
2023-02-14 05:21:25 -05:00
|
|
|
updateManagedInstitution: (institution: ManagedInstitution) => void
|
2023-02-21 10:24:28 -05:00
|
|
|
modalIdShown?: SubscriptionDashModalIds
|
2023-02-22 07:33:17 -05:00
|
|
|
personalSubscription?: RecurlySubscription | CustomSubscription
|
2023-02-22 07:18:01 -05:00
|
|
|
hasSubscription: boolean
|
2023-02-14 05:21:25 -05:00
|
|
|
plans: Plan[]
|
2023-02-16 11:29:49 -05:00
|
|
|
planCodeToChangeTo?: string
|
2023-02-21 10:24:49 -05:00
|
|
|
queryingGroupPlanToChangeToPrice: boolean
|
2023-02-07 10:38:30 -05:00
|
|
|
queryingIndividualPlansData: boolean
|
2023-01-26 12:01:54 -05:00
|
|
|
recurlyLoadError: boolean
|
2023-02-21 10:24:57 -05:00
|
|
|
setGroupPlanToChangeToCode: React.Dispatch<React.SetStateAction<string>>
|
2023-02-21 10:24:28 -05:00
|
|
|
setGroupPlanToChangeToSize: React.Dispatch<React.SetStateAction<string>>
|
|
|
|
setGroupPlanToChangeToUsage: React.Dispatch<React.SetStateAction<string>>
|
|
|
|
setModalIdShown: React.Dispatch<
|
|
|
|
React.SetStateAction<SubscriptionDashModalIds | undefined>
|
|
|
|
>
|
2023-02-16 11:29:49 -05:00
|
|
|
setPlanCodeToChangeTo: React.Dispatch<
|
|
|
|
React.SetStateAction<string | undefined>
|
|
|
|
>
|
2023-01-26 12:01:54 -05:00
|
|
|
setRecurlyLoadError: React.Dispatch<React.SetStateAction<boolean>>
|
|
|
|
showCancellation: boolean
|
|
|
|
setShowCancellation: React.Dispatch<React.SetStateAction<boolean>>
|
2023-02-22 07:10:02 -05:00
|
|
|
leavingGroupId?: string
|
|
|
|
setLeavingGroupId: React.Dispatch<React.SetStateAction<string | undefined>>
|
2023-01-26 11:48:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export const SubscriptionDashboardContext = createContext<
|
|
|
|
SubscriptionDashboardContextValue | undefined
|
|
|
|
>(undefined)
|
|
|
|
|
|
|
|
export function SubscriptionDashboardProvider({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: ReactNode
|
|
|
|
}) {
|
2023-02-21 10:24:28 -05:00
|
|
|
const [modalIdShown, setModalIdShown] = useState<
|
|
|
|
SubscriptionDashModalIds | undefined
|
|
|
|
>()
|
2023-01-26 12:01:54 -05:00
|
|
|
const [recurlyLoadError, setRecurlyLoadError] = useState(false)
|
|
|
|
const [showCancellation, setShowCancellation] = useState(false)
|
2023-02-07 10:38:30 -05:00
|
|
|
const [plans, setPlans] = useState([])
|
|
|
|
const [queryingIndividualPlansData, setQueryingIndividualPlansData] =
|
|
|
|
useState(true)
|
2023-02-16 11:29:49 -05:00
|
|
|
const [planCodeToChangeTo, setPlanCodeToChangeTo] = useState<
|
|
|
|
string | undefined
|
|
|
|
>()
|
2023-02-21 10:24:28 -05:00
|
|
|
const [groupPlanToChangeToSize, setGroupPlanToChangeToSize] = useState('10')
|
2023-02-21 10:24:57 -05:00
|
|
|
const [groupPlanToChangeToCode, setGroupPlanToChangeToCode] =
|
|
|
|
useState('collaborator')
|
2023-02-21 10:24:28 -05:00
|
|
|
const [groupPlanToChangeToUsage, setGroupPlanToChangeToUsage] =
|
|
|
|
useState('enterprise')
|
2023-02-21 10:24:49 -05:00
|
|
|
const [
|
|
|
|
queryingGroupPlanToChangeToPrice,
|
|
|
|
setQueryingGroupPlanToChangeToPrice,
|
|
|
|
] = useState(false)
|
|
|
|
const [groupPlanToChangeToPrice, setGroupPlanToChangeToPrice] =
|
|
|
|
useState<PriceForDisplayData>()
|
|
|
|
const [groupPlanToChangeToPriceError, setGroupPlanToChangeToPriceError] =
|
|
|
|
useState(false)
|
2023-02-22 07:10:02 -05:00
|
|
|
const [leavingGroupId, setLeavingGroupId] = useState<string | undefined>()
|
2023-01-26 11:48:30 -05:00
|
|
|
|
2023-02-07 10:38:30 -05:00
|
|
|
const plansWithoutDisplayPrice = getMeta('ol-plans')
|
2023-02-22 07:10:02 -05:00
|
|
|
const institutionMemberships: Institution[] = getMeta(
|
|
|
|
'ol-currentInstitutionsWithLicence'
|
|
|
|
)
|
2023-02-22 07:33:17 -05:00
|
|
|
const personalSubscription = getMeta('ol-subscription')
|
2023-02-22 07:10:02 -05:00
|
|
|
const managedGroupSubscriptions: ManagedGroupSubscription[] = getMeta(
|
|
|
|
'ol-managedGroupSubscriptions'
|
|
|
|
)
|
|
|
|
const memberGroupSubscriptions: MemberGroupSubscription[] = getMeta(
|
|
|
|
'ol-memberGroupSubscriptions'
|
|
|
|
)
|
2023-02-14 05:21:25 -05:00
|
|
|
const [managedInstitutions, setManagedInstitutions] = useState<
|
|
|
|
ManagedInstitution[]
|
|
|
|
>(getMeta('ol-managedInstitutions'))
|
2023-02-22 06:50:24 -05:00
|
|
|
const managedPublishers = getMeta('ol-managedPublishers')
|
2023-02-22 07:18:01 -05:00
|
|
|
const hasSubscription = getMeta('ol-hasSubscription')
|
2023-02-07 10:38:30 -05:00
|
|
|
const recurlyApiKey = getMeta('ol-recurlyApiKey')
|
2023-02-07 10:38:04 -05:00
|
|
|
|
2023-02-22 07:10:02 -05:00
|
|
|
const hasDisplayedSubscription = Boolean(
|
2023-02-07 10:38:04 -05:00
|
|
|
institutionMemberships?.length > 0 ||
|
2023-02-22 07:10:02 -05:00
|
|
|
personalSubscription ||
|
|
|
|
memberGroupSubscriptions?.length > 0 ||
|
|
|
|
managedGroupSubscriptions?.length > 0 ||
|
|
|
|
managedInstitutions?.length > 0 ||
|
|
|
|
managedPublishers?.length > 0
|
|
|
|
)
|
2023-02-07 10:38:04 -05:00
|
|
|
|
2023-08-10 05:44:01 -04:00
|
|
|
const hasValidActiveSubscription = Boolean(
|
|
|
|
['active', 'canceled'].includes(personalSubscription?.recurly?.state) ||
|
|
|
|
institutionMemberships?.length > 0 ||
|
|
|
|
memberGroupSubscriptions?.length > 0
|
|
|
|
)
|
|
|
|
|
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(() => {
|
2023-02-22 07:33:17 -05:00
|
|
|
if (
|
|
|
|
isRecurlyLoaded() &&
|
|
|
|
plansWithoutDisplayPrice &&
|
|
|
|
personalSubscription?.recurly
|
|
|
|
) {
|
2023-02-07 10:38:30 -05:00
|
|
|
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-02-21 10:24:49 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (
|
|
|
|
isRecurlyLoaded() &&
|
|
|
|
groupPlanToChangeToCode &&
|
|
|
|
groupPlanToChangeToSize &&
|
|
|
|
groupPlanToChangeToUsage &&
|
2023-02-22 07:33:17 -05:00
|
|
|
personalSubscription?.recurly
|
2023-02-21 10:24:49 -05:00
|
|
|
) {
|
|
|
|
setQueryingGroupPlanToChangeToPrice(true)
|
|
|
|
|
|
|
|
const { currency, taxRate } = personalSubscription.recurly
|
|
|
|
const fetchGroupDisplayPrice = async () => {
|
|
|
|
setGroupPlanToChangeToPriceError(false)
|
|
|
|
let priceData
|
|
|
|
try {
|
|
|
|
priceData = await loadGroupDisplayPriceWithTaxPromise(
|
|
|
|
groupPlanToChangeToCode,
|
|
|
|
currency,
|
|
|
|
taxRate,
|
|
|
|
groupPlanToChangeToSize,
|
|
|
|
groupPlanToChangeToUsage
|
|
|
|
)
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
setGroupPlanToChangeToPriceError(true)
|
|
|
|
}
|
|
|
|
setQueryingGroupPlanToChangeToPrice(false)
|
|
|
|
setGroupPlanToChangeToPrice(priceData)
|
|
|
|
}
|
|
|
|
fetchGroupDisplayPrice()
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
groupPlanToChangeToUsage,
|
|
|
|
groupPlanToChangeToSize,
|
|
|
|
personalSubscription,
|
|
|
|
groupPlanToChangeToCode,
|
|
|
|
])
|
|
|
|
|
2023-02-14 05:21:25 -05:00
|
|
|
const updateManagedInstitution = useCallback(
|
|
|
|
(institution: ManagedInstitution) => {
|
|
|
|
setManagedInstitutions(institutions => {
|
|
|
|
return [
|
|
|
|
...(institutions || []).map(i =>
|
|
|
|
i.v1Id === institution.v1Id ? institution : i
|
|
|
|
),
|
|
|
|
]
|
|
|
|
})
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
)
|
2023-02-21 10:24:49 -05:00
|
|
|
|
2023-02-16 11:29:49 -05:00
|
|
|
const handleCloseModal = useCallback(() => {
|
2023-02-21 10:24:28 -05:00
|
|
|
setModalIdShown(undefined)
|
2023-02-16 11:29:49 -05:00
|
|
|
setPlanCodeToChangeTo(undefined)
|
|
|
|
}, [setModalIdShown, setPlanCodeToChangeTo])
|
|
|
|
|
|
|
|
const handleOpenModal = useCallback(
|
|
|
|
(id, planCode) => {
|
|
|
|
setModalIdShown(id)
|
|
|
|
setPlanCodeToChangeTo(planCode)
|
|
|
|
},
|
|
|
|
[setModalIdShown, setPlanCodeToChangeTo]
|
|
|
|
)
|
2023-02-14 05:21:25 -05:00
|
|
|
|
2023-01-26 11:48:30 -05:00
|
|
|
const value = useMemo<SubscriptionDashboardContextValue>(
|
|
|
|
() => ({
|
2023-02-21 10:24:28 -05:00
|
|
|
groupPlanToChangeToCode,
|
2023-02-21 10:24:49 -05:00
|
|
|
groupPlanToChangeToPrice,
|
|
|
|
groupPlanToChangeToPriceError,
|
2023-02-21 10:24:28 -05:00
|
|
|
groupPlanToChangeToSize,
|
|
|
|
groupPlanToChangeToUsage,
|
2023-02-16 11:29:49 -05:00
|
|
|
handleCloseModal,
|
|
|
|
handleOpenModal,
|
2023-02-07 10:38:04 -05:00
|
|
|
hasDisplayedSubscription,
|
2023-08-10 05:44:01 -04:00
|
|
|
hasValidActiveSubscription,
|
2023-02-07 10:38:04 -05:00
|
|
|
institutionMemberships,
|
|
|
|
managedGroupSubscriptions,
|
2023-02-22 07:10:02 -05:00
|
|
|
memberGroupSubscriptions,
|
2023-02-14 05:21:25 -05:00
|
|
|
managedInstitutions,
|
2023-02-22 06:50:24 -05:00
|
|
|
managedPublishers,
|
2023-02-14 05:21:25 -05:00
|
|
|
updateManagedInstitution,
|
2023-02-16 11:29:49 -05:00
|
|
|
modalIdShown,
|
2023-02-07 10:38:04 -05:00
|
|
|
personalSubscription,
|
2023-02-22 07:18:01 -05:00
|
|
|
hasSubscription,
|
2023-02-07 10:38:04 -05:00
|
|
|
plans,
|
2023-02-16 11:29:49 -05:00
|
|
|
planCodeToChangeTo,
|
2023-02-21 10:24:49 -05:00
|
|
|
queryingGroupPlanToChangeToPrice,
|
2023-02-07 10:38:30 -05:00
|
|
|
queryingIndividualPlansData,
|
2023-01-26 12:01:54 -05:00
|
|
|
recurlyLoadError,
|
2023-02-21 10:24:28 -05:00
|
|
|
setGroupPlanToChangeToCode,
|
|
|
|
setGroupPlanToChangeToSize,
|
|
|
|
setGroupPlanToChangeToUsage,
|
2023-02-16 11:29:49 -05:00
|
|
|
setModalIdShown,
|
|
|
|
setPlanCodeToChangeTo,
|
2023-01-26 12:01:54 -05:00
|
|
|
setRecurlyLoadError,
|
|
|
|
showCancellation,
|
|
|
|
setShowCancellation,
|
2023-02-22 07:10:02 -05:00
|
|
|
leavingGroupId,
|
|
|
|
setLeavingGroupId,
|
2023-01-26 11:48:30 -05:00
|
|
|
}),
|
2023-01-26 12:01:54 -05:00
|
|
|
[
|
2023-02-21 10:24:28 -05:00
|
|
|
groupPlanToChangeToCode,
|
2023-02-21 10:24:49 -05:00
|
|
|
groupPlanToChangeToPrice,
|
|
|
|
groupPlanToChangeToPriceError,
|
2023-02-21 10:24:28 -05:00
|
|
|
groupPlanToChangeToSize,
|
|
|
|
groupPlanToChangeToUsage,
|
2023-02-16 11:29:49 -05:00
|
|
|
handleCloseModal,
|
|
|
|
handleOpenModal,
|
2023-02-07 10:38:04 -05:00
|
|
|
hasDisplayedSubscription,
|
2023-08-10 05:44:01 -04:00
|
|
|
hasValidActiveSubscription,
|
2023-02-07 10:38:04 -05:00
|
|
|
institutionMemberships,
|
|
|
|
managedGroupSubscriptions,
|
2023-02-22 07:10:02 -05:00
|
|
|
memberGroupSubscriptions,
|
2023-02-14 05:21:25 -05:00
|
|
|
managedInstitutions,
|
2023-02-22 06:50:24 -05:00
|
|
|
managedPublishers,
|
2023-02-14 05:21:25 -05:00
|
|
|
updateManagedInstitution,
|
2023-02-16 11:29:49 -05:00
|
|
|
modalIdShown,
|
2023-02-07 10:38:04 -05:00
|
|
|
personalSubscription,
|
2023-02-22 07:18:01 -05:00
|
|
|
hasSubscription,
|
2023-02-07 10:38:04 -05:00
|
|
|
plans,
|
2023-02-16 11:29:49 -05:00
|
|
|
planCodeToChangeTo,
|
2023-02-21 10:24:49 -05:00
|
|
|
queryingGroupPlanToChangeToPrice,
|
2023-02-07 10:38:30 -05:00
|
|
|
queryingIndividualPlansData,
|
2023-01-26 12:01:54 -05:00
|
|
|
recurlyLoadError,
|
2023-02-21 10:24:28 -05:00
|
|
|
setGroupPlanToChangeToCode,
|
|
|
|
setGroupPlanToChangeToSize,
|
|
|
|
setGroupPlanToChangeToUsage,
|
2023-02-16 11:29:49 -05:00
|
|
|
setModalIdShown,
|
|
|
|
setPlanCodeToChangeTo,
|
2023-01-26 12:01:54 -05:00
|
|
|
setRecurlyLoadError,
|
|
|
|
showCancellation,
|
|
|
|
setShowCancellation,
|
2023-02-22 07:10:02 -05:00
|
|
|
leavingGroupId,
|
|
|
|
setLeavingGroupId,
|
2023-01-26 12:01:54 -05:00
|
|
|
]
|
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
|
|
|
|
}
|