import React, { useState, useEffect, useContext } from 'react';
import {
Container,
Paper,
Box,
Typography,
Tabs,
Tab,
CircularProgress
} from '@mui/material';
import { useLocation, useNavigate, Navigate } from 'react-router-dom';
import SocketContext from '../contexts/SocketContext.js';
// Import extracted components
import OrdersTab from '../components/profile/OrdersTab.js';
import SettingsTab from '../components/profile/SettingsTab.js';
import CartTab from '../components/profile/CartTab.js';
import LoginComponent from '../components/LoginComponent.js';
// Functional Profile Page Component
const ProfilePage = (props) => {
const location = useLocation();
const navigate = useNavigate();
const [tabValue, setTabValue] = useState(0);
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const [showLogin, setShowLogin] = useState(false);
const [orderIdFromHash, setOrderIdFromHash] = useState(null);
const [paymentCompletion, setPaymentCompletion] = useState(null);
// @note Check for payment completion parameters from Stripe redirect
useEffect(() => {
const urlParams = new URLSearchParams(location.search);
const isComplete = urlParams.has('complete');
const paymentIntent = urlParams.get('payment_intent');
const paymentIntentClientSecret = urlParams.get('payment_intent_client_secret');
const redirectStatus = urlParams.get('redirect_status');
if (isComplete && paymentIntent && redirectStatus) {
setPaymentCompletion({
paymentIntent,
paymentIntentClientSecret,
redirectStatus,
isSuccessful: redirectStatus === 'succeeded'
});
// Clean up the URL by removing the payment parameters
const newUrl = new URL(window.location);
newUrl.searchParams.delete('complete');
newUrl.searchParams.delete('payment_intent');
newUrl.searchParams.delete('payment_intent_client_secret');
newUrl.searchParams.delete('redirect_status');
window.history.replaceState({}, '', newUrl.toString());
}
}, [location.search]);
useEffect(() => {
const hash = location.hash;
switch (hash) {
case '#cart':
setTabValue(0);
setOrderIdFromHash(null);
break;
case '#orders':
setTabValue(1);
setOrderIdFromHash(null);
break;
case '#settings':
setTabValue(2);
setOrderIdFromHash(null);
break;
default:
if (hash && hash.startsWith('#ORD-')) {
const orderId = hash.substring(1);
setOrderIdFromHash(orderId);
setTabValue(1); // Switch to Orders tab
} else {
setOrderIdFromHash(null);
}
}
}, [location.hash]);
useEffect(() => {
const checkUserLoggedIn = () => {
const storedUser = sessionStorage.getItem('user');
if (!storedUser) {
setShowLogin(true);
setUser(null);
return;
}
try {
const userData = JSON.parse(storedUser);
if (!userData) {
setShowLogin(true);
setUser(null);
} else if (!user) {
setUser(userData);
setShowLogin(false); // Hide login on successful user load
}
} catch (error) {
console.error('Error parsing user from sessionStorage:', error);
setShowLogin(true);
setUser(null);
}
if (loading) {
setLoading(false);
}
};
checkUserLoggedIn();
const checkLoginInterval = setInterval(checkUserLoggedIn, 1000);
const handleStorageChange = (e) => {
if (e.key === 'user' && !e.newValue) {
// User was removed from sessionStorage in another tab
setShowLogin(true);
setUser(null);
}
};
window.addEventListener('storage', handleStorageChange);
return () => {
clearInterval(checkLoginInterval);
window.removeEventListener('storage', handleStorageChange);
};
}, [user, loading]);
const handleTabChange = (event, newValue) => {
setTabValue(newValue);
};
const handleGoToOrders = () => {
setTabValue(1);
};
const handleClearPaymentCompletion = () => {
setPaymentCompletion(null);
};
const handleLoginClose = () => {
setShowLogin(false);
// If user closes login without logging in, redirect to home.
const storedUser = sessionStorage.getItem('user');
if (!storedUser) {
navigate('/', { replace: true });
}
}
if (showLogin) {
return ;
}
if (loading) {
return (
);
}
if (!user) {
return ;
}
return (
{window.innerWidth < 600 ?
(tabValue === 0 ? 'Bestellabschluss' :
tabValue === 1 ? 'Bestellungen' :
tabValue === 2 ? 'Einstellungen' : 'Mein Profil')
: 'Mein Profil'
}
{user && (
{user.email}
)}
{tabValue === 0 && }
{tabValue === 1 && }
{tabValue === 2 && }
);
};
// Wrap with socket context
const ProfilePageWithSocket = (props) => {
const {socket,socketB} = useContext(SocketContext);
return ;
};
export default ProfilePageWithSocket;