EduNes Logo

Less Stress ↓

More Success ↑

EduNes means


Educational Network for Excellence and Success

EduNes Students

EDIT PROFILE CODE

import React, { useState, useEffect } from "react"; import { useUser } from "../context/UserContext"; import { updateProfile } from "firebase/auth"; import { doc, getDoc, setDoc } from "firebase/firestore"; import { auth, db } from "../firebase"; import "../styles/EditProfile.css"; const EditProfile = () => { const { user } = useUser(); const [formData, setFormData] = useState({ firstName: "", lastName: "", accountType: "Free User", class: "", institution: "", city: "", about: "", email: user?.email || "", phone: "", password: "", photo: null, }); const [loading, setLoading] = useState(true); useEffect(() => { const fetchUserData = async () => { if (!user?.uid) return; try { const userRef = doc(db, "users", user.uid); const userSnap = await getDoc(userRef); if (userSnap.exists()) { setFormData((prev) => ({ ...prev, ...userSnap.data(), })); } setLoading(false); } catch (error) { console.error("Error fetching user data:", error); setLoading(false); } }; fetchUserData(); }, [user?.uid]); const handleChange = (e) => { const { name, value } = e.target; setFormData((prev) => ({ ...prev, [name]: value, })); }; const handleImageUpload = (e) => { const file = e.target.files[0]; if (file) { setFormData((prev) => ({ ...prev, photo: URL.createObjectURL(file), // preview only })); } }; const handleUpgrade = () => { alert("Upgrade functionality will be added here!"); }; const handleSave = async () => { try { await updateProfile(auth.currentUser, { displayName: `${formData.firstName} ${formData.lastName}`, photoURL: formData.photo?.startsWith("blob:") ? user.photoURL || auth.currentUser?.photoURL : formData.photo || auth.currentUser?.photoURL, }); const userRef = doc(db, "users", user.uid); await setDoc( userRef, { firstName: formData.firstName, lastName: formData.lastName, class: formData.class, institution: formData.institution, city: formData.city, about: formData.about, phone: formData.phone, email: formData.email, updatedAt: new Date(), }, { merge: true } ); alert("Profile updated successfully!"); } catch (error) { console.error("Error saving profile:", error); alert("Failed to update profile. Please try again."); } }; if (loading) { return

Loading profile...

; } return (

Edit your profile

{/* Left Column: Form */}
Account Type: {formData.accountType} Upgrade