-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathCheckoutWrapper.tsx
More file actions
47 lines (41 loc) · 1.46 KB
/
CheckoutWrapper.tsx
File metadata and controls
47 lines (41 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use client";
import React, { useEffect, useState } from "react";
import { useSubscription } from "@/hooks/useSubscription";
import { useRouter, useSearchParams } from "next/navigation";
import { useSession } from "next-auth/react";
import CheckoutConfirmation from "./checkout-confirmation";
export default function CheckoutWrapper() {
const { isPaidUser, isLoading } = useSubscription();
const router = useRouter();
const searchParams = useSearchParams();
const { data: session, update } = useSession();
const paymentSuccess = searchParams.get("payment") === "success";
const [hasRefreshed, setHasRefreshed] = useState(false);
useEffect(() => {
if (paymentSuccess && session && !hasRefreshed) {
update().then(() => {
setHasRefreshed(true);
router.refresh();
});
}
}, [paymentSuccess, session, update, hasRefreshed, router]);
// Show loading state while checking subscription
if (isLoading) {
return (
<div className="flex flex-col h-screen w-full justify-center items-center">
<div className="text-white text-xl">Loading...</div>
</div>
);
}
// Redirect to pricing if not a paid user
if (!isPaidUser) {
router.push("/pricing");
return (
<div className="flex flex-col h-screen w-full justify-center items-center">
<div className="text-white text-xl">Redirecting...</div>
</div>
);
}
// Show checkout confirmation for paid users
return <CheckoutConfirmation />;
}