import { useTheme } from "@emotion/react"; import { useState } from "react"; import TabPanel from "@mui/lab/TabPanel"; import { Box, Divider, Modal, Stack, Typography } from "@mui/material"; import ButtonSpinner from "../../ButtonSpinner"; import Button from "../../Button"; import EmailTextField from "../../TextFields/Email/EmailTextField"; import StringTextField from "../../TextFields/Text/TextField"; import Avatar from "../../Avatar"; import { editProfileValidation, imageValidation, } from "../../../Validation/validation"; import { useDispatch, useSelector } from "react-redux"; import { update } from "../../../Features/Auth/authSlice"; import ImageField from "../../TextFields/Image"; import ImageIcon from "@mui/icons-material/Image"; import ProgressUpload from "../../ProgressBars"; /** * ProfilePanel component displays a form for editing user profile information * and allows for actions like updating profile picture, credentials, * and deleting account. * * @returns {JSX.Element} */ const ProfilePanel = () => { const theme = useTheme(); const dispatch = useDispatch(); const { user, authToken, isLoading } = useSelector((state) => state.auth); const idToName = { "edit-first-name": "firstname", "edit-last-name": "lastname", //Disabled for now, will revisit in the future // "edit-email": "email", }; const [localData, setLocalData] = useState({ firstname: user.firstname, lastname: user.lastname, //Disabled for now, will revisit in the future // email: user.email, //TODO - upload picture profilePicUrl: "placeholder", }); const [errors, setErrors] = useState({}); const [isOpen, setIsOpen] = useState(""); const isModalOpen = (name) => isOpen === name; const [loading, setLoading] = useState(false); const handleChange = (event) => { const { value, id } = event.target; const name = idToName[id]; setLocalData((prev) => ({ ...prev, [name]: value, })); const validation = editProfileValidation.validate( { [name]: value }, { abortEarly: false } ); setErrors((prev) => { const updatedErrors = { ...prev }; if (validation.error) { updatedErrors[name] = validation.error.details[0].message; } else { delete updatedErrors[name]; } return updatedErrors; }); }; //TODO - add setPicture state to localData const [picture, setPicture] = useState(); const [uploadProgress, setUploadProgress] = useState(0); const [isUploading, setIsUploading] = useState(false); const handlePicture = (event) => { clearError("picture"); const pic = event.target.files[0]; console.log(pic); //TODO - add setPicture state to localData setPicture({ name: pic.name, type: pic.type, size: formatBytes(pic.size) }); setIsUploading(true); const { error } = imageValidation.validate( { type: pic.type, size: pic.size, }, { abortEarly: false } ); if (error) { setErrors((prev) => { const updatedErrors = { ...prev }; updatedErrors["picture"] = error.details[0].message; return updatedErrors; }); return; } setTimeout(() => { //TODO - add setPicture state to localData setPicture((prev) => ({ ...prev, src: URL.createObjectURL(pic) })); }, 1500); let progress = 0; const interval = setInterval(() => { progress += 10; setUploadProgress(progress); if (progress >= 100) { clearInterval(interval); } }, 150); }; const formatBytes = (bytes) => { if (bytes === 0) return "0 Bytes"; const megabytes = bytes / (1024 * 1024); return megabytes.toFixed(2) + " MB"; }; const clearError = (err) => { setErrors((prev) => { const updatedErrors = { ...prev }; if (updatedErrors[err]) delete updatedErrors[err]; return updatedErrors; }); }; const handleCancelUpload = () => { //TODO - add setPicture state to localData setPicture(); setUploadProgress(0); setIsUploading(false); clearError("picture"); }; const handleClosePictureModal = () => { setIsOpen(""); setUploadProgress(0); setIsUploading(false); clearError("picture"); }; //TODO - revisit once localData is set up properly const handleDeletePicture = () => { setLoading(true); setTimeout(() => { //TODO - add setPicture state to localData setPicture(null); setLoading(false); }, 2000); }; //TODO - implement update profile function const handleUpdatePicture = () => { setIsOpen(""); setUploadProgress(0); setIsUploading(false); }; //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 ) { //TODO - add toast(profile data is unchanged) and maybe disable button return; } dispatch(update({ authToken, localData })); //TODO - add toast confirmation }; return (
First name {errors[idToName["edit-first-name"]] ? ( {errors[idToName["edit-first-name"]]} ) : ( "" )}
Last name {errors[idToName["edit-last-name"]] ? ( {errors[idToName["edit-last-name"]]} ) : ( "" )}
Email This is your current email address — it cannot be changed. {errors[idToName["edit-email"]] ? ( {errors[idToName["edit-email"]]} ) : ( "" )}
Your photo This photo will be displayed in your profile page.