diff --git a/Client/src/Components/TabPanels/Account/ProfilePanel.jsx b/Client/src/Components/TabPanels/Account/ProfilePanel.jsx index a9d5a5c81..c19e3ce18 100644 --- a/Client/src/Components/TabPanels/Account/ProfilePanel.jsx +++ b/Client/src/Components/TabPanels/Account/ProfilePanel.jsx @@ -1,5 +1,5 @@ import { useTheme } from "@emotion/react"; -import { useRef, useState } from "react"; +import { useEffect, useRef, useState } from "react"; import TabPanel from "@mui/lab/TabPanel"; import { Box, Divider, Modal, Stack, Typography } from "@mui/material"; import ButtonSpinner from "../../ButtonSpinner"; @@ -16,7 +16,7 @@ import { update } from "../../../Features/Auth/authSlice"; import ImageField from "../../TextFields/Image"; import ImageIcon from "@mui/icons-material/Image"; import ProgressUpload from "../../ProgressBars"; -import { bytesToUrl, checkImage, formatBytes } from "../../../Utils/fileUtils"; +import { bufferTo64, formatBytes } from "../../../Utils/fileUtils"; /** * ProfilePanel component displays a form for editing user profile information @@ -43,7 +43,7 @@ const ProfilePanel = () => { //Disabled for now, will revisit in the future // email: user.email, //TODO - upload picture - profilePicUrl: { src: user.profilePicUrl }, + profileImage: { file: user.profileImage }, }); const [errors, setErrors] = useState({}); const clearError = (err) => { @@ -56,6 +56,26 @@ const ProfilePanel = () => { const [isOpen, setIsOpen] = useState(""); const isModalOpen = (name) => isOpen === name; + //maybe move this to Avatar component? + const [image, setImage] = useState(""); + useEffect(() => { + const fetchImage = async () => { + if ( + localData.profileImage.file && + localData.profileImage.file.contentType + ) { + try { + const base64 = await bufferTo64(localData.profileImage.file.data); + setImage(base64); + } catch (error) { + console.error("Error converting buffer to base64:", error); + } + } + }; + + fetchImage(); + }, [localData.profileImage.file]); + const handleChange = (event) => { const { value, id } = event.target; const name = idToName[id]; @@ -90,11 +110,12 @@ const ProfilePanel = () => { setProgress((prev) => ({ ...prev, isLoading: true })); setLocalData((prev) => ({ ...prev, - profilePicUrl: { + profileImage: { + ...prev.profileImage, name: pic.name, type: pic.type, size: formatBytes(pic.size), - src: "", + toUpload: pic, }, })); @@ -116,13 +137,12 @@ const ProfilePanel = () => { const reader = new FileReader(); reader.onloadend = () => { - const arrayBuffer = reader.result; - const bytes = new Uint8Array(arrayBuffer); + const base64 = reader.result; setLocalData((prev) => ({ ...prev, - profilePicUrl: { - ...prev.profilePicUrl, - src: bytes, + profileImage: { + ...prev.profileImage, + src: base64, }, })); @@ -136,44 +156,61 @@ const ProfilePanel = () => { } return { ...prev, value: prev.value + buffer }; }); - }, 150); + }, 120); }; - reader.readAsArrayBuffer(pic); + reader.readAsDataURL(pic); } }; const removePicture = () => { clearError("picture"); setLocalData((prev) => ({ ...prev, - profilePicUrl: { src: user.profilePicUrl }, + profileImage: { file: "" }, })); - //interrupt interval if modal closes prior to completing the process + //interrupt interval if image upload is canceled prior to completing the process clearInterval(intervalRef.current); setProgress({ value: 0, isLoading: false }); }; const closePictureModal = () => { - removePicture(); + clearError("picture"); + setLocalData((prev) => ({ + ...prev, + profileImage: { file: user.profileImage }, + })); + //interrupt interval if modal closes prior to completing the process + clearInterval(intervalRef.current); + setProgress({ value: 0, isLoading: false }); setIsOpen(""); }; const handleUpdatePicture = () => { setProgress({ value: 0, isLoading: false }); setIsOpen(""); }; + const handleDeletePicture = () => { + setImage(null); + setLocalData((prev) => ({ + ...prev, + profileImage: { file: "", toUpload: "" }, + })); + }; //TODO - implement delete account function const handleDeleteAccount = () => {}; - //TODO - implement save profile function const handleSaveProfile = (event) => { event.preventDefault(); if ( localData.firstname === user.firstname && localData.lastname === user.lastname && - localData.profilePicUrl === user.profilePicUrl + localData.profileImage.toUpload === user.profileImage ) { //TODO - add toast(profile data is unchanged) and maybe disable button return; } - dispatch(update({ authToken, localData })); + const toUpdate = { + ...localData, + profileImage: localData.profileImage.toUpload, + }; + dispatch(update({ authToken, toUpdate })); //TODO - add toast confirmation }; @@ -263,8 +300,10 @@ const ProfilePanel = () => { {