Refactored some of the code

This commit is contained in:
Daniel Cojocea committed 2024-07-04 20:47:23 -04:00
1 parent bef0ad797b
commit 5807990342
7 files changed
+100 -148

No files matched your search

+42 -13
View File
@@ -1,12 +1,13 @@
import { Avatar as MuiAvatar } from "@mui/material";
import PropTypes from "prop-types";
import { useSelector } from "react-redux";
import { useEffect, useState } from "react";
import { bufferTo64 } from "../../Utils/fileUtils";
/**
* @component
* @param {Object} props
* @param {string} props.src - Path to image for avatar
* @param {string} props.firstName - The users first name
* @param {string} props.lastName - The users last name
* @param {boolean} props.small - Specifies if avatar should be large
* @param {Object} [props.sx] - Additional styles to apply to the button.
* @returns {JSX.Element}
@@ -15,29 +16,57 @@ import PropTypes from "prop-types";
* <Avatar src="assets/img" first="Alex" last="Holliday" small />
*/
const Avatar = ({ src, firstName, lastName, small, sx }) => {
const borderColor = "#F0F2F4";
const smallStyle = small ? { width: 32, height: 32 } : {};
const Avatar = ({ src, small, sx }) => {
const { user } = useSelector((state) => state.auth);
const style = small ? { width: 25, height: 25 } : { width: 64, height: 64 };
const border = small ? 1 : 3;
const [image, setImage] = useState();
useEffect(() => {
const fetchImage = async () => {
if (user.profileImage.data && user.profileImage.contentType) {
try {
const base64 = await bufferTo64(user.profileImage.data);
setImage(base64);
} catch (error) {
console.error("Error converting buffer to base64:", error);
}
}
};
fetchImage();
}, [user]);
return (
<MuiAvatar
alt={`${firstName} ${lastName}`}
src={src}
alt={`${user?.firstname} ${user?.lastname}`}
src={src ? src : user?.profileImage ? image : ""}
sx={{
fontSize: small ? "12px" : "20px",
display: "inline-flex",
border: `0.2rem solid ${borderColor}`,
...smallStyle,
...sx
"&::before": {
content: `""`,
position: "absolute",
top: "0",
left: "0",
width: `calc(100% - ${border * 2}px)`,
height: `calc(100% - ${border * 2}px)`,
border: `${border}px solid rgba(255,255,255,0.2)`,
borderRadius: "50%",
},
...style,
...sx,
}}
>
{src === undefined ? `${firstName[0]}${lastName[0]}` : null}
{src === undefined
? `${user.firstname?.charAt(0)}${user.lastname?.charAt(0)}`
: null}
</MuiAvatar>
);
};
Avatar.propTypes = {
src: PropTypes.string,
firstName: PropTypes.string.isRequired,
lastName: PropTypes.string.isRequired,
small: PropTypes.bool,
sx: PropTypes.object,
};