import React, { useState, useEffect } from "react"; import { useSubscriptionPlan } from "../../hooks/useTenant"; import SegmentedControl from "./SegmentedControl"; import { useFormContext } from "react-hook-form"; import { CONSTANT_TEXT } from "../../utils/constants"; import Label from "../common/Label"; const SubScription = ({ onSubmitSubScription }) => { const [frequency, setFrequency] = useState(2); const [selectedPlanId, setSelectedPlanId] = useState(null); const { data: plans = [], isError, isLoading, } = useSubscriptionPlan(frequency); const { register, setValue, getValues, trigger, formState: { errors }, } = useFormContext(); const handleSubscriptionSubmit = async () => { const isValid = await trigger([ "planId", "currencyId", "maxUsers", "frequency", "isTrial", "autoRenew", ]); if (isValid) { const payload = getValues(); onSubmitSubScription(payload); } }; const handlePlanSelection = (plan) => { setSelectedPlanId(plan.id); setValue("planId", plan.id); setValue("currencyId", plan.currency?.id); setValue("frequency", frequency); }; const selectedPlan = plans.find((p) => p.id === selectedPlanId); return (
{!isLoading && !isError && plans.length > 0 && (
{plans.map((plan) => { const isSelected = plan.id === selectedPlanId; return (
handlePlanSelection(plan)} >

{plan.planName}

{plan.description}

{plan.currency?.symbol} {plan.price}

  • Max Users {plan.maxUser}
  • Storage {plan.maxStorage} MB
  • Trial Days {plan.trialDays}
Features
{Object.entries(plan.features.modules) .filter(([key]) => key !== "id") .map(([key, mod]) => (
{mod.name}
))}
); })} {/* Form Inputs */}
{CONSTANT_TEXT.RenewsubscriptionLabel}
{Object.keys(errors).length > 0 && ( )}
)}
); }; export default SubScription;