import { useState, useEffect } from "react"; import { User, Lock, Trash2, History, Shield } from "lucide-react"; import FloatingAlert from "../components/FloatingAlert"; // adjust path if needed const Settings = () => { const [userData, setUserData] = useState({ userId: "", name: "", email: "", phone: "", UCID: "", address: "", password: "", }); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [showAlert, setShowAlert] = useState(false); const storedUser = JSON.parse(sessionStorage.getItem("user")); // Fetch user data when component mounts useEffect(() => { const fetchUserData = async () => { try { setIsLoading(true); setError(null); // Get the user's data from localStorage const storedUser = JSON.parse(sessionStorage.getItem("user")); console.log(storedUser); if (!storedUser || !storedUser.email) { throw new Error("User details not found. Please log in again."); } // Make API call to fetch user data const response = await fetch( "http://localhost:3030/api/user/find_user", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ email: storedUser.email, }), }, ); const data = await response.json(); console.log(data); if (data.found) { // Update state with fetched data setUserData((prevData) => ({ ...prevData, userId: storedUser.ID, // Try both sources name: data.name || storedUser.name || "", email: data.email || storedUser.email || "", UCID: data.UCID || storedUser.UCID || "", phone: data.phone || storedUser.phone || "", address: data.address || storedUser.address || "", password: data.password, })); } else { throw new Error(data.error || "Failed to retrieve user data"); } } catch (error) { console.error("Error fetching user data:", error); setError( error.message || "An error occurred while loading your profile", ); } finally { setIsLoading(false); } }; fetchUserData(); }, []); const handleInputChange = (e) => { const { id, value } = e.target; setUserData((prevData) => ({ ...prevData, [id]: value, })); }; const removeHistory = async () => { const response = await fetch( "http://localhost:3030/api/history/delHistory", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userID: storedUser.ID, }), }, ); if (response.ok) { setShowAlert(true); } }; const handleUpdateProfile = async () => { try { // Ensure userId is present if (!userData.userId) { throw new Error("User ID is missing. Unable to update profile."); } setIsLoading(true); setError(null); const response = await fetch("http://localhost:3030/api/user/update", { method: "POST", // or "PUT" if your backend supports it headers: { "Content-Type": "application/json", }, body: JSON.stringify(userData), }); const result = await response.json(); if (!response.ok) { throw new Error(result.error || "Failed to update profile"); } console.log("Profile updated successfully:", result); alert("Profile updated successfully!"); } catch (error) { console.error("Error updating profile:", error); setError( error.message || "An error occurred while updating your profile.", ); } finally { setIsLoading(false); } }; const handleDeleteAccount = async () => { if ( window.confirm( "Are you sure you want to delete your account? This action cannot be undone.", ) ) { try { // Get user ID from state or localStorage const storedUser = JSON.parse(sessionStorage.getItem("user")); const userId = storedUser.ID; if (!userId) { throw new Error("User ID not found"); } // Make API call to delete the account const response = await fetch("http://localhost:3030/api/user/delete", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userId: userId, }), }); const data = await response.json(); if (data.success) { alert("Account deleted successfully. You will be logged out."); // Clear user data from localStorage and redirect to login sessionStorage.removeItem("user"); sessionStorage.removeItem("isAuthenticated"); window.location.href = "/login"; } else { throw new Error(data.error || "Failed to delete account"); } } catch (error) { console.error("Error deleting account:", error); alert("Cannot delete account, Please logout and retry:"); } } }; // Show loading state if (isLoading) { return (
Delete all your history on Market
Once you delete your account, there is no going back. Please be certain.