page.tsx58 lines, 1.6 KB
| 1 | "use client"; |
| 2 | |
| 3 | import ContractAnalysisResults from "@/components/analysis/contract-analysis-results"; |
| 4 | import EmptyState from "@/components/analysis/empty-state"; |
| 5 | import { useSubscription } from "@/hooks/use-subscription"; |
| 6 | import { api } from "@/lib/api"; |
| 7 | import stripePromise from "@/lib/stripe"; |
| 8 | import { useContractStore } from "@/store/zustand"; |
| 9 | import { toast } from "sonner"; |
| 10 | |
| 11 | export default function ContractResultsPage() { |
| 12 | const analysisResults = useContractStore((state) => state.analysisrResults); |
| 13 | |
| 14 | const { |
| 15 | subscriptionStatus, |
| 16 | isSubscriptionLoading, |
| 17 | isSubscriptionError, |
| 18 | setLoading, |
| 19 | } = useSubscription(); |
| 20 | |
| 21 | if (!subscriptionStatus) { |
| 22 | return <div>Loading...</div>; |
| 23 | } |
| 24 | |
| 25 | const isActive = subscriptionStatus.status === "active"; |
| 26 | |
| 27 | const handleUpgrade = async () => { |
| 28 | setLoading(true); |
| 29 | if (!isActive) { |
| 30 | try { |
| 31 | const response = await api.get("/payments/create-checkout-session"); |
| 32 | const stripe = await stripePromise; |
| 33 | await stripe?.redirectToCheckout({ |
| 34 | sessionId: response.data.sessionId, |
| 35 | }); |
| 36 | } catch (error) { |
| 37 | toast.error("Please try again or login to your account"); |
| 38 | } finally { |
| 39 | setLoading(false); |
| 40 | } |
| 41 | } else { |
| 42 | toast.error("You are already a premium member"); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | if (!analysisResults) { |
| 47 | return <EmptyState title="No Analysis" description="Please try again" />; |
| 48 | } |
| 49 | |
| 50 | return ( |
| 51 | <ContractAnalysisResults |
| 52 | contractId={analysisResults._id} |
| 53 | isActive={isActive} |
| 54 | analysisResults={analysisResults} |
| 55 | onUpgrade={handleUpgrade} |
| 56 | /> |
| 57 | ); |
| 58 | } |