import React, { useState, useEffect } from "react"; import { useAddSubscription, 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"; import { useSelector } from "react-redux"; import { useNavigate } from "react-router-dom"; const SubScription = ({ onSubmitSubScription, onNext }) => { const [frequency, setFrequency] = useState(2); const [selectedPlanId, setSelectedPlanId] = useState(null); const selectedTenant = useSelector( (store) => store.globalVariables.currentTenant ); const naviget = useNavigate(); const { data: plans = [], isError, isLoading, } = useSubscriptionPlan(frequency); const { register, setValue, getValues, trigger, formState: { errors }, } = useFormContext(); const { mutate: AddSubScription, isPending, error, } = useAddSubscription(() => { onNext(); }); const handleSubscriptionSubmit = async () => { const isValid = await trigger([ "planId", "currencyId", "maxUsers", "frequency", "isTrial", "autoRenew", ]); if (isValid) { const payload = getValues(); // onSubmitSubScription(payload); const subscriptionPayload = { ...payload, tenantId: selectedTenant.id }; AddSubScription(subscriptionPayload); } }; 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
{plan?.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;