import React, { useState, useEffect } from 'react'; import { useFilePicker, utils } from 'react-sage'; import { makeStyles } from '@material-ui/core/styles'; import CloudUploadIcon from '@material-ui/icons/CloudUpload'; import { CardActionArea, CardMedia, Typography } from '@material-ui/core'; import CancelOutlinedIcon from '@material-ui/icons/CancelOutlined'; interface PropTypes { file: File | undefined; setFile: (file: File | undefined) => void; } const useStyles = makeStyles({ root: { display: 'flex', justifyContent: 'center', flexDirection: 'column', alignItems: 'center' }, clearIcon: { opacity: '.5', fontSize: 50 }, media: { height: '100%', width: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center' }, text: { textAlign: 'center' } }); const PollSubmissionImage: React.FC = ({ file, setFile }) => { const classes = useStyles(); const { files, onClick, HiddenFileInput } = useFilePicker(); const [url, setUrl] = useState(); const [isMediaHover, setIsMediaHover] = useState(false); const handleMouseEnter = (): void => { setIsMediaHover(true); }; const handleMouseLeave = (): void => { setIsMediaHover(false); }; useEffect(() => { if (files?.length) { const chosenFile = files[0]; setFile(chosenFile); utils.loadFile(chosenFile).then(result => setUrl(result)); } }, [files, setFile]); const handleClick = () => { if (file) { setFile(undefined); } else onClick(); }; const Upload = ( <> Upload an image ); const Media = ( {isMediaHover && } ); return ( <> {file ? (url && Media) : Upload} ); }; export default PollSubmissionImage;