From f62b506a05e4024d15a68a7441f320bae557df06 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 13 Aug 2020 20:14:53 +0300 Subject: refactor: prepare PollCreation --- src/containers/PollCreation/PollCreation.tsx | 30 ++++++----- src/containers/PollCreation/PollCreationImage.tsx | 63 ++++++++++++++++------- 2 files changed, 61 insertions(+), 32 deletions(-) (limited to 'src/containers/PollCreation') diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx index 7501d3a..1286c6f 100644 --- a/src/containers/PollCreation/PollCreation.tsx +++ b/src/containers/PollCreation/PollCreation.tsx @@ -1,4 +1,5 @@ import React, { useState } from 'react'; +import { useHistory } from 'react-router-dom'; import { makeStyles } from '@material-ui/core/styles'; import { Button, @@ -6,7 +7,6 @@ import { Divider, Container } from '@material-ui/core'; -import { Poll } from 'which-types'; import { useSnackbar } from 'notistack'; import axios from 'axios'; @@ -14,10 +14,7 @@ import PollCreationImage from './PollCreationImage'; import UserStrip from '../../components/UserStrip/UserStrip'; import { get, post } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; - -interface PropTypes{ - addPoll: (poll: Poll) => void; -} +import { useFeed } from '../../hooks/APIClient'; const useStyles = makeStyles(theme => ({ root: { @@ -29,29 +26,36 @@ const useStyles = makeStyles(theme => ({ } })); -const PollCreation: React.FC = ({ addPoll }) => { +const PollCreation: React.FC = () => { const classes = useStyles(); - const [left, setLeft] = useState(); - const [right, setRight] = useState(); + const history = useHistory(); + const [left, setLeft] = useState(); + const [right, setRight] = useState(); const { enqueueSnackbar } = useSnackbar(); const { user } = useAuth(); + const { mutate: updateFeed } = useFeed(); const readyToSubmit = left && right; - const uploadImage = (file?: File) => { + const uploadFile = (file: File): Promise => { 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); + return url?.slice(0, url?.indexOf('?')) || ''; }); }; + const resolveFile = async (file?: File | string): Promise => { + if (file instanceof File) return uploadFile(file); + return file || ''; + }; + const handleClick = async () => { if (readyToSubmit) { - const [leftUrl, rightUrl] = await Promise.all([uploadImage(left), uploadImage(right)]); + const [leftUrl, rightUrl] = await Promise.all([resolveFile(left), resolveFile(right)]); const contents = { left: { url: leftUrl }, @@ -59,11 +63,13 @@ const PollCreation: React.FC = ({ addPoll }) => { }; post('/polls/', { contents }).then(response => { - addPoll(response.data); + updateFeed(); enqueueSnackbar('Your poll has been successfully created!', { variant: 'success' }); }); + + history.push('/feed'); } }; diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx index d3203a6..f19162d 100644 --- a/src/containers/PollCreation/PollCreationImage.tsx +++ b/src/containers/PollCreation/PollCreationImage.tsx @@ -1,13 +1,18 @@ 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 { + CardActionArea, + CardMedia, + Typography, + Button +} from '@material-ui/core'; +import { CloudUpload, CancelOutlined, Link, Publish } from '@material-ui/icons/'; +import UploadImage from '../../components/UploadImage/UploadImage'; interface PropTypes { - file: File | undefined; - setFile: (file: File | undefined) => void; + file?: File | string; + setFile: (file?: File | string) => void; } const useStyles = makeStyles({ @@ -15,7 +20,8 @@ const useStyles = makeStyles({ display: 'flex', justifyContent: 'center', flexDirection: 'column', - alignItems: 'center' + alignItems: 'center', + width: '50%' }, clearIcon: { opacity: '.5', @@ -38,7 +44,8 @@ const PollCreationImage: React.FC = ({ file, setFile }) => { const classes = useStyles(); const { files, onClick, HiddenFileInput } = useFilePicker(); const [url, setUrl] = useState(); - const [isMediaHover, setIsMediaHover] = useState(false); + const [isMediaHover, setIsMediaHover] = useState(false); + const [isModalOpen, setIsModalOpen] = useState(false); const handleMouseEnter = (): void => { setIsMediaHover(true); @@ -56,19 +63,37 @@ const PollCreationImage: React.FC = ({ file, setFile }) => { } }, [files, setFile]); - - const handleClick = () => { - if (file) { - setFile(undefined); - } else onClick(); + const handleOpenModal = () => { + setIsModalOpen(true); }; + const callback = (result: string) => { + setUrl(result); + setFile(result); + }; const Upload = ( <> - - Upload an image + + or + + ); @@ -79,16 +104,14 @@ const PollCreationImage: React.FC = ({ file, setFile }) => { onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave} > - {isMediaHover && } + {isMediaHover && } ); return ( - <> - - {file ? (url && Media) : Upload} - - +
+ {file ? (url && Media) : Upload} +
); }; -- cgit v1.2.3 From 4109cd1b2fca7e4934f0aba1c3a7fabab62270bb Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 13 Aug 2020 20:50:31 +0300 Subject: feat: create AttachLink component --- src/containers/PollCreation/PollCreationImage.tsx | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) (limited to 'src/containers/PollCreation') diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx index f19162d..e2084bb 100644 --- a/src/containers/PollCreation/PollCreationImage.tsx +++ b/src/containers/PollCreation/PollCreationImage.tsx @@ -8,7 +8,7 @@ import { Button } from '@material-ui/core'; import { CloudUpload, CancelOutlined, Link, Publish } from '@material-ui/icons/'; -import UploadImage from '../../components/UploadImage/UploadImage'; +import AttachLink from '../../components/AttachLink/AttachLink'; interface PropTypes { file?: File | string; @@ -45,7 +45,6 @@ const PollCreationImage: React.FC = ({ file, setFile }) => { const { files, onClick, HiddenFileInput } = useFilePicker(); const [url, setUrl] = useState(); const [isMediaHover, setIsMediaHover] = useState(false); - const [isModalOpen, setIsModalOpen] = useState(false); const handleMouseEnter = (): void => { setIsMediaHover(true); @@ -63,10 +62,6 @@ const PollCreationImage: React.FC = ({ file, setFile }) => { } }, [files, setFile]); - const handleOpenModal = () => { - setIsModalOpen(true); - }; - const callback = (result: string) => { setUrl(result); setFile(result); @@ -85,15 +80,7 @@ const PollCreationImage: React.FC = ({ file, setFile }) => { Upload or - - + ); -- cgit v1.2.3 From 625ae3280bdf83b66a66873344acad4103d30006 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 13 Aug 2020 21:33:03 +0300 Subject: feat: create FileUpload component --- src/containers/PollCreation/PollCreation.tsx | 4 +- src/containers/PollCreation/PollCreationImage.tsx | 77 +++++++---------------- 2 files changed, 24 insertions(+), 57 deletions(-) (limited to 'src/containers/PollCreation') diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx index 1286c6f..34fc7c3 100644 --- a/src/containers/PollCreation/PollCreation.tsx +++ b/src/containers/PollCreation/PollCreation.tsx @@ -79,8 +79,8 @@ const PollCreation: React.FC = () => { {user && }
- - + +
+
+ or - - + +
); const Media = ( - - {isMediaHover && } - + + + + + ); - return ( -
- {file ? (url && Media) : Upload} -
- ); + return url ? Media : Upload; }; export default PollCreationImage; -- cgit v1.2.3 From 02c346c65f266c1ffc108a27795c4aee9bb7616b Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 13 Aug 2020 21:36:51 +0300 Subject: fix: resolve eslint errors --- src/containers/PollCreation/PollCreation.tsx | 2 +- src/containers/PollCreation/PollCreationImage.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/containers/PollCreation') diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx index 34fc7c3..64ab7fd 100644 --- a/src/containers/PollCreation/PollCreation.tsx +++ b/src/containers/PollCreation/PollCreation.tsx @@ -62,7 +62,7 @@ const PollCreation: React.FC = () => { right: { url: rightUrl } }; - post('/polls/', { contents }).then(response => { + post('/polls/', { contents }).then(() => { updateFeed(); enqueueSnackbar('Your poll has been successfully created!', { variant: 'success' diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx index d669d91..1200b11 100644 --- a/src/containers/PollCreation/PollCreationImage.tsx +++ b/src/containers/PollCreation/PollCreationImage.tsx @@ -3,7 +3,7 @@ import { makeStyles } from '@material-ui/core/styles'; import { CardActionArea, CardMedia, - Typography, + Typography } from '@material-ui/core'; import ClearIcon from '@material-ui/icons/CancelOutlined'; -- cgit v1.2.3