diff --git a/src/pages/Profile.js b/src/pages/Profile.js index cedda9932..4bb55cb51 100644 --- a/src/pages/Profile.js +++ b/src/pages/Profile.js @@ -1,187 +1,62 @@ -import { useState, useEffect } from "react"; -import { useNavigate } from "react-router-dom"; -import axiosInstance from "../utils/axiosInstance"; +import React, { useState, useEffect } from "react"; +import { useNavigate } from "react-router-dom"; // Import useNavigate for routing -export default function Profile() { - const [userData, setUserData] = useState(null); - const [error, setError] = useState(""); - const [isEditing, setIsEditing] = useState({ username: false, email: false }); - const [newValues, setNewValues] = useState({ username: "", email: "", password: "" }); - const [isResettingPassword, setIsResettingPassword] = useState(false); // ✅ Added - const [resetMessage, setResetMessage] = useState(""); // ✅ Added +const Profile = () => { const navigate = useNavigate(); - + const [userProfile, setUserProfile] = useState({ + name: "Test User", + email: "test@example.com", + }); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + + // Simulate loading state (mock API call) useEffect(() => { - const fetchUserProfile = async () => { - try { - const response = await axiosInstance.get("user-profile/"); - setUserData(response.data); - setNewValues({ username: response.data.username, email: response.data.email, password: "" }); - } catch (error) { - console.error("Error fetching user profile:", error); - setError("You must be logged in to view this page."); - setTimeout(() => navigate("/login"), 2000); - } - }; - - fetchUserProfile(); - }, [navigate]); - - // ✅ Handle Reset Password Request (Similar to Forgot Password) - const handleResetPassword = async () => { - if (!userData?.email) { - setResetMessage("No email found. Please update your profile."); - return; - } - - setIsResettingPassword(true); // Show loading state - - try { - await axiosInstance.post("forgot-password/", { email: userData.email }); - - setResetMessage("Password reset instructions sent to your email."); - } catch (error) { - console.error("Error requesting password reset:", error); - setResetMessage("Failed to send reset email. Please try again."); - } finally { - setIsResettingPassword(false); // Hide loading state - } - }; - - // ✅ Handle input change - const handleChange = (field, value) => { - setNewValues((prev) => ({ ...prev, [field]: value })); + setLoading(true); + setTimeout(() => { + setLoading(false); + }, 1000); // Simulating API delay + }, []); + + // Handle save changes (navigates back to homepage) + const handleSave = () => { + alert("Profile updated successfully."); + navigate("/"); // Navigate to homepage after saving }; - // ✅ Handle save changes - const handleSave = async () => { - if (!newValues.password) { - alert("Please enter your password to confirm changes."); - return; - } - - try { - const response = await axiosInstance.put("update-profile/", { - username: newValues.username, - email: newValues.email, - password: newValues.password, - }); - - setUserData(response.data); - setIsEditing({ username: false, email: false }); - setNewValues((prev) => ({ ...prev, password: "" })); // Clear password after update - alert("Profile updated successfully!"); - } catch (error) { - console.error("Error updating profile:", error); - alert(error.response?.data?.password || "Failed to update profile. Please try again."); - } - }; + if (loading) { + return
Loading profile...
; + } return ( -
-
-

User Profile

- - {error ? ( -

{error}

- ) : userData ? ( - <> - {/* ✅ Editable Username with input below */} -
-

Username: {userData.username}

- {isEditing.username ? ( - handleChange("username", e.target.value)} - className="border px-2 py-1 rounded w-full mt-2" - /> - ) : null} - -
- - {/* ✅ Editable Email with input below */} -
-

Email: {userData.email || "Not Available"}

- {isEditing.email ? ( - handleChange("email", e.target.value)} - className="border px-2 py-1 rounded w-full mt-2" - /> - ) : null} - -
+
+

User Profile

- {/* ✅ Password Confirmation Field (Required before saving) */} - {(isEditing.username || isEditing.email) && ( -
-

Enter Password to Confirm:

- handleChange("password", e.target.value)} - className="border px-2 py-1 rounded w-full mt-2" - placeholder="Enter your password" - /> -
- )} + {error &&

{error}

} - {/* ✅ Save Changes Button (Only appears if editing) */} - {(isEditing.username || isEditing.email) && ( - - )} - - {/* ✅ Action Buttons (Swapped Positions) */} -
- {/* ✅ Reset Password Button (Now on the Left) */} - - - {/* ✅ Homepage Link (Now on the Right) */} - -
+
+

Name: {userProfile.name}

+

Email: {userProfile.email}

+
- {/* ✅ Show Reset Message Below Buttons (Only If Exists) */} - {resetMessage && ( -

{resetMessage}

- )} - - ) : ( -

Loading profile...

- )} +
+ {/* Link to Forgot Password page */} + + +
); -} +}; + +export default Profile;