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
+113 -111

No files matched your search

+11 -10
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 { if (user.profileImage.data && user.profileImage.contentType) {
const base64 = await bufferTo64(user.profileImage.data); const url = await bufferToUrl(user.profileImage);
setImage(base64); setImage(url);
} catch (error) {
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,87 +26,81 @@ const ImageField = forwardRef(({ id, src, loading, onChange }, ref) => {
}; };
return ( return (
<Box> <>
<Box {!checkImage(src) || loading ? (
className="image-field-wrapper" <>
mt="20px" <Box
sx={{ className="image-field-wrapper"
display: !checkImage(src) || loading ? "block" : "none", mt="20px"
position: "relative",
height: "fit-content",
border: "dashed",
borderRadius: `${theme.shape.borderRadius}px`,
borderColor: isDragging
? theme.palette.primary.main
: theme.palette.otherColors.graishWhite,
borderWidth: "2px",
transition: "0.2s",
"&:hover": {
borderColor: theme.palette.primary.main,
backgroundColor: "hsl(215, 87%, 51%, 0.05)",
},
}}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
onDrop={handleDragLeave}
>
<TextField
id={id}
type="file"
onChange={onChange}
inputRef={ref}
sx={{
width: "100%",
"& .MuiInputBase-input[type='file']": {
opacity: 0,
cursor: "pointer",
maxWidth: "500px",
minHeight: "175px",
},
"& fieldset": {
padding: 0,
border: "none",
},
}}
/>
<Stack
className="custom-file-text"
alignItems="center"
gap="4px"
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
zIndex: "-1",
width: "100%",
}}
>
<IconButton
sx={{ sx={{
pointerEvents: "none", position: "relative",
height: "fit-content",
border: "dashed",
borderRadius: `${theme.shape.borderRadius}px`, borderRadius: `${theme.shape.borderRadius}px`,
border: `solid ${theme.shape.borderThick}px ${theme.palette.otherColors.graishWhite}`, borderColor: isDragging
boxShadow: theme.shape.boxShadow, ? theme.palette.primary.main
: theme.palette.otherColors.graishWhite,
borderWidth: "2px",
transition: "0.2s",
"&:hover": {
borderColor: theme.palette.primary.main,
backgroundColor: "hsl(215, 87%, 51%, 0.05)",
},
}} }}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
onDrop={handleDragLeave}
> >
<CloudUploadIcon /> <TextField
</IconButton> id={id}
<Typography component="h2"> type="file"
<span>Click to upload</span> or drag and drop onChange={onChange}
</Typography> sx={{
<Typography component="p">(maximum size: 3MB)</Typography> width: "100%",
</Stack> "& .MuiInputBase-input[type='file']": {
</Box> opacity: 0,
<Typography cursor: "pointer",
component="p" maxWidth: "500px",
sx={{ minHeight: "175px",
display: !checkImage(src) || loading ? "block" : "none", },
}} "& fieldset": {
> padding: 0,
Supported formats: JPG, PNG border: "none",
</Typography> },
{checkImage(src) && !loading ? ( }}
/>
<Stack
className="custom-file-text"
alignItems="center"
gap="4px"
sx={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
zIndex: "-1",
width: "100%",
}}
>
<IconButton
sx={{
pointerEvents: "none",
borderRadius: `${theme.shape.borderRadius}px`,
border: `solid ${theme.shape.borderThick}px ${theme.palette.otherColors.graishWhite}`,
boxShadow: theme.shape.boxShadow,
}}
>
<CloudUploadIcon />
</IconButton>
<Typography component="h2">
<span>Click to upload</span> or drag and drop
</Typography>
<Typography component="p">(maximum size: 3MB)</Typography>
</Stack>
</Box>
<Typography component="p">Supported formats: JPG, PNG</Typography>
</>
) : (
<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;
};