From eff2e26fe4692ba8b0aedc11ced13cc3c5ffbed7 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Tue, 22 Jul 2025 20:01:13 +0100 Subject: [PATCH] Add purchase button events --- src/components/Premium/PremiumPage.tsx | 129 ++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 12 deletions(-) diff --git a/src/components/Premium/PremiumPage.tsx b/src/components/Premium/PremiumPage.tsx index 342388ee9..c4a3d3faa 100644 --- a/src/components/Premium/PremiumPage.tsx +++ b/src/components/Premium/PremiumPage.tsx @@ -8,25 +8,114 @@ import { Wand2, Zap, } from 'lucide-react'; -import { useState } from 'react'; +import { useState, useEffect } from 'react'; +import { useMutation } from '@tanstack/react-query'; +import { useToast } from '../../hooks/use-toast'; +import { httpPost } from '../../lib/query-http'; +import { + USER_SUBSCRIPTION_PLAN_PRICES, + type AllowedSubscriptionInterval, +} from '../../queries/billing'; +import { queryClient } from '../../stores/query-client'; import { VideoModal } from '../VideoModal'; import { CredibilityStats } from './CredibilityStats'; import { FeatureCard } from './FeatureCard'; import { StatsItem } from './StatsItem'; import { features, paidFeaturesList } from './constants'; - - +import { isLoggedIn } from '../../lib/jwt'; +import { showLoginPopup } from '../../lib/popup'; export function PremiumPage() { const [activeVideoId, setActiveVideoId] = useState(null); + const [selectedPlan, setSelectedPlan] = + useState(null); + + const toast = useToast(); + + useEffect(() => { + window?.fireEvent?.({ + action: 'tutor_pricing', + category: 'ai_tutor', + label: 'Clicked Upgrade to Pro', + }); + }, []); + + const { + mutate: createCheckoutSession, + isPending: isCreatingCheckoutSession, + } = useMutation( + { + mutationFn: (body: { + priceId: string; + success?: string; + cancel?: string; + }) => { + return httpPost<{ checkoutUrl: string }>( + '/v1-create-subscription-checkout-session', + body, + ); + }, + onSuccess: (data) => { + window.location.href = data.checkoutUrl; + }, + onError: (error: any) => { + console.error(error); + toast.error(error?.message || 'Failed to create checkout session'); + }, + }, + queryClient, + ); + + const monthlyPlan = USER_SUBSCRIPTION_PLAN_PRICES.find( + (p) => p.interval === 'month', + ); + const yearlyPlan = USER_SUBSCRIPTION_PLAN_PRICES.find( + (p) => p.interval === 'year', + ); const activeVideoStartTime = features.find((feature) => feature.videoId === activeVideoId)?.startTime || '0'; - const handleUpgrade = () => { - alert('Upgrade functionality coming soon!'); - }; + function handleUpgrade(plan: (typeof USER_SUBSCRIPTION_PLAN_PRICES)[number]) { + if (!isLoggedIn()) { + showLoginPopup(); + return; + } + + if (!plan) { + return; + } + + setSelectedPlan(plan.interval); + + const currentUrlPath = window.location.pathname; + const encodedCurrentUrlPath = encodeURIComponent(currentUrlPath); + const successPage = `/thank-you?next=${encodedCurrentUrlPath}&s=1`; + + window?.fireEvent?.({ + action: 'tutor_checkout', + category: 'ai_tutor', + label: 'Checkout Started', + }); + + createCheckoutSession( + { + priceId: plan.priceId, + success: successPage, + cancel: `${currentUrlPath}?s=0`, + }, + { + onSuccess: () => { + window?.fireEvent?.({ + action: `tutor_checkout_${plan.interval === 'month' ? 'mo' : 'an'}`, + category: 'ai_tutor', + label: `${plan.interval} Plan Checkout Started`, + }); + }, + }, + ); + } return (
@@ -129,10 +218,18 @@ export function PremiumPage() {

    {paidFeaturesList.map((feature) => ( @@ -164,10 +261,18 @@ export function PremiumPage() {

      {paidFeaturesList.map((feature) => (