diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-10 23:16:01 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-10 23:16:01 +0300 |
commit | c6723fa8a2a303f356b1756b45b2190203c96582 (patch) | |
tree | 86f2a5cb68cf2db9e4638430e2a0c8898ed341f0 /src/containers | |
parent | fb738c8d487552a625305fa1b4e815a788ea83ae (diff) | |
parent | bac1f95ffe697fb6c008ab7cc29183efb6dc9893 (diff) | |
download | which-ui-c6723fa8a2a303f356b1756b45b2190203c96582.tar.gz |
Merge pull request #75 from which-ecosystem/uploads
S3 file uploads
Diffstat (limited to 'src/containers')
-rw-r--r-- | src/containers/Feed/PollSubmission.tsx | 46 | ||||
-rw-r--r-- | src/containers/Feed/PollSubmissionImage.tsx | 40 |
2 files changed, 52 insertions, 34 deletions
diff --git a/src/containers/Feed/PollSubmission.tsx b/src/containers/Feed/PollSubmission.tsx index 347eecc..faaf2bd 100644 --- a/src/containers/Feed/PollSubmission.tsx +++ b/src/containers/Feed/PollSubmission.tsx @@ -7,12 +7,13 @@ import { ClickAwayListener, Divider } from '@material-ui/core'; -import { Poll, Which } from 'which-types'; +import { Poll } from 'which-types'; import { useSnackbar } from 'notistack'; +import axios from 'axios'; + import PollSubmissionImage from './PollSubmissionImage'; import UserStrip from '../../components/UserStrip/UserStrip'; -import { post } from '../../requests'; -import { Contents } from './types'; +import { get, post } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; interface PropTypes{ @@ -29,37 +30,46 @@ const useStyles = makeStyles(theme => ({ } })); -const emptyContents: Contents = { - left: { url: '' }, - right: { url: '' } -}; - const PollSubmission: React.FC<PropTypes> = ({ addPoll }) => { const classes = useStyles(); const [expanded, setExpanded] = useState(false); - const [contents, setContents] = useState<Contents>(emptyContents); + const [left, setLeft] = useState<File>(); + const [right, setRight] = useState<File>(); const { enqueueSnackbar } = useSnackbar(); const { user } = useAuth(); - const readyToSubmit = contents.left.url && contents.right.url; - - const setUrl = (which: Which) => (url: string): void => { - setContents({ ...contents, [which]: { url } }); - }; + const readyToSubmit = left && right; const handleClickAway = () => { setExpanded(false); }; - const handleClick = () => { + const uploadImage = (file?: File) => { + const headers = { 'Content-Type': 'image/png' }; + return get('/files') + .then(response => response.data) + .then(uploadUrl => axios.put(uploadUrl, file, { headers })) + .then(response => { + const { config: { url } } = response; + return url && url.slice(0, url.indexOf('.png') + 4); + }); + }; + + const handleClick = async () => { if (expanded && readyToSubmit) { + const [leftUrl, rightUrl] = await Promise.all([uploadImage(left), uploadImage(right)]); + + const contents = { + left: { url: leftUrl }, + right: { url: rightUrl } + }; + post('/polls/', { contents }).then(response => { addPoll(response.data); enqueueSnackbar('Your poll has been successfully created!', { variant: 'success' }); }); - setContents({ ...emptyContents }); } setExpanded(!expanded); }; @@ -71,8 +81,8 @@ const PollSubmission: React.FC<PropTypes> = ({ addPoll }) => { {user && <UserStrip user={user} info="" />} <Divider /> <div className={classes.images}> - <PollSubmissionImage url={contents.left.url} setUrl={setUrl('left')} /> - <PollSubmissionImage url={contents.right.url} setUrl={setUrl('right')} /> + <PollSubmissionImage file={left} setFile={setLeft} /> + <PollSubmissionImage file={right} setFile={setRight} /> </div> </Collapse> <Button diff --git a/src/containers/Feed/PollSubmissionImage.tsx b/src/containers/Feed/PollSubmissionImage.tsx index 8835989..cd67847 100644 --- a/src/containers/Feed/PollSubmissionImage.tsx +++ b/src/containers/Feed/PollSubmissionImage.tsx @@ -1,14 +1,13 @@ -import React, { useState } from 'react'; +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'; -import UploadImage from '../../components/UploadImage/UploadImage'; - interface PropTypes { - url: string; - setUrl: (url: string) => void; + file: File | undefined; + setFile: (file: File | undefined) => void; } const useStyles = makeStyles({ @@ -35,18 +34,12 @@ const useStyles = makeStyles({ }); -const PollSubmissionImage: React.FC<PropTypes> = ({ url, setUrl }) => { +const PollSubmissionImage: React.FC<PropTypes> = ({ file, setFile }) => { const classes = useStyles(); - const [isModalOpen, setIsModalOpen] = useState(false); + const { files, onClick, HiddenFileInput } = useFilePicker(); + const [url, setUrl] = useState<string>(); const [isMediaHover, setIsMediaHover] = useState(false); - const handleClick = (): void => { - if (!isModalOpen) { - if (url) setUrl(''); - else setIsModalOpen(!isModalOpen); - } - }; - const handleMouseEnter = (): void => { setIsMediaHover(true); }; @@ -55,11 +48,27 @@ const PollSubmissionImage: React.FC<PropTypes> = ({ url, setUrl }) => { 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} /> </> ); @@ -77,9 +86,8 @@ const PollSubmissionImage: React.FC<PropTypes> = ({ url, setUrl }) => { return ( <> <CardActionArea onClick={handleClick} className={classes.root}> - {url ? Media : Upload} + {file ? (url && Media) : Upload} </CardActionArea> - <UploadImage isOpen={isModalOpen} setIsOpen={setIsModalOpen} callback={setUrl} /> </> ); }; |