'use client'; import { useState, useEffect } from 'react'; import { listPlans, deletePlan, getEntitlements } from '@/src/lib/api/facility-admin'; import type { MembershipPlan, PlanTemplate, PlanEntitlements } from '@/src/types/facility-admin'; import PlanCard from '@/src/components/plans/PlanCard'; import PlanFormModal from '@/src/components/plans/PlanFormModal'; import PlanListSkeleton from '@/src/components/plans/PlanListSkeleton'; import TemplatePicker from '@/src/components/plans/TemplatePicker'; import EntitlementsConfigModal from '@/src/components/plans/EntitlementsConfigModal'; interface MembershipPlansComponentProps { facilityId: number; } export default function MembershipPlansComponent({ facilityId }: MembershipPlansComponentProps) { const [plans, setPlans] = useState([]); const [entitlementsMap, setEntitlementsMap] = useState>({}); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [templatePickerOpen, setTemplatePickerOpen] = useState(false); const [createModalOpen, setCreateModalOpen] = useState(false); const [selectedTemplate, setSelectedTemplate] = useState(null); const [editingPlan, setEditingPlan] = useState(null); const [configuringPlan, setConfiguringPlan] = useState(null); useEffect(() => { fetchPlans(); }, [facilityId]); async function fetchPlans() { setLoading(true); setError(null); const result = await listPlans(facilityId, { include_inactive: false }); if (result.success) { setPlans(result.data); // Fetch entitlements for all plans in parallel await fetchAllEntitlements(result.data); } else { setError(result.error.detail || 'Failed to load plans'); } setLoading(false); } async function fetchAllEntitlements(plansList: MembershipPlan[]) { const entitlementPromises = plansList.map(async (plan) => { const result = await getEntitlements(facilityId, plan.facility_membership_plan_id); if (result.success) { return { planId: plan.facility_membership_plan_id, entitlements: result.data }; } return null; }); const results = await Promise.all(entitlementPromises); const newEntitlementsMap: Record = {}; for (const result of results) { if (result) { newEntitlementsMap[result.planId] = result.entitlements; } } setEntitlementsMap(newEntitlementsMap); } async function handleDelete(planId: number, planName: string) { if (!confirm(`Are you sure you want to deactivate "${planName}"?`)) return; const result = await deletePlan(facilityId, planId); if (result.success) { fetchPlans(); } else { alert(result.error.detail || 'Failed to delete plan'); } } function handleCreateClick() { // Open template picker first setTemplatePickerOpen(true); } function handleTemplateSelect(template: PlanTemplate | null) { setSelectedTemplate(template); setTemplatePickerOpen(false); setCreateModalOpen(true); } function handleCreateModalClose() { setCreateModalOpen(false); setSelectedTemplate(null); } return (
{/* Header */}

Membership Plans

Create and manage subscription plans for your facility

{/* Error Message */} {error && (
{error}
)} {/* Plans Grid */} {loading ? ( ) : plans.length === 0 ? (
📋

No membership plans yet

Create your first plan to get started

) : (
{plans.map(plan => ( setEditingPlan(plan)} onDelete={() => handleDelete(plan.facility_membership_plan_id, plan.name)} onConfigureEntitlements={() => setConfiguringPlan(plan)} /> ))}
)} {/* Template Picker Modal */} setTemplatePickerOpen(false)} onSelect={handleTemplateSelect} /> {/* Create Modal */} {createModalOpen && ( )} {/* Edit Modal */} {editingPlan && ( setEditingPlan(null)} facilityId={facilityId} plan={editingPlan} onSuccess={fetchPlans} /> )} {/* Entitlements Config Modal */} {configuringPlan && ( setConfiguringPlan(null)} facilityId={facilityId} plan={configuringPlan} onSuccess={fetchPlans} /> )}
); }