Skip to content

Commit

Permalink
Merge pull request #82 from ArhanAnsari/ArhanAnsari-patch-1
Browse files Browse the repository at this point in the history
Solving Application Error on /dashboard/upgrade
  • Loading branch information
ArhanAnsari authored Oct 8, 2024
2 parents 72d73d1 + 732ccaa commit 99a9a8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
47 changes: 33 additions & 14 deletions app/dashboard/upgrade/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ const UpgradePage: React.FC = () => {
const userData = await getUserData(session.user.email);
if (userData) {
setUserPlan(userData.plan); // Update the state with the user's plan
} else {
throw new Error("User data not found");
}
} catch (error) {
console.error("Error fetching user data:", error);
console.error("Error fetching user data:", error); // Log the error to the console
}
}
};
Expand All @@ -35,21 +37,38 @@ const UpgradePage: React.FC = () => {
}
}, [session]);

const getPriceFn = (plan: string) => {
if (plan === "free") {
// Redirect to dashboard if user selects the Free plan
router.push("/dashboard");
return;
const getPriceFn = async (plan: string) => {
if (plan === "free") {
// Redirect to dashboard if user selects the Free plan
router.push("/dashboard");
return;
}

try {
const response = await fetch(`/api/checkout?plan=${plan}`);
if (!response.ok) {
throw new Error(`Failed to initiate checkout for plan: ${plan}`);
}
const body = await response.json();
const sessionId = body.sessionId;

const stripe = await getStripe();
if (!stripe) {
throw new Error("Stripe initialization failed");
}

fetch(`/api/checkout?plan=${plan}`)
.then((data) => data.json())
.then(async (body) => {
const sessionId = body.sessionId;
const stripe = await getStripe();
await stripe?.redirectToCheckout({ sessionId });
});
};
await stripe.redirectToCheckout({ sessionId });
} catch (error) {
// Check if the error is an instance of Error
if (error instanceof Error) {
console.error("Error during checkout process:", error); // Log the error to the console
alert(`Error during checkout process: ${error.message}`); // Show the error to the user
} else {
console.error("Unknown error during checkout process:", error); // Log unknown error
alert("An unknown error occurred during checkout.");
}
}
};

const isCurrentPlan = (plan: string) => userPlan === plan; // Helper function to check if the user is on the current plan

Expand Down
2 changes: 1 addition & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function HomePage() {
Leverage the power of AI with Google Gemini to create amazing content effortlessly. Whether you need help generating stories, articles, or creative ideas, InspireGem is here to assist you.
</p>
<Link
href="/plans"
href="/dashboard/upgrade"
className="inline-block bg-blue-500 text-white font-bold py-3 px-6 rounded-lg shadow-md hover:bg-blue-600 transition-transform transform hover:scale-105"
>
View Plans
Expand Down

0 comments on commit 99a9a8f

Please sign in to comment.