diff --git a/packages/frontend/src/main.tsx b/packages/frontend/src/main.tsx index e0d83d3..0245714 100644 --- a/packages/frontend/src/main.tsx +++ b/packages/frontend/src/main.tsx @@ -13,7 +13,6 @@ import Issues from "@/pages/Issues"; import Landing from "@/pages/Landing"; import NotFound from "@/pages/NotFound"; import Plans from "@/pages/Plans"; -import StripeTest from "@/pages/StripeTest"; import Test from "@/pages/Test"; import Timeline from "@/pages/Timeline"; @@ -54,14 +53,6 @@ ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( } /> - - - - } - /> ("annual"); const [loginModalOpen, setLoginModalOpen] = useState(false); - const [subscription, setSubscription] = useState(null); const [processingTier, setProcessingTier] = useState(null); - // fetch subscription if user is logged in - useEffect(() => { - if (user) { - getSubscription() - .then((result) => { - const data = result.data as { subscription?: SubscriptionResponse } | null; - if (data?.subscription) { - setSubscription(data.subscription); - } else { - setSubscription(null); - } - }) - .catch(() => { - setSubscription(null); - }); - } - }, [user]); + const { data: subscriptionData } = useSubscription(); + const createCheckoutSession = useCreateCheckoutSession(); + const createPortalSession = useCreatePortalSession(); + const subscription = subscriptionData?.subscription ?? null; const hasProSubscription = subscription?.status === "active"; const handleTierAction = async (tierName: string) => { @@ -50,21 +33,27 @@ export default function Plans() { if (hasProSubscription) { // open customer portal setProcessingTier(tierName); - const result = await createPortalSession(); - const portalData = result.data as { url?: string } | null; - if (portalData?.url) { - window.location.href = portalData.url; - } else { + try { + const result = await createPortalSession.mutateAsync(); + if (result.url) { + window.location.href = result.url; + } else { + setProcessingTier(null); + } + } catch { setProcessingTier(null); } } else { // start checkout setProcessingTier(tierName); - const result = await createCheckoutSession({ billingPeriod }); - const checkoutData = result.data as { url?: string } | null; - if (checkoutData?.url) { - window.location.href = checkoutData.url; - } else { + try { + const result = await createCheckoutSession.mutateAsync({ billingPeriod }); + if (result.url) { + window.location.href = result.url; + } else { + setProcessingTier(null); + } + } catch { setProcessingTier(null); } }