import { useState, useEffect } from "react"; import { User, Lock, Trash2, History, Search, Shield } from "lucide-react"; const Settings = () => { const [userData, setUserData] = useState({ userId: "", name: "", email: "", phone: "", UCID: "", address: "", currentPassword: "", newPassword: "", confirmPassword: "", }); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); // 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/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: data.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 || "", // Reset password fields currentPassword: "", newPassword: "", confirmPassword: "", })); } 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 handleProfileUpdate = async (e) => { e.preventDefault(); try { // TODO: Implement the actual update API call console.log("Profile updated:", userData); // Update localStorage with new user data const storedUser = JSON.parse(localStorage.getItem("user")); const updatedUser = { ...storedUser, name: userData.name, phone: userData.phone, UCID: userData.UCID, address: userData.address, }; localStorage.setItem("user", JSON.stringify(updatedUser)); alert("Profile updated successfully!"); } catch (error) { console.error("Error updating profile:", error); alert("Failed to update profile: " + error.message); } }; const handlePasswordUpdate = async (e) => { e.preventDefault(); try { // Validate passwords match if (userData.newPassword !== userData.confirmPassword) { alert("New passwords do not match!"); return; } // TODO: Implement the actual password update API call console.log("Password updated"); // Update password in localStorage const storedUser = JSON.parse(localStorage.getItem("user")); const updatedUser = { ...storedUser, password: userData.newPassword, }; localStorage.setItem("user", JSON.stringify(updatedUser)); // Reset password fields setUserData((prevData) => ({ ...prevData, currentPassword: "", newPassword: "", confirmPassword: "", })); alert("Password updated successfully!"); } catch (error) { console.error("Error updating password:", error); alert("Failed to update password: " + error.message); } }; const handleDeleteHistory = (type) => { // TODO: Delete the specified history console.log(`Deleting ${type} history`); alert(`${type} history deleted successfully!`); }; 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/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("Failed to delete account: " + error.message); } } }; // Show loading state if (isLoading) { return (
Delete all your search history on StudentMarket
Delete all your browsing history on StudentMarket
Once you delete your account, there is no going back. Please be certain.