From 9720daea05bfa7c4f36228d8e620591ea215d435 Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Mon, 10 Aug 2026 14:01:57 +0300 Subject: [PATCH] Unify active subscriptions into one card with plan pickers for grant and extend. Co-authored-by: Cursor --- .../components/ExtendSubscriptionDialog.tsx | 7 +- .../components/GrantSubscriptionDialog.tsx | 82 +++--- .../components/UserSubscriptionsSection.tsx | 248 ++++++++++-------- 3 files changed, 175 insertions(+), 162 deletions(-) diff --git a/src/pages/user-management/components/ExtendSubscriptionDialog.tsx b/src/pages/user-management/components/ExtendSubscriptionDialog.tsx index b727c4a..d30c031 100644 --- a/src/pages/user-management/components/ExtendSubscriptionDialog.tsx +++ b/src/pages/user-management/components/ExtendSubscriptionDialog.tsx @@ -130,7 +130,7 @@ export function ExtendSubscriptionDialog({ }) : "the plan price" - const showPlanPicker = subscriptions.length > 1 + const showPlanPicker = subscriptions.length > 0 return ( <> @@ -152,9 +152,8 @@ export function ExtendSubscriptionDialog({ Extend subscription - Extend by one plan period - {showPlanPicker ? ". Choose which active subscription to extend" : ""} - . Choose whether to record a payment at the plan price. + Extend by one plan period. Select which active subscription to extend, then + choose whether to record a payment at the plan price. diff --git a/src/pages/user-management/components/GrantSubscriptionDialog.tsx b/src/pages/user-management/components/GrantSubscriptionDialog.tsx index cbad36b..a829628 100644 --- a/src/pages/user-management/components/GrantSubscriptionDialog.tsx +++ b/src/pages/user-management/components/GrantSubscriptionDialog.tsx @@ -31,9 +31,8 @@ type GrantSubscriptionDialogProps = { onOpenChange: (open: boolean) => void userId: number userName?: string + /** Categories that already have an active subscription for this learner. */ activeByCategory: Record - /** Plan IDs the learner already has an active subscription for. */ - activePlanIds?: Set | number[] onGranted: () => void } @@ -41,13 +40,19 @@ function planLabel(plan: SubscriptionPlan): string { return `${plan.name} · ${formatPlanCategory(plan.category)} · ${formatPlanDuration(plan)} · ${formatPlanPrice(plan)}` } +function isCategoryActive( + category: string, + activeByCategory: Record, +): boolean { + return activeByCategory[category] === true +} + export function GrantSubscriptionDialog({ open, onOpenChange, userId, userName, activeByCategory, - activePlanIds, onGranted, }: GrantSubscriptionDialogProps) { const [plans, setPlans] = useState([]) @@ -60,10 +65,15 @@ export function GrantSubscriptionDialog({ const [saving, setSaving] = useState(false) const [formError, setFormError] = useState(null) - const activePlanIdSet = useMemo(() => { - if (!activePlanIds) return new Set() - return activePlanIds instanceof Set ? activePlanIds : new Set(activePlanIds) - }, [activePlanIds]) + const activeCategoryKey = useMemo( + () => + Object.entries(activeByCategory) + .filter(([, active]) => active) + .map(([category]) => category) + .sort() + .join("|"), + [activeByCategory], + ) useEffect(() => { if (!open) return @@ -79,7 +89,12 @@ export function GrantSubscriptionDialog({ setPlansLoading(true) try { const res = await getSubscriptionPlans() - setPlans(res.data.filter((plan) => plan.is_active)) + setPlans( + res.data.filter( + (plan) => + plan.is_active && !isCategoryActive(plan.category, activeByCategory), + ), + ) } catch (err) { setPlans([]) notifyApiError(err, "Failed to load subscription plans") @@ -88,19 +103,15 @@ export function GrantSubscriptionDialog({ } } void loadPlans() - }, [open]) + // activeCategoryKey captures category coverage; activeByCategory is read for filtering. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [open, activeCategoryKey]) const selectedPlan = useMemo( () => plans.find((p) => String(p.id) === planId) ?? null, [plans, planId], ) - const planAlreadyActive = - selectedPlan != null && activePlanIdSet.has(selectedPlan.id) - const categoryBlocked = - selectedPlan != null && activeByCategory[selectedPlan.category] === true - const grantBlocked = planAlreadyActive || categoryBlocked - const filteredPlans = useMemo(() => { const q = planSearch.trim().toLowerCase() if (!q) return plans @@ -128,13 +139,11 @@ export function GrantSubscriptionDialog({ setFormError("Select a subscription plan.") return } - if (planAlreadyActive) { - setFormError( - "This learner already has an active subscription for that plan. Extend it instead.", - ) + if (!selectedPlan) { + setFormError("Select a subscription plan.") return } - if (categoryBlocked) { + if (isCategoryActive(selectedPlan.category, activeByCategory)) { setFormError( "This learner already has an active subscription in that category. Extend the existing plan or cancel it first.", ) @@ -146,7 +155,8 @@ export function GrantSubscriptionDialog({ const handleConfirmGrant = async () => { const id = Number(planId) - if (!Number.isFinite(id) || id < 1 || grantBlocked) return + if (!Number.isFinite(id) || id < 1 || !selectedPlan) return + if (isCategoryActive(selectedPlan.category, activeByCategory)) return setSaving(true) try { @@ -189,8 +199,9 @@ export function GrantSubscriptionDialog({ Grant plan access - {userName ? ` to ${userName}` : ""}. Choose whether to record a - payment at the plan price. + {userName ? ` to ${userName}` : ""}. Only plans in categories without an + active subscription are listed. Choose whether to record a payment at the + plan price. @@ -204,6 +215,11 @@ export function GrantSubscriptionDialog({ Loading plans… + ) : plans.length === 0 ? ( +

+ No grantable plans left — every available category already has an + active subscription. +

) : (
) @@ -293,13 +298,6 @@ export function GrantSubscriptionDialog({ ) : null}
)} - {selectedPlan && grantBlocked ? ( -

- {planAlreadyActive - ? "This plan is already active for the learner. Extend it instead." - : `An active ${formatPlanCategory(selectedPlan.category)} subscription already exists. Extend or cancel it instead.`} -

- ) : null}
@@ -378,7 +376,7 @@ export function GrantSubscriptionDialog({ + ) : null} +
+ - - + ) + })}
- {canExtend && !isLifetime ? ( + {canExtend ? ( - ) : null} - {canCancel ? ( - - ) : null} -
- )} + {canGrant ? ( + + ) : null} ) @@ -284,7 +295,7 @@ function EmptySubscriptionCard({ return (
-

Subscription

+

Subscriptions

{ - return new Set( - activePlans - .filter( - (subscription) => - subscription.is_currently_active || - subscription.status.toUpperCase() === "ACTIVE", - ) - .map((subscription) => subscription.plan_id), - ) - }, [activePlans]) + const activeCategories = useMemo(() => { + const fromSubs = activePlans + .filter( + (subscription) => + subscription.is_currently_active || + subscription.status.toUpperCase() === "ACTIVE", + ) + .map((subscription) => subscription.plan_category) + .filter((category) => Boolean(category?.trim())) - // Disable grant only when every available active plan already has an active subscription. - const allActivePlansSubscribed = - catalogPlans.length > 0 && - catalogPlans.every((plan) => subscribedPlanIds.has(plan.id)) - const grantEnabled = showGrant && !allActivePlansSubscribed + const categories = new Set(fromSubs) + for (const [category, active] of Object.entries( + subscriptions?.active_by_category ?? {}, + )) { + if (active) categories.add(category) + } + return categories + }, [activePlans, subscriptions?.active_by_category]) + + // Grant is available when at least one active catalog plan is in a category + // the learner does not already have covered. + const hasGrantablePlan = + catalogPlans.length === 0 + ? true + : catalogPlans.some((plan) => !activeCategories.has(plan.category)) + const grantEnabled = showGrant && hasGrantablePlan + const extendEnabled = canExtend && extendableActivePlans.length > 0 const openExtend = (preferred?: UserSubscriptionRecord) => { const preferredId = @@ -462,21 +483,17 @@ export function UserSubscriptionsSection({ {!loading && !error && subscriptions ? ( <> {activePlans.length > 0 ? ( -
- {activePlans.map((subscription) => ( - openExtend(subscription)} - onGrant={() => setGrantOpen(true)} - onCancel={() => setCancelTarget(subscription)} - /> - ))} -
+ openExtend()} + onGrant={() => setGrantOpen(true)} + onCancel={(subscription) => setCancelTarget(subscription)} + /> ) : (