issue 12 : fix back navigation library #19
51
src/App.tsx
51
src/App.tsx
|
|
@ -19,6 +19,7 @@ import { AuthModal } from './components/auth/AuthModal';
|
|||
import { ProfileDrawer } from './components/auth/ProfileDrawer';
|
||||
import { PaymentSuccessModal } from './components/checkout/PaymentSuccessModal';
|
||||
import { PasswordUpdatedModal } from './components/auth/PasswordUpdatedModal';
|
||||
import { openMobileApp, DEFAULT_RETURN_ROUTE } from './utils/navigation';
|
||||
|
||||
const getInitialTab = (): 'storefront' | 'subscriptions' => {
|
||||
if (typeof window !== 'undefined') {
|
||||
|
|
@ -43,14 +44,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
|
||||
|
|
@ -106,6 +99,29 @@ export default function App() {
|
|||
// Active payment state
|
||||
const [verificationResult, setVerificationResult] = useState<VerificationResult | null>(null);
|
||||
const [razorpayKeyId, setRazorpayKeyId] = useState<string>('');
|
||||
const [returnRoute, setReturnRoute] = useState<string>(DEFAULT_RETURN_ROUTE);
|
||||
const [appScheme, setAppScheme] = useState<string | undefined>(undefined);
|
||||
|
||||
// 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) {
|
||||
openMobileApp({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined });
|
||||
} else {
|
||||
switchTab('subscriptions');
|
||||
setVerificationResult(null);
|
||||
}
|
||||
return;
|
||||
}
|
||||
setActiveTab(getInitialTab());
|
||||
};
|
||||
window.addEventListener('popstate', handlePopState);
|
||||
return () => window.removeEventListener('popstate', handlePopState);
|
||||
}, [verificationResult, returnRoute, appScheme]);
|
||||
|
||||
// Profile Update & Drawer State
|
||||
const [showProfileDrawer, setShowProfileDrawer] = useState<boolean>(false);
|
||||
|
|
@ -157,6 +173,15 @@ export default function App() {
|
|||
const params = new URLSearchParams(window.location.search);
|
||||
const urlToken = params.get('token');
|
||||
const urlPlanCode = params.get('planCode');
|
||||
const urlReturnRoute = params.get('returnRoute') || params.get('redirectPath') || params.get('redirect');
|
||||
const urlScheme = params.get('scheme');
|
||||
|
||||
if (urlReturnRoute) {
|
||||
setReturnRoute(urlReturnRoute);
|
||||
}
|
||||
if (urlScheme) {
|
||||
setAppScheme(urlScheme);
|
||||
}
|
||||
|
||||
let activeToken = userToken;
|
||||
|
||||
|
|
@ -742,7 +767,13 @@ export default function App() {
|
|||
message: verifyRes.data.message || 'Payment verified successfully and module entitlements unlocked!'
|
||||
});
|
||||
|
||||
// If on mobile browser, automatically attempt to redirect to mobile app after successful payment
|
||||
|
||||
// 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(() => {
|
||||
if (/Android/i.test(navigator.userAgent)) {
|
||||
|
|
@ -957,6 +988,8 @@ export default function App() {
|
|||
loggedInUser={loggedInUser}
|
||||
onDownloadInvoice={handleDownloadCheckoutInvoice}
|
||||
downloadingInvoice={downloadingModalInvoice}
|
||||
returnRoute={returnRoute}
|
||||
appScheme={appScheme}
|
||||
/>
|
||||
)}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import React, { useState } from 'react';
|
||||
import { XCircle, CheckCircle, Check, Download, RefreshCw, Smartphone } from 'lucide-react';
|
||||
import type { VerificationResult, UserProfile } from '../../types';
|
||||
import { openMobileApp, getAppDeepLinkUrls } from '../../utils/navigation';
|
||||
|
||||
interface PaymentSuccessModalProps {
|
||||
verificationResult: VerificationResult;
|
||||
|
|
@ -8,6 +9,8 @@ interface PaymentSuccessModalProps {
|
|||
loggedInUser: UserProfile | null;
|
||||
onDownloadInvoice: (paymentId: string, invoiceUrl?: string) => void;
|
||||
downloadingInvoice: boolean;
|
||||
returnRoute?: string;
|
||||
appScheme?: string;
|
||||
}
|
||||
|
||||
export const PaymentSuccessModal: React.FC<PaymentSuccessModalProps> = ({
|
||||
|
|
@ -16,6 +19,8 @@ export const PaymentSuccessModal: React.FC<PaymentSuccessModalProps> = ({
|
|||
loggedInUser,
|
||||
onDownloadInvoice,
|
||||
downloadingInvoice,
|
||||
returnRoute,
|
||||
appScheme,
|
||||
}) => {
|
||||
const [desktopNotice, setDesktopNotice] = useState(false);
|
||||
|
||||
|
|
@ -212,15 +217,18 @@ export const PaymentSuccessModal: React.FC<PaymentSuccessModalProps> = ({
|
|||
)}
|
||||
|
||||
{/* Open in App Button */}
|
||||
<a
|
||||
href="acupedia://"
|
||||
id="open-in-app-btn"
|
||||
onClick={handleOpenApp}
|
||||
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 in App
|
||||
</a>
|
||||
{/* Open in App Button */}
|
||||
<a
|
||||
href={getAppDeepLinkUrls({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined })[0]}
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
openMobileApp({ route: returnRoute, schemes: appScheme ? [appScheme] : undefined });
|
||||
}}
|
||||
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 in App / Library
|
||||
</a>
|
||||
|
||||
{desktopNotice && (
|
||||
<div className="p-2.5 bg-slate-950 border border-emerald-900/40 rounded-lg text-center animate-in fade-in duration-200">
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* Generic Navigation & Deep-Linking Utility for Acupedia Storefront
|
||||
*/
|
||||
|
||||
export const DEFAULT_APP_SCHEMES = ['acupedia', 'myapp'];
|
||||
export const DEFAULT_RETURN_ROUTE = 'modules';
|
||||
|
||||
export interface MobileAppLaunchOptions {
|
||||
route?: string;
|
||||
schemes?: string[];
|
||||
delayBetweenFallbacks?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanly format route for custom URI scheme (e.g. "/modules" -> "modules")
|
||||
*/
|
||||
export function formatDeepLinkRoute(route?: string): string {
|
||||
if (!route) return DEFAULT_RETURN_ROUTE;
|
||||
return route.replace(/^\/+/, '').trim() || DEFAULT_RETURN_ROUTE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate full deep-link URLs for supported schemes
|
||||
*/
|
||||
export function getAppDeepLinkUrls(options: MobileAppLaunchOptions = {}): string[] {
|
||||
const route = formatDeepLinkRoute(options.route);
|
||||
const schemes = options.schemes && options.schemes.length > 0 ? options.schemes : DEFAULT_APP_SCHEMES;
|
||||
return schemes.map((scheme) => `${scheme}://${route}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic function to launch the mobile app with progressive fallback across supported schemes
|
||||
*/
|
||||
export function openMobileApp(options: MobileAppLaunchOptions = {}): void {
|
||||
const urls = getAppDeepLinkUrls(options);
|
||||
if (urls.length === 0) return;
|
||||
|
||||
const [primaryUrl, ...fallbackUrls] = urls;
|
||||
window.location.href = primaryUrl;
|
||||
|
||||
fallbackUrls.forEach((url, idx) => {
|
||||
setTimeout(() => {
|
||||
window.location.href = url;
|
||||
}, (idx + 1) * (options.delayBetweenFallbacks || 500));
|
||||
});
|
||||
}
|
||||
Loading…
Reference in New Issue