From d46e0df8e3ae4fcb41880c7015a552bf8216316c Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 10 Aug 2020 22:45:35 +0300 Subject: chore: install react-sage --- package-lock.json | 5 +++++ package.json | 1 + 2 files changed, 6 insertions(+) diff --git a/package-lock.json b/package-lock.json index 7e5a09d..c01707b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10927,6 +10927,11 @@ "tiny-warning": "^1.0.0" } }, + "react-sage": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/react-sage/-/react-sage-0.1.2.tgz", + "integrity": "sha512-tJk2SATS0Czg5a3Sae4ilvv2W0FedBuvPFKi81qgAO7cIzn5/p1EArOYcRvGzinJVFP3/4l0Uz4gF95cx3cVhg==" + }, "react-scripts": { "version": "3.4.1", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-3.4.1.tgz", diff --git a/package.json b/package.json index 368f4e2..a18e64d 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "react-dom": "^16.13.1", "react-icons": "^3.10.0", "react-router-dom": "^5.2.0", + "react-sage": "^0.1.2", "react-scripts": "3.4.1", "react-virtualized": "^9.21.2", "swr": "^0.3.0", -- cgit v1.2.3 From bac1f95ffe697fb6c008ab7cc29183efb6dc9893 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 10 Aug 2020 23:10:47 +0300 Subject: feat: basic image upload --- src/containers/Feed/PollSubmission.tsx | 46 ++++++++++++++++++----------- 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 = ({ addPoll }) => { const classes = useStyles(); const [expanded, setExpanded] = useState(false); - const [contents, setContents] = useState(emptyContents); + const [left, setLeft] = useState(); + const [right, setRight] = useState(); 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 = ({ addPoll }) => { {user && }
- - + +