fix(checkout): redirect back navigation to library after module purchase (#12)

This commit is contained in:
ShivaniKhaire 2026-10-05 14:23:21 +05:30
parent 776e09a918
commit 3f536986de
2 changed files with 56 additions and 12 deletions

View File

@ -41,14 +41,6 @@ export default function App() {
}
};
useEffect(() => {
const handlePopState = () => {
setActiveTab(getInitialTab());
};
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, []);
const [currency, setCurrency] = useState<Currency>('INR');
// Auth state
@ -105,6 +97,30 @@ export default function App() {
const [verificationResult, setVerificationResult] = useState<VerificationResult | null>(null);
const [razorpayKeyId, setRazorpayKeyId] = useState<string>('');
// Handle browser back button navigation
useEffect(() => {
const handlePopState = () => {
// If purchase was completed and user presses the Back button,
// redirect them to the mobile app's All Modules / Library instead of Subscription page
if (verificationResult && verificationResult.status === 'success') {
const isMobile = /Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
if (isMobile) {
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
} else {
switchTab('subscriptions');
setVerificationResult(null);
}
return;
}
setActiveTab(getInitialTab());
};
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, [verificationResult]);
// Profile Update & Drawer State
const [showProfileDrawer, setShowProfileDrawer] = useState<boolean>(false);
const [profileDisplayName, setProfileDisplayName] = useState<string>('');
@ -658,9 +674,18 @@ export default function App() {
message: verifyRes.data.message || 'Payment verified successfully and module entitlements unlocked!'
});
// Push a history state so that pressing browser/device Back button will be intercepted
// by popstate and redirected to All Modules / Library instead of returning to the Subscription page
if (typeof window !== 'undefined') {
window.history.pushState({ paymentCompleted: true }, document.title, window.location.href);
}
if (/Android|iPhone|iPad|iPod/i.test(navigator.userAgent)) {
setTimeout(() => {
window.location.href = 'acupedia://';
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
}, 1500);
}

View File

@ -176,11 +176,30 @@ export const PaymentSuccessModal: React.FC<PaymentSuccessModalProps> = ({
)}
<a
href="acupedia://"
className="w-full h-9 bg-emerald-600 hover:bg-emerald-500 text-white font-bold text-xs rounded-lg transition-all duration-300 flex items-center justify-center gap-2 shadow-md shadow-emerald-600/10 no-underline decoration-0"
href="acupedia://modules"
onClick={(e) => {
e.preventDefault();
const userAgent = navigator.userAgent || '';
const isAndroid = /Android/i.test(userAgent);
const isIOS = /iPhone|iPad|iPod/i.test(userAgent);
if (isAndroid) {
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
} else if (isIOS) {
window.location.href = 'acupedia://modules';
setTimeout(() => {
window.location.href = 'myapp://modules';
}, 500);
} else {
window.location.href = 'acupedia://modules';
}
}}
className="w-full h-9 bg-emerald-600 hover:bg-emerald-500 text-white font-bold text-xs rounded-lg transition-all duration-300 flex items-center justify-center gap-2 shadow-md shadow-emerald-600/10 no-underline decoration-0 cursor-pointer"
>
<Smartphone className="h-3.5 w-3.5 text-emerald-200" />
Open Acupedia Mobile App
Open in App / Library
</a>
<button