import React, { useState, useContext, useEffect, useRef } from "react"; import { getAuth, updateProfile, signOut } from "firebase/auth"; import { db } from "../Firebase/FirebaseConfig"; import { ref, uploadBytesResumable, getDownloadURL, getStorage, } from "firebase/storage"; import { useNavigate } from "react-router-dom"; import { Fade } from "react-reveal"; import toast, { Toaster } from "react-hot-toast"; import { AuthContext } from "../Context/UserContext"; import WelcomePageBanner from "../images/WelcomePageBanner.jpg"; import "swiper/css"; import "swiper/css/navigation"; import "swiper/css/pagination"; function Profile() { const { User } = useContext(AuthContext); const [profilePic, setProfilePic] = useState(""); const [newProfielPicURL, setNewProfielPicURL] = useState(""); const [newProfielPic, setNewProfielPic] = useState(""); const [isUserNameChanged, setIsUserNameChanged] = useState(false); const [userName, setUserName] = useState(""); const [isMyListUpdated, setisMyListUpdated] = useState(false); const navigate = useNavigate(); useEffect(() => { if (User != null) { console.log(User.photoURL, "hello"); setProfilePic(User.photoURL); } }, []); const inputRef = useRef(null); const handleClick = () => { inputRef.current.click(); }; function notify() { toast.success(" Data Updated Sucessfuly "); } const handleFileChange = (event) => { const fileObj = event.target.files[0]; setNewProfielPic(fileObj); setNewProfielPicURL(URL.createObjectURL(fileObj)); if (!fileObj) { return; } console.log("fileObj is", fileObj); event.target.value = null; }; const changeUserName = (e) => { e.preventDefault(); if (isUserNameChanged) { if (userName !== "") { const auth = getAuth(); updateProfile(auth.currentUser, { displayName: userName }) .then(() => { notify(); }) .catch((error) => { alert(error.message); }); } else { setIsUserNameChanged(false); } } if (newProfielPic != "") { const storage = getStorage(); const storageRef = ref(storage, `/ProfilePics/${User.uid}`); const uploadTask = uploadBytesResumable(storageRef, newProfielPic); uploadTask.on( "state_changed", (snapshot) => { const prog = Math.round( (snapshot.bytesTransferred / snapshot.totalBytes) * 100 ); }, (error) => { alert(error.message); alert(error.code); }, () => { getDownloadURL(uploadTask.snapshot.ref).then((url) => { console.log(url, "This is the new Url for Profile Pic"); setProfilePic(url); const auth = getAuth(); updateProfile(auth.currentUser, { photoURL: url }) .then(() => { notify(); setisMyListUpdated(true); }) .catch((error) => { alert(error.message); }); }); } ); } }; const updateProfilePic = (imageURL) => { const auth = getAuth(); updateProfile(auth.currentUser, { photoURL: imageURL }) .then(() => { setProfilePic(User.photoURL); notify(); }) .catch((error) => { alert(error.message); }); }; const SignOut = () => { const auth = getAuth(); signOut(auth) .then(() => { navigate("/"); }) .catch((error) => { alert(error.message); }); }; return (