diff --git a/Client/src/Components/Avatar/index.jsx b/Client/src/Components/Avatar/index.jsx index 5b7734b7b..16a93bf18 100644 --- a/Client/src/Components/Avatar/index.jsx +++ b/Client/src/Components/Avatar/index.jsx @@ -2,7 +2,7 @@ import { Avatar as MuiAvatar } from "@mui/material"; import PropTypes from "prop-types"; import { useSelector } from "react-redux"; import { useEffect, useState } from "react"; -import { bufferTo64 } from "../../Utils/fileUtils"; +import { bufferTo64, bufferToUrl } from "../../Utils/fileUtils"; /** * @component @@ -24,24 +24,25 @@ const Avatar = ({ src, small, sx }) => { const [image, setImage] = useState(); useEffect(() => { - const fetchImage = async () => { - if (user.profileImage.data && user.profileImage.contentType) { - try { - const base64 = await bufferTo64(user.profileImage.data); - setImage(base64); - } catch (error) { - console.error("Error converting buffer to base64:", error); + const fetchData = async () => { + try { + if (user.profileImage.data && user.profileImage.contentType) { + const url = await bufferToUrl(user.profileImage); + setImage(url); } + } catch (error) { + console.error("Error converting buffer to URL: ", error); } }; - fetchImage(); + + fetchData(); }, [user]); return ( { }); }; - const [isOpen, setIsOpen] = useState(""); - const isModalOpen = (name) => isOpen === name; - const handleChange = (event) => { const { value, id } = event.target; const name = idToName[id]; @@ -68,9 +65,8 @@ const ProfilePanel = () => { validateField({ [name]: value }, editProfileValidation, name); }; - const fileRef = useRef(); + const [file, setFile] = useState(); const intervalRef = useRef(null); - const [src, setSrc] = useState(); const [progress, setProgress] = useState({ value: 0, isLoading: false }); const handlePicture = (event) => { const pic = event.target.files[0]; @@ -81,7 +77,11 @@ const ProfilePanel = () => { if (error) return; setProgress((prev) => ({ ...prev, isLoading: true })); - setSrc(URL.createObjectURL(fileRef.current.files[0])); + setFile({ + src: URL.createObjectURL(pic), + name: pic.name, + size: formatBytes(pic.size), + }); //TODO - potentitally remove once image compression functions are implemented above intervalRef.current = setInterval(() => { @@ -109,11 +109,7 @@ const ProfilePanel = () => { const removePicture = () => { clearError("picture"); - if (fileRef.current && fileRef.current.files[0]) { - fileRef.current.value = ""; - URL.revokeObjectURL(fileRef.current?.files[0]); - setSrc(); - } + setFile({ src: "placeholder" }); //interrupt interval if image upload is canceled prior to completing the process clearInterval(intervalRef.current); setProgress({ value: 0, isLoading: false }); @@ -127,16 +123,16 @@ const ProfilePanel = () => { setProgress({ value: 0, isLoading: false }); setLocalData((prev) => ({ ...prev, - profileImage: fileRef.current?.files[0], + profileImage: null, })); setIsOpen(""); }; const handleDeletePicture = () => { + setFile({ src: "placeholder" }); setLocalData((prev) => ({ ...prev, profileImage: null, })); - setSrc(null); }; //TODO - implement delete account function @@ -156,6 +152,12 @@ const ProfilePanel = () => { //TODO - add toast confirmation }; + //modal controllers + const [isOpen, setIsOpen] = useState(""); + const isModalOpen = (name) => isOpen === name; + + // console.log(document.querySelector('input[type="file"]')?.files[0]); + return (
@@ -240,7 +242,7 @@ const ProfilePanel = () => { - +