issue 12 : fix back navigation library #19
43
src/App.tsx
43
src/App.tsx
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue