Skip to content

Commit

Permalink
Replace uncalled function with IIFE
Browse files Browse the repository at this point in the history
  • Loading branch information
campb303 committed Apr 20, 2021
1 parent 39f1615 commit 6cd750f
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/components/UserAvatar/UserAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@ import PropTypes from "prop-types";
import { Avatar } from "@material-ui/core";

export default function UserAvatar({ name, alias }) {
const [userImageURL, setUserImageURL] = useState([]);

useEffect(() => {
const getImageURL = (async () => {
const response = await fetch(`https://engineering.purdue.edu/ECN/PersonPhotos/getPhoto?json=1&alias=${alias}`);
if (response.status >= 200 && response.status <= 299) {
const jsonResponse = await response.json();
setUserImageURL(jsonResponse);
} else {
console.log(response.status, response.statusText)
const [userImageURL, setUserImageURL] = useState("");

// Load User Image from ECNDB
useEffect(_ => {
(async function getUserImage() {
if (alias === "") {
return null;
}

const userImageResponse = await fetch(`https://engineering.purdue.edu/ECN/PersonPhotos/getPhoto?json=1&alias=${alias}`);

if (userImageResponse.status !== 200) {
console.error(`Failed to load user image from ECNDB. Got code ${userImageResponse.status} (${userImageResponse.statusText})`);
return null;
}

let userImageData = await userImageResponse.json();
if (userImageData.imagePath === "") {
return null;
}

setUserImageURL(`https://engineering.purdue.edu/${userImageData.imagePath}`);
return null;
})();
}, [alias])

return (
<Avatar src={`https://engineering.purdue.edu/${userImageURL.imagePath}`}>
<Avatar src={userImageURL}>
{name === "" ? null : name.charAt(0)}
</Avatar>
);
Expand Down

0 comments on commit 6cd750f

Please sign in to comment.