Skip to content

Commit

Permalink
refs #921 feat: set default tier based on current subscription census…
Browse files Browse the repository at this point in the history
… size
  • Loading branch information
elboletaire committed Jan 7, 2025
1 parent bb6394e commit cf88b75
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/components/Pricing/Plans.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,25 @@ export const SubscriptionPlans = ({ featuresRef }: { featuresRef?: MutableRefObj
const translations = usePlanTranslations()
const { openModal } = usePricingModal()

// Find the best fitting tier for the current subscription's census size
const defaultCensusSize = useMemo(() => {
if (!plans || !subscription?.subscriptionDetails?.maxCensusSize) return null

// Get all available tiers across all plans
const allTiers = plans.flatMap((plan) => plan.censusSizeTiers || [])

// Sort by upTo value to find the smallest tier that fits
const sortedTiers = allTiers.sort((a, b) => a.upTo - b.upTo)

// Find the first tier that can accommodate the current census size
const bestFitTier = sortedTiers.find((tier) => tier.upTo >= subscription.subscriptionDetails.maxCensusSize)

return bestFitTier?.upTo || null
}, [plans, subscription?.subscriptionDetails?.maxCensusSize])

const methods = useForm<FormValues>({
defaultValues: {
censusSize: null,
censusSize: defaultCensusSize,
planId: null,
},
})
Expand Down

0 comments on commit cf88b75

Please sign in to comment.