Merge pull request 'Added navigate funtionalies' (#269) from Print-token into main
Reviewed-on: Pozomind/pozo-retail-app#269
This commit is contained in:
commit
5b2367fe73
103
src/App.jsx
103
src/App.jsx
|
|
@ -31,9 +31,9 @@ const isCapacitor = () => !!window.Capacitor?.isNativePlatform?.();
|
||||||
|
|
||||||
// ✅ Define your home/exit pages here
|
// ✅ Define your home/exit pages here
|
||||||
const HOME_PAGES = [
|
const HOME_PAGES = [
|
||||||
'/landing-page/home',
|
|
||||||
'/home/landing-page/home',
|
|
||||||
'/app-page/home', // android minimize
|
'/app-page/home', // android minimize
|
||||||
|
'/apps/retail/app-page/home',
|
||||||
|
'/' // android minimize
|
||||||
];
|
];
|
||||||
|
|
||||||
const LOGIN_PAGES = ['signin', 'login', 'auth'];
|
const LOGIN_PAGES = ['signin', 'login', 'auth'];
|
||||||
|
|
@ -88,104 +88,87 @@ const AppRoutes = () => {
|
||||||
const handlePopState = () => {
|
const handlePopState = () => {
|
||||||
const currentPath = location.pathname;
|
const currentPath = location.pathname;
|
||||||
|
|
||||||
// On home page — just stay, don't redirect
|
const SessionId = getSession('SessionId');
|
||||||
if (HOME_PAGES.some((p) => currentPath.includes(p))) {
|
|
||||||
window.history.go(1); // stay here
|
// 🟢 If session exists → allow navigation
|
||||||
|
if (SessionId) {
|
||||||
|
console.log("Browser back allowed");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SessionId = getSession('SessionId');
|
// 🔴 Only redirect if user is on protected page
|
||||||
if (!SessionId) {
|
if (!LOGIN_PAGES.some(p => currentPath.includes(p))) {
|
||||||
alert(
|
console.log("Session missing → redirecting login");
|
||||||
'Session invalid and Unauthorized action detected. Redirecting to login...'
|
|
||||||
);
|
|
||||||
clearSession();
|
clearSession();
|
||||||
window.location.replace(`${commonSubDir}`);
|
window.location.replace(`${commonSubDir}`);
|
||||||
} else {
|
|
||||||
window.history.go(1);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add event listener on mount
|
// Add event listener on mount
|
||||||
window.addEventListener('popstate', handlePopState);
|
window.addEventListener('popstate', handlePopState);
|
||||||
|
|
||||||
return () => window.removeEventListener('popstate', handlePopState);
|
return () => window.removeEventListener('popstate', handlePopState);
|
||||||
|
|
||||||
}, [location.pathname]);
|
}, [location.pathname]);
|
||||||
|
|
||||||
// ✅ Android — Capacitor back button
|
// ✅ Android — Capacitor back button
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isMobile && !isIOS && !isCapacitor()) return;
|
if (!isCapacitor()) return;
|
||||||
|
|
||||||
let backButtonListener;
|
const handler = async () => {
|
||||||
|
|
||||||
const setupBackButton = async () => {
|
|
||||||
try {
|
|
||||||
backButtonListener = await CapacitorApp.addListener(
|
|
||||||
'backButton',
|
|
||||||
async () => {
|
|
||||||
try {
|
try {
|
||||||
const currentPath = location.pathname + (location.search || '');
|
const currentPath = location.pathname + (location.search || '');
|
||||||
console.log('🔙 Back pressed:', currentPath);
|
console.log('🔙 Back pressed:', currentPath);
|
||||||
|
|
||||||
// 🏠 Home page → minimize
|
|
||||||
if (HOME_PAGES?.some((p) => currentPath?.startsWith(p))) {
|
|
||||||
if (isCapacitor()) {
|
|
||||||
await CapacitorApp.minimizeApp();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 🚪 Login page → exit
|
|
||||||
if (LOGIN_PAGES.some((p) => currentPath.includes(p))) {
|
|
||||||
if (isCapacitor()) {
|
|
||||||
await CapacitorApp.exitApp();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const raw = sessionStorage.getItem('navStack');
|
const raw = sessionStorage.getItem('navStack');
|
||||||
let stack = raw ? JSON.parse(raw) : [];
|
let stack = raw ? JSON.parse(raw) : [];
|
||||||
|
|
||||||
|
// Remove current path from stack if it's at the end
|
||||||
while (stack.length && stack[stack.length - 1] === currentPath) {
|
while (stack.length && stack[stack.length - 1] === currentPath) {
|
||||||
stack.pop();
|
stack.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stack.length) {
|
// If there are pages in history, go back to previous page
|
||||||
|
if (stack.length > 0) {
|
||||||
const previous = stack[stack.length - 1];
|
const previous = stack[stack.length - 1];
|
||||||
|
sessionStorage.setItem('navStack', JSON.stringify(stack));
|
||||||
if (LOGIN_PAGES.some((p) => previous.includes(p))) {
|
|
||||||
const home = '/landing-page/home';
|
|
||||||
sessionStorage.setItem('navStack', JSON.stringify([home]));
|
|
||||||
isBackNav.current = true;
|
isBackNav.current = true;
|
||||||
navigate(home);
|
navigate(previous);
|
||||||
|
console.log('Navigate to previous:', previous);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sessionStorage.setItem('navStack', JSON.stringify(stack));
|
// Stack is empty - check where we are
|
||||||
isBackNav.current = true;
|
// 🏠 Home page → minimize
|
||||||
navigate(previous);
|
if (HOME_PAGES?.some((p) => currentPath?.includes(p))) {
|
||||||
} else {
|
console.log("Home page & empty stack → minimizing app");
|
||||||
const home = '/landing-page/home';
|
await CapacitorApp.minimizeApp();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 🚪 Login page → minimize
|
||||||
|
if (LOGIN_PAGES?.some((p) => currentPath?.includes(p))) {
|
||||||
|
console.log("Login page & empty stack → minimizing app");
|
||||||
|
await CapacitorApp.minimizeApp();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Anywhere else with empty stack → go to home
|
||||||
|
const home = '/app-page/home';
|
||||||
sessionStorage.setItem('navStack', JSON.stringify([home]));
|
sessionStorage.setItem('navStack', JSON.stringify([home]));
|
||||||
isBackNav.current = true;
|
isBackNav.current = true;
|
||||||
navigate(home);
|
navigate(home);
|
||||||
}
|
console.log('Empty stack, navigate to home');
|
||||||
} catch (err) {
|
} catch (e) {
|
||||||
console.error('Back handler error:', err);
|
console.error('Back handler error:', e);
|
||||||
navigate('/landing-page/home');
|
await CapacitorApp.minimizeApp();
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Listener error:', err);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
setupBackButton();
|
const listener = CapacitorApp.addListener('backButton', handler);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
try {
|
listener.remove();
|
||||||
backButtonListener?.remove();
|
|
||||||
} catch (err) {}
|
|
||||||
};
|
};
|
||||||
}, [navigate, location.pathname, location.search]);
|
}, [navigate, location.pathname, location.search]);
|
||||||
// mohan
|
// mohan
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue