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.
+
) : (
)}
- {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.`}
-