home and selligns page polished

This commit is contained in:
Mann Patel
2025-04-20 21:42:34 -06:00
parent bcf849611f
commit 77a35810fd
5 changed files with 667 additions and 552 deletions

View File

@@ -12,7 +12,7 @@ import Selling from "./pages/Selling";
import Transactions from "./pages/Transactions";
import Favorites from "./pages/Favorites";
import ProductDetail from "./pages/ProductDetail";
import SearchPage from "./pages/SearchPage"; // Make sure to import the SearchPage
import SearchPage from "./pages/SearchPage";
function App() {
// Authentication state - initialize from localStorage if available
@@ -30,6 +30,11 @@ function App() {
const [error, setError] = useState("");
const [isLoading, setIsLoading] = useState(false);
// Product recommendation states
const [isGeneratingRecommendations, setIsGeneratingRecommendations] =
useState(false);
const [recommendations, setRecommendations] = useState([]);
// New verification states
const [verificationStep, setVerificationStep] = useState("initial"); // 'initial', 'code-sent', 'verifying'
const [tempUserData, setTempUserData] = useState(null);
@@ -51,8 +56,69 @@ function App() {
}, []);
useEffect(() => {
sendSessionDataToServer();
}, []);
if (isAuthenticated && user) {
sendSessionDataToServer();
}
}, [isAuthenticated, user]);
// Generate product recommendations when user logs in
useEffect(() => {
if (isAuthenticated && user) {
generateProductRecommendations();
}
}, [isAuthenticated, user]);
// Generate product recommendations
const generateProductRecommendations = async () => {
try {
setIsGeneratingRecommendations(true);
// Add a short delay to simulate calculation time
await new Promise((resolve) => setTimeout(resolve, 500));
console.log("Generating product recommendations for user:", user.ID);
// Make API call to get recommendations
const response = await fetch(
"http://localhost:3030/api/recommendations/generate",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
userId: user.ID,
}),
},
);
if (!response.ok) {
throw new Error("Failed to generate recommendations");
}
const result = await response.json();
if (result.success) {
console.log(
"Recommendations generated successfully:",
result.recommendations,
);
setRecommendations(result.recommendations);
// Store recommendations in session storage for access across the app
sessionStorage.setItem(
"userRecommendations",
JSON.stringify(result.recommendations),
);
} else {
console.error("Error generating recommendations:", result.message);
}
} catch (err) {
console.error("Error generating product recommendations:", err);
} finally {
setIsGeneratingRecommendations(false);
}
};
// Send verification code
const sendVerificationCode = async (userData) => {
@@ -180,6 +246,7 @@ function App() {
if (result.success) {
// Create user object from API response
const newUser = {
ID: result.userID || result.ID,
name: result.name || userData.name,
email: result.email || userData.email,
UCID: result.UCID || userData.ucid,
@@ -194,13 +261,17 @@ function App() {
sessionStorage.setItem("user", JSON.stringify(newUser));
// After successful signup, send session data to server
sendSessionDataToServer(); // Call it after signup
sendSessionDataToServer();
// Reset verification steps
setVerificationStep("initial");
setTempUserData(null);
console.log("Signup completed successfully");
// Generate recommendations for the new user
generateProductRecommendations();
return true;
} else {
setError(result.message || "Failed to complete signup");
@@ -299,9 +370,11 @@ function App() {
sessionStorage.setItem("isAuthenticated", "true");
sessionStorage.setItem("user", JSON.stringify(userObj));
sessionStorage.getItem("user");
console.log("Login successful for:", userData.email);
// Start generating recommendations with a slight delay
// This will happen in the useEffect, but we set a loading state to show to the user
setIsGeneratingRecommendations(true);
} else {
// Show error message for invalid credentials
setError("Invalid email or password");
@@ -335,11 +408,12 @@ function App() {
setUser(null);
setVerificationStep("initial");
setTempUserData(null);
setRecommendations([]);
// Clear localStorage
//
sessionStorage.removeItem("user");
sessionStorage.removeItem("isAuthenticated");
sessionStorage.removeItem("userRecommendations");
console.log("User logged out");
};
@@ -367,8 +441,6 @@ function App() {
try {
// Retrieve data from sessionStorage
const user = JSON.parse(sessionStorage.getItem("user"));
// const isAuthenticated =
// sessionStorage.getItem("isAuthenticated") === "true";
if (!user || !isAuthenticated) {
console.log("User is not authenticated");
@@ -403,6 +475,13 @@ function App() {
}
};
// Loading overlay component
const LoadingOverlay = () => (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="animate-spin rounded-full h-12 w-12 border-4 border-green-500 border-t-transparent"></div>
</div>
);
// Login component
const LoginComponent = () => (
<div className="flex h-screen bg-white">
@@ -671,6 +750,9 @@ function App() {
return (
<Router>
<div className="min-h-screen bg-gray-50">
{/* Show loading overlay when generating recommendations */}
{isGeneratingRecommendations && <LoadingOverlay />}
{/* Only show navbar when authenticated */}
{isAuthenticated && (
<Navbar onLogout={handleLogout} userName={user?.name} />
@@ -687,7 +769,7 @@ function App() {
element={
<ProtectedRoute>
<div className="container mx-auto px-4 py-6">
<Home />
<Home recommendations={recommendations} />
</div>
</ProtectedRoute>
}