Removed ref

This commit is contained in:
Daniel Cojocea committed 2024-07-05 18:48:32 -04:00
1 parent d7c1972c99
commit 6c645ba500
4 files changed
+47 -45

No files matched your search

+10 -9
View File
@@ -2,7 +2,7 @@ import { Avatar as MuiAvatar } from "@mui/material";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { useSelector } from "react-redux"; import { useSelector } from "react-redux";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { bufferTo64 } from "../../Utils/fileUtils"; import { bufferTo64, bufferToUrl } from "../../Utils/fileUtils";
/** /**
* @component * @component
@@ -24,24 +24,25 @@ const Avatar = ({ src, small, sx }) => {
const [image, setImage] = useState(); const [image, setImage] = useState();
useEffect(() => { useEffect(() => {
const fetchImage = async () => { const fetchData = async () => {
if (user.profileImage.data && user.profileImage.contentType) {
try { try {
const base64 = await bufferTo64(user.profileImage.data); if (user.profileImage.data && user.profileImage.contentType) {
setImage(base64); const url = await bufferToUrl(user.profileImage);
} catch (error) { setImage(url);
console.error("Error converting buffer to base64:", error);
} }
} catch (error) {
console.error("Error converting buffer to URL: ", error);
} }
}; };
fetchImage();
fetchData();
}, [user]); }, [user]);
return ( return (
<MuiAvatar <MuiAvatar
alt={`${user?.firstname} ${user?.lastname}`} alt={`${user?.firstname} ${user?.lastname}`}
src={ src={
src === null src === "placeholder"
? "/static/images/avatar/2.jpg" ? "/static/images/avatar/2.jpg"
: src : src
? src ? src
@@ -54,9 +54,6 @@ const ProfilePanel = () => {
}); });
}; };
const [isOpen, setIsOpen] = useState("");
const isModalOpen = (name) => isOpen === name;
const handleChange = (event) => { const handleChange = (event) => {
const { value, id } = event.target; const { value, id } = event.target;
const name = idToName[id]; const name = idToName[id];
@@ -68,9 +65,8 @@ const ProfilePanel = () => {
validateField({ [name]: value }, editProfileValidation, name); validateField({ [name]: value }, editProfileValidation, name);
}; };
const fileRef = useRef(); const [file, setFile] = useState();
const intervalRef = useRef(null); const intervalRef = useRef(null);
const [src, setSrc] = useState();
const [progress, setProgress] = useState({ value: 0, isLoading: false }); const [progress, setProgress] = useState({ value: 0, isLoading: false });
const handlePicture = (event) => { const handlePicture = (event) => {
const pic = event.target.files[0]; const pic = event.target.files[0];
@@ -81,7 +77,11 @@ const ProfilePanel = () => {
if (error) return; if (error) return;
setProgress((prev) => ({ ...prev, isLoading: true })); 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 //TODO - potentitally remove once image compression functions are implemented above
intervalRef.current = setInterval(() => { intervalRef.current = setInterval(() => {
@@ -109,11 +109,7 @@ const ProfilePanel = () => {
const removePicture = () => { const removePicture = () => {
clearError("picture"); clearError("picture");
if (fileRef.current && fileRef.current.files[0]) { setFile({ src: "placeholder" });
fileRef.current.value = "";
URL.revokeObjectURL(fileRef.current?.files[0]);
setSrc();
}
//interrupt interval if image upload is canceled prior to completing the process //interrupt interval if image upload is canceled prior to completing the process
clearInterval(intervalRef.current); clearInterval(intervalRef.current);
setProgress({ value: 0, isLoading: false }); setProgress({ value: 0, isLoading: false });
@@ -127,16 +123,16 @@ const ProfilePanel = () => {
setProgress({ value: 0, isLoading: false }); setProgress({ value: 0, isLoading: false });
setLocalData((prev) => ({ setLocalData((prev) => ({
...prev, ...prev,
profileImage: fileRef.current?.files[0], profileImage: null,
})); }));
setIsOpen(""); setIsOpen("");
}; };
const handleDeletePicture = () => { const handleDeletePicture = () => {
setFile({ src: "placeholder" });
setLocalData((prev) => ({ setLocalData((prev) => ({
...prev, ...prev,
profileImage: null, profileImage: null,
})); }));
setSrc(null);
}; };
//TODO - implement delete account function //TODO - implement delete account function
@@ -156,6 +152,12 @@ const ProfilePanel = () => {
//TODO - add toast confirmation //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 ( return (
<TabPanel value="profile"> <TabPanel value="profile">
<form className="edit-profile-form" noValidate spellCheck="false"> <form className="edit-profile-form" noValidate spellCheck="false">
@@ -240,7 +242,7 @@ const ProfilePanel = () => {
</Typography> </Typography>
</Stack> </Stack>
<Stack className="row-stack" direction="row" alignItems="center"> <Stack className="row-stack" direction="row" alignItems="center">
<Avatar src={src} sx={{ mr: "8px" }} /> <Avatar src={file?.src} sx={{ mr: "8px" }} />
<Button <Button
level="tertiary" level="tertiary"
label="Delete" label="Delete"
@@ -376,16 +378,15 @@ const ProfilePanel = () => {
</Typography> </Typography>
<ImageField <ImageField
id="update-profile-picture" id="update-profile-picture"
src={src} src={file?.src}
loading={progress.isLoading && progress.value !== 100} loading={progress.isLoading && progress.value !== 100}
onChange={handlePicture} onChange={handlePicture}
ref={fileRef}
/> />
{progress.isLoading || progress.value !== 0 || errors["picture"] ? ( {progress.isLoading || progress.value !== 0 || errors["picture"] ? (
<ProgressUpload <ProgressUpload
icon={<ImageIcon />} icon={<ImageIcon />}
label={fileRef.current?.files[0]?.name} label={file.name}
size={formatBytes(fileRef.current?.files[0]?.size)} size={file.size}
progress={progress.value} progress={progress.value}
onClick={removePicture} onClick={removePicture}
error={errors["picture"]} error={errors["picture"]}
@@ -1,4 +1,4 @@
import React, { useState, forwardRef } from "react"; import React, { useState } from "react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { Box, IconButton, Stack, TextField, Typography } from "@mui/material"; import { Box, IconButton, Stack, TextField, Typography } from "@mui/material";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
@@ -14,7 +14,7 @@ import { checkImage } from "../../../Utils/fileUtils";
* @returns {JSX.Element} The rendered component. * @returns {JSX.Element} The rendered component.
*/ */
const ImageField = forwardRef(({ id, src, loading, onChange }, ref) => { const ImageField = ({ id, src, loading, onChange }) => {
const theme = useTheme(); const theme = useTheme();
const [isDragging, setIsDragging] = useState(false); const [isDragging, setIsDragging] = useState(false);
@@ -26,12 +26,13 @@ const ImageField = forwardRef(({ id, src, loading, onChange }, ref) => {
}; };
return ( return (
<Box> <>
{!checkImage(src) || loading ? (
<>
<Box <Box
className="image-field-wrapper" className="image-field-wrapper"
mt="20px" mt="20px"
sx={{ sx={{
display: !checkImage(src) || loading ? "block" : "none",
position: "relative", position: "relative",
height: "fit-content", height: "fit-content",
border: "dashed", border: "dashed",
@@ -54,7 +55,6 @@ const ImageField = forwardRef(({ id, src, loading, onChange }, ref) => {
id={id} id={id}
type="file" type="file"
onChange={onChange} onChange={onChange}
inputRef={ref}
sx={{ sx={{
width: "100%", width: "100%",
"& .MuiInputBase-input[type='file']": { "& .MuiInputBase-input[type='file']": {
@@ -98,15 +98,9 @@ const ImageField = forwardRef(({ id, src, loading, onChange }, ref) => {
<Typography component="p">(maximum size: 3MB)</Typography> <Typography component="p">(maximum size: 3MB)</Typography>
</Stack> </Stack>
</Box> </Box>
<Typography <Typography component="p">Supported formats: JPG, PNG</Typography>
component="p" </>
sx={{ ) : (
display: !checkImage(src) || loading ? "block" : "none",
}}
>
Supported formats: JPG, PNG
</Typography>
{checkImage(src) && !loading ? (
<Stack direction="row" justifyContent="center"> <Stack direction="row" justifyContent="center">
<Box <Box
sx={{ sx={{
@@ -119,12 +113,10 @@ const ImageField = forwardRef(({ id, src, loading, onChange }, ref) => {
}} }}
></Box> ></Box>
</Stack> </Stack>
) : (
""
)} )}
</Box> </>
); );
}); };
ImageField.propTypes = { ImageField.propTypes = {
id: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
+8
View File
@@ -21,3 +21,11 @@ export const bufferTo64 = (bufferData) => {
reader.readAsDataURL(blob); reader.readAsDataURL(blob);
}); });
}; };
export const bufferToUrl = (buffer) => {
console.log(buffer);
const blob = new Blob([buffer.data], { type: buffer.contentType });
const url = URL.createObjectURL(blob);
return url;
};