diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-13 17:27:32 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-13 17:27:32 +0300 |
commit | d1e0dcd8538a61184eca50fbf7769c6d2943ff6b (patch) | |
tree | 9c2ba42d34e469d292fc1fe807e3f814a872a69e /src/containers/PollCreation/PollCreationImage.tsx | |
parent | 2dc5fc00347256982136deea98d483c444002595 (diff) | |
parent | 52799ec4e4cd5801423ee0d2aa56039c061afdb4 (diff) | |
download | which-ui-d1e0dcd8538a61184eca50fbf7769c6d2943ff6b.tar.gz |
Merge pull request #78 from which-ecosystem/redesign
Move PollSubmission to separate page and add FAB
Diffstat (limited to 'src/containers/PollCreation/PollCreationImage.tsx')
-rw-r--r-- | src/containers/PollCreation/PollCreationImage.tsx | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx new file mode 100644 index 0000000..d3203a6 --- /dev/null +++ b/src/containers/PollCreation/PollCreationImage.tsx @@ -0,0 +1,95 @@ +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 PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => { + const classes = useStyles(); + const { files, onClick, HiddenFileInput } = useFilePicker(); + const [url, setUrl] = useState<string>(); + 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 = ( + <> + <CloudUploadIcon fontSize="large" color="primary" /> + <Typography variant="h5" className={classes.text}> Upload an image </Typography> + <HiddenFileInput accept=".jpg, .jpeg, .png, .gif" multiple={false} /> + </> + ); + + const Media = ( + <CardMedia + image={url} + className={classes.media} + onMouseEnter={handleMouseEnter} + onMouseLeave={handleMouseLeave} + > + {isMediaHover && <CancelOutlinedIcon className={classes.clearIcon} />} + </CardMedia> + ); + + return ( + <> + <CardActionArea onClick={handleClick} className={classes.root}> + {file ? (url && Media) : Upload} + </CardActionArea> + </> + ); +}; + +export default PollCreationImage; |