Android_Retail/src/App.jsx

224 lines
7.1 KiB
React
Raw Normal View History

import { useEffect, lazy, Suspense, useRef } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { App as CapacitorApp } from '@capacitor/app';
2026-01-27 18:27:29 +05:30
import { Routes, Route } from 'react-router-dom';
2026-02-10 16:35:19 +05:30
import { useSelector } from 'react-redux';
2026-01-27 18:27:29 +05:30
import ProtectedRoutes from './ProtectedRoutes';
import SelfBooking from './Pages/SelfBooking/SelfBooking.jsx';
2026-02-10 16:35:19 +05:30
import { isMobile, isIOS } from 'react-device-detect';
2026-01-27 18:27:29 +05:30
import { routesConfig } from './routesConfig';
2026-02-10 16:35:19 +05:30
import { GlobalItemCard } from './Features/BookingScreen/BookingData/BookingData.js';
import { clearSession, getSession } from './Services/Others.js';
2026-02-23 12:18:31 +05:30
const KisokSelBooking = lazy(() => import('./Pages/SelfBooking/KisokSelBooking'));
const IndividualBooking = lazy(() => import('./Pages/SelfBooking/IndividualBooking'));
2026-02-23 12:18:31 +05:30
2026-01-27 18:27:29 +05:30
const commonSubDir = import.meta.env.ENV_COMMON_BASE_URL;
const subDirectory = import.meta.env.ENV_BASE_URL;
2026-02-10 16:35:19 +05:30
import '../ownLib/my-ui-lib.css';
import CustomerMenuPage from './Pages/Reports/MenuQRCode/CustomerMenuPage.jsx';
import WeightScaleApp from './Pages/BookingScreen/WeightscaleApp.jsx';
import useSubscriptionManager from './useSubscriptionManager.js';
import useSessionManager from './useSessionManager.js';
import ExtendSubscriptionModal from './Pages/ExtentedModal/ExtendSubscriptionModal.jsx';
const isCapacitor = () => !!(window.Capacitor?.isNativePlatform?.());
// ✅ Define your home/exit pages here
const HOME_PAGES = [
'/landing-page/home',
'/home/landing-page/home',
'app-page/home', // android minimize
];
const LOGIN_PAGES = ['signin', 'login', 'auth'];
2026-02-11 12:45:21 +05:30
2026-01-27 18:27:29 +05:30
const AppRoutes = () => {
const ItemCard = useSelector(GlobalItemCard);
2026-02-11 15:27:50 +05:30
const { sessionData, logout } = useSessionManager(ItemCard);
const { remainingDays, handleCancel, freeExtend, extendModel, setExtendModel } = useSubscriptionManager(sessionData, logout);
const location = useLocation();
const navigate = useNavigate();
// ✅ Flag to prevent stack push when back button navigates
const isBackNav = useRef(false);
2026-02-10 16:35:19 +05:30
// ✅ Build navigation stack
2026-01-27 18:27:29 +05:30
useEffect(() => {
// Skip pushing to stack when back button caused this navigation
if (isBackNav.current) {
isBackNav.current = false;
return;
}
try {
const path = location.pathname + (location.search || '');
// Don't track login pages
if (LOGIN_PAGES.some(p => path.includes(p))) return;
const raw = sessionStorage.getItem('navStack');
let stack = raw ? JSON.parse(raw) : [];
if (stack.length === 0 || stack[stack.length - 1] !== path) {
stack.push(path);
if (stack.length > 50) stack = stack.slice(-50);
sessionStorage.setItem('navStack', JSON.stringify(stack));
console.log('📍 Stack:', stack);
}
} catch (e) {}
}, [location.pathname, location.search]);
// ✅ Web — block browser back button
useEffect(() => {
if (isCapacitor()) return; // Android handled separately
2026-02-10 16:35:19 +05:30
const handlePopState = () => {
const currentPath = location.pathname;
// On home page — just stay, don't redirect
if (HOME_PAGES.some(p => currentPath.includes(p))) {
window.history.go(1); // stay here
return;
}
2026-01-27 18:27:29 +05:30
const SessionId = getSession('SessionId');
if (!SessionId) {
alert(
'Session invalid and Unauthorized action detected. Redirecting to login...'
);
clearSession();
window.location.replace(`${commonSubDir}`);
} else {
window.history.go(1); // block back on web
2026-01-27 18:27:29 +05:30
}
};
window.addEventListener('popstate', handlePopState);
return () => window.removeEventListener('popstate', handlePopState);
}, [location.pathname]);
2026-01-27 18:27:29 +05:30
// ✅ Android — Capacitor back button
useEffect(() => {
if (!isMobile && !isIOS && !isCapacitor()) return;
const handler = async () => {
try {
const currentPath = location.pathname + (location.search || '');
console.log('🔙 Back pressed:', currentPath);
// ✅ On home page
if (HOME_PAGES.some(p => currentPath.includes(p))) {
if (isCapacitor()) {
console.log('📱 Minimizing app');
await CapacitorApp.minimizeApp(); // minimize — not exit
}
return;
}
// ✅ On login page → exit app
if (LOGIN_PAGES.some(p => currentPath.includes(p))) {
console.log('🚪 Exiting app');
if (isCapacitor()) await CapacitorApp.exitApp();
return;
}
// ✅ Get stack and go back one by one
const raw = sessionStorage.getItem('navStack');
let stack = raw ? JSON.parse(raw) : [];
// Remove current path from top
while (stack.length > 0 && stack[stack.length - 1] === currentPath) {
stack.pop();
}
if (stack.length > 0) {
const previous = stack[stack.length - 1];
// If previous is login → go to home instead
if (LOGIN_PAGES.some(p => previous.includes(p))) {
console.log('⬅️ Previous was login → going home');
const home = '/landing-page/home';
sessionStorage.setItem('navStack', JSON.stringify([home]));
isBackNav.current = true;
navigate(home);
return;
}
console.log('⬅️ Back to:', previous);
sessionStorage.setItem('navStack', JSON.stringify(stack));
isBackNav.current = true; // ✅ Prevent loop
navigate(previous);
} else {
// Stack empty → go home
const home = '/landing-page/home';
console.log('📭 Stack empty → home');
sessionStorage.setItem('navStack', JSON.stringify([home]));
isBackNav.current = true;
navigate(home);
}
} catch (e) {
console.error('Back error:', e);
navigate('/landing-page/home');
}
2026-01-27 18:27:29 +05:30
};
let listener;
try {
listener = CapacitorApp.addListener('backButton', handler);
} catch (e) {}
2026-01-27 18:27:29 +05:30
return () => {
try {
if (listener?.remove) listener.remove();
} catch (e) {}
};
}, [navigate, location.pathname, location.search]);
2026-01-27 18:27:29 +05:30
2026-02-11 12:09:36 +05:30
if (extendModel) {
return (
<ExtendSubscriptionModal
onClose={handleCancel}
2026-02-23 12:18:31 +05:30
onPayNow={handlePay}
onContinueWithCredits={freeExtend}
2026-01-27 18:27:29 +05:30
/>
2026-02-11 12:09:36 +05:30
);
}
2026-02-11 12:09:36 +05:30
return (
2026-02-24 17:19:56 +05:30
<Suspense fallback={<CardSkeleton />}>
2026-02-11 12:09:36 +05:30
<Routes>
<Route
path={`${subDirectory}selfBooking/:SelfBookingId`}
element={<SelfBooking />}
/>
<Route
path={`${subDirectory}MenuQRCode`}
element={<CustomerMenuPage />}
/>
<Route
path={`${subDirectory}weightscaleapp`}
element={<WeightScaleApp />}
/>
2026-02-10 16:35:19 +05:30
<Route
path={`${subDirectory}kioskSelfBooking`}
element={<KisokSelBooking />}
/>
2026-01-27 18:27:29 +05:30
2026-02-11 12:09:36 +05:30
<Route
path={`${subDirectory}kiosk-individual-self-booking`}
element={<IndividualBooking />}
/>
<Route
path="/*"
element={<ProtectedRoutes routesConfig={routesConfig} />}
/>
</Routes>
2026-02-10 16:35:19 +05:30
</Suspense>
2026-01-27 18:27:29 +05:30
);
};
export default AppRoutes;