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 } from "../../../Validation/validation"; import { useDispatch, useSelector } from "react-redux"; import { update } from "../../../Features/Auth/authSlice"; /** * 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(false); const [loadingPfp, setLoadingPfp] = 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 - implement delete profile picture function const handleDeletePicture = () => { setLoadingPfp(true); setTimeout(() => { setLoadingPfp(false); }, 2000); }; //TODO - implement update profile function const handleUpdatePicture = () => { setLocalData((prev) => ({ ...prev, profilePicUrl: file })); console.log(localData); }; //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 })); }; 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. {/* TODO - Update placeholder values with redux data */} {/* TODO - modal popup for update pfp? */}