"use client"; import ApiRequest from "@services/Api.Service"; import AnimatedContainer from "@core/app/components/AnimatedConainer/AnimatedContainer"; import SecurePage from "@core/app/components/Secure/Secure"; import { LOGIN_ACTIONS } from "@core/Store/LoginAction.Store"; import { useSessionStore } from "@core/Store/Sesion.Store"; import { useParams } from "next/navigation"; import { useSearchParams } from "next/navigation"; import { useEffect, useState, Suspense } from "react"; import { CreateMPPreApprovalParams, MP_PRE_APPROVAL_STATUS, MPPreApprovalResponse, } from "@core/Models/Subscription.Model"; import { useAlert } from "@core/Store/Alert.Store"; import { EVENT_TYPES, useEventHandlerStore } from "@core/Store/EventHandler.Store"; import { useNavigation } from "@core/app/hooks/goto"; import SuspenseLoading from "@core/app/components/SuspendeLoading/SuspenseLoading"; import { IPlan } from "@core/Models/Plans.model"; import styles from "./page.module.css"; type UrlData = { planid: string; }; function SubscriptionAddContent() { const data = useParams(); const searchParams = useSearchParams(); const id = data.planid; const isRenewal = searchParams.get("renew") === "1"; const SessionInfo = useSessionStore(); const alert = useAlert(); const eventHandler = useEventHandlerStore(); const { goTo } = useNavigation(); const [plan, setPlan] = useState(null); const [selectedMonths, setSelectedMonths] = useState(1); const [finalPrice, setFinalPrice] = useState(0); const [isSubmitting, setIsSubmitting] = useState(false); const getPlanInfo = () => { eventHandler.setEventType(EVENT_TYPES.LOADING); ApiRequest.post("plans/find", { _id: id }) .then((res) => { if (res && res.length > 0) { setPlan(res[0]); setFinalPrice(res[0].price); } }) .catch(() => { alert.showError("No se pudo cargar la información del plan"); }) .finally(() => { eventHandler.setEventType(EVENT_TYPES.SLEEP); }); }; useEffect(() => { if (id) { getPlanInfo(); } }, [id]); useEffect(() => { if (plan) { let discount = 0; if (selectedMonths === 3) discount = plan.discount3Months || 0; else if (selectedMonths === 6) discount = plan.discount6Months || 0; else if (selectedMonths === 12) discount = plan.discount12Months || 0; const total = plan.price * selectedMonths; const final = total - (total * (discount / 100)); setFinalPrice(final); } }, [selectedMonths, plan]); const getLink = () => { if (isSubmitting) { return; } if (SessionInfo.userId && SessionInfo.loged) { const preApprovalData: CreateMPPreApprovalParams = { planId: id, sessionUser: SessionInfo.userId, months: selectedMonths, }; eventHandler.setEventType(EVENT_TYPES.LOADING); setIsSubmitting(true); ApiRequest.post("suscriptions/preapproval", preApprovalData) .then(async (response) => { if (response.status === MP_PRE_APPROVAL_STATUS.READY_TO_START) { if (plan?.price === 0) { // Actualizamos la sesión manualmente para evitar la "race condition" con los sockets // Esto asegura que el estado de Zustand tenga la nueva suscripción antes de ir al dashboard await SessionInfo.checkSession(); } goTo(response.init_point); } }) .catch((error) => { alert.showError(error.format ? error.format() : "Ocurrió un error"); }) .finally(() => { setIsSubmitting(false); eventHandler.setEventType(EVENT_TYPES.SLEEP); }); } }; return ( }>
<>
{isRenewal ? "Resumen de renovación" : "Resumen de contratación"}

{plan?.name}

{plan?.price === 0 ? "Activa tu plan de forma instantánea. Sin tarjetas de crédito, sin complicaciones." : isRenewal ? "Elegí cuántos meses querés sumar. El nuevo período se agrega al vencimiento actual cuando se apruebe el pago." : "Selecciona la duración de tu plan. ¡Aprovechá los descuentos exclusivos pagando por más meses por adelantado!"}

{plan?.features && (
    {plan.features.slice(0, 4).map((feature, idx) => (
  • {feature}
  • ))} {plan.features.length > 4 && (
  • Y mucho más...
  • )}
)}
{plan?.price === 0 ? (
Suscripción Base Gratis por siempre
$0
) : ( [ { months: 1, label: '1 Mes', discount: 0 }, { months: 3, label: '3 Meses', discount: plan?.discount3Months || 0 }, { months: 6, label: '6 Meses', discount: plan?.discount6Months || 0 }, { months: 12, label: '12 Meses (1 Año)', discount: plan?.discount12Months || 0 }, ].map((option) => (
setSelectedMonths(option.months)} >
{option.label} {option.discount > 0 && ( {option.discount}% OFF )}
${plan ? ((plan.price * option.months) * (1 - option.discount / 100)).toLocaleString('es-AR') : '0'}
)) )}
Total a pagar hoy: ${plan?.price === 0 ? "0" : finalPrice.toLocaleString('es-AR')}
{plan?.price === 0 ? ( ) : ( )}
); } export default function SubscriptionAddPage() { return ( }> ); }