import React, { useEffect, useState } from "react";
import shinaraHelper from "./ShinaraHelper"; // import the helper class
const App: React.FC = () => {
const [loading, setLoading] = useState(false);
const handleBuySubscription = async () => {
setLoading(true);
// Get the referral code from localStorage
const referralCode = shinaraHelper.getReferralCode();
try {
// Call your backend to create the Stripe session and pass the referral code
const response = await fetch("/api/create-stripe-session", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
referral_code: referralCode,
}),
});
const data = await response.json();
if (response.ok) {
// Redirect the user to Stripe's checkout page
window.location.href = data.stripeCheckoutUrl;
} else {
console.error("Failed to create Stripe session", data.message);
}
} catch (error) {
console.error("Error creating Stripe session:", error);
} finally {
setLoading(false);
}
};
return (
<div>
<h1>Welcome to Shinara App</h1>
<button onClick={handleBuySubscription} disabled={loading}>
{loading ? "Processing..." : "Buy Subscription"}
</button>
</div>
);
};
export default App;