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(-) 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/components/AttachLink/AttachLink.tsx | 41 ++++++++++++ src/components/AttachLink/Modal.tsx | 76 +++++++++++++++++++++++ src/components/UploadImage/UploadImage.tsx | 76 ----------------------- src/containers/Page/Page.tsx | 2 +- src/containers/PollCreation/PollCreationImage.tsx | 17 +---- src/containers/Profile/ProfileInfo.tsx | 13 +--- 6 files changed, 123 insertions(+), 102 deletions(-) create mode 100644 src/components/AttachLink/AttachLink.tsx create mode 100644 src/components/AttachLink/Modal.tsx delete mode 100644 src/components/UploadImage/UploadImage.tsx diff --git a/src/components/AttachLink/AttachLink.tsx b/src/components/AttachLink/AttachLink.tsx new file mode 100644 index 0000000..b8742a2 --- /dev/null +++ b/src/components/AttachLink/AttachLink.tsx @@ -0,0 +1,41 @@ +import React, { useState } from 'react'; +import { Button, } from '@material-ui/core'; +import LinkIcon from '@material-ui/icons/Link'; +import Modal from './Modal'; + +interface PropTypes { + callback: (url: string) => void; +} + +const AttachLink: React.FC = ({ callback, children }) => { + const [isOpen, setIsOpen] = useState(false); + + const handleOpen = (): void => { + setIsOpen(true); + }; + + const defaultButton = ( + + ); + + const child = children && React.Children.toArray(children)[0]; + + return ( + <> + + {React.isValidElement(child) + ? React.cloneElement(child, { onClick: handleOpen }) + : defaultButton + } + + ); +}; + +export default AttachLink; diff --git a/src/components/AttachLink/Modal.tsx b/src/components/AttachLink/Modal.tsx new file mode 100644 index 0000000..fa58fc4 --- /dev/null +++ b/src/components/AttachLink/Modal.tsx @@ -0,0 +1,76 @@ +import React, { useState } from 'react'; +import { + Button, + TextField, + Dialog, + DialogActions, + DialogContent, + DialogContentText, + DialogTitle +} from '@material-ui/core'; + +interface PropTypes { + isOpen: boolean; + setIsOpen: (value: boolean) => void; + callback: (url: string) => void; +} + +const Modal: React.FC = ({ setIsOpen, isOpen, callback }) => { + const [url, setUrl] = useState(''); + + const handleClose = () => { + setIsOpen(false); + }; + + const handleSubmit = () => { + let result = url; + if (url.startsWith('https://www.instagram.com/')) { + const match = url.match('/p/(.*)/'); + const id = match && match[1]; + result = `https://www.instagram.com/p/${id}/media/?size=l`; + } else if (url.startsWith('https://drive.google.com/')) { + const match = url.match('/d/(.*)/'); + const fileId = match && match[1]; + result = `https://drive.google.com/uc?export=view&id=${fileId}`; + } + callback(result || ''); + handleClose(); + }; + + const handleChange = (event:React.ChangeEvent) => { + setUrl(event.target.value); + }; + + return ( +
+ + Upload via link + + + Provide a valid URL to your image: + + + + + + + + +
+ ); +}; + +export default Modal; diff --git a/src/components/UploadImage/UploadImage.tsx b/src/components/UploadImage/UploadImage.tsx deleted file mode 100644 index 238d5cd..0000000 --- a/src/components/UploadImage/UploadImage.tsx +++ /dev/null @@ -1,76 +0,0 @@ -import React, { useState } from 'react'; -import { - Button, - TextField, - Dialog, - DialogActions, - DialogContent, - DialogContentText, - DialogTitle -} from '@material-ui/core'; - -interface PropTypes { - isOpen: boolean; - setIsOpen: (value: boolean) => void; - callback: (url: string) => void; -} - -const UploadImage: React.FC = ({ setIsOpen, isOpen, callback }) => { - const [url, setUrl] = useState(''); - - const handleClose = () => { - setIsOpen(false); - }; - - const handleSubmit = () => { - let result = url; - if (url.startsWith('https://www.instagram.com/')) { - const match = url.match('/p/(.*)/'); - const id = match && match[1]; - result = `https://www.instagram.com/p/${id}/media/?size=l`; - } else if (url.startsWith('https://drive.google.com/')) { - const match = url.match('/d/(.*)/'); - const fileId = match && match[1]; - result = `https://drive.google.com/uc?export=view&id=${fileId}`; - } - callback(result || ''); - handleClose(); - }; - - const handleChange = (event:React.ChangeEvent) => { - setUrl(event.target.value); - }; - - return ( -
- - Upload an Image - - - Unfortunetly we do not support uploading images yet. Please provide a valid URL to your image: - - - - - - - - -
- ); -}; - -export default UploadImage; diff --git a/src/containers/Page/Page.tsx b/src/containers/Page/Page.tsx index 19cf6aa..848ca1d 100644 --- a/src/containers/Page/Page.tsx +++ b/src/containers/Page/Page.tsx @@ -36,7 +36,7 @@ const Page: React.FC = () => { preventDuplicate maxSnack={isMobile ? 1 : 3} anchorOrigin={{ - vertical: isMobile ? 'top' : 'bottom', + vertical: 'top', horizontal: 'right' }} > 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 - - + ); diff --git a/src/containers/Profile/ProfileInfo.tsx b/src/containers/Profile/ProfileInfo.tsx index 87af99d..83555e9 100644 --- a/src/containers/Profile/ProfileInfo.tsx +++ b/src/containers/Profile/ProfileInfo.tsx @@ -6,7 +6,7 @@ import CameraAltIcon from '@material-ui/icons/CameraAlt'; import VerifiedIcon from '@material-ui/icons/CheckCircleOutline'; import Skeleton from '@material-ui/lab/Skeleton'; import Highlight from './Highlight'; -import UploadImage from '../../components/UploadImage/UploadImage'; +import AttachLink from '../../components/AttachLink/AttachLink'; import Avatar from '../../components/Avatar/Avatar'; import { patch } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; @@ -86,14 +86,9 @@ const ProfileInfo: React.FC = ({ savedPolls, totalVotes, setUserInfo, userInfo }) => { const classes = useStyles(); - const [input, setInput] = useState(false); const { user } = useAuth(); const dateSince = new Date(userInfo?.createdAt || '').toLocaleDateString(); - const handleClick = () => { - setInput(!input); - }; - const patchAvatar = (url: string) => { const id = user?._id; patch(`/users/${id}`, { avatarUrl: url }).then(res => { @@ -108,7 +103,7 @@ const ProfileInfo: React.FC = ({ ? : userInfo?._id === user?._id ? ( -
+
= ({ vertical: 'bottom', horizontal: 'right' }} - onClick={handleClick} badgeContent={(
@@ -126,8 +120,7 @@ const ProfileInfo: React.FC = ({
- -
+
) : } -- 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/components/AttachLink/AttachLink.tsx | 6 +- src/components/FileUpload/FileUpload.tsx | 46 ++++++++++++++ src/containers/PollCreation/PollCreation.tsx | 4 +- src/containers/PollCreation/PollCreationImage.tsx | 77 +++++++---------------- 4 files changed, 73 insertions(+), 60 deletions(-) create mode 100644 src/components/FileUpload/FileUpload.tsx diff --git a/src/components/AttachLink/AttachLink.tsx b/src/components/AttachLink/AttachLink.tsx index b8742a2..eb7aebd 100644 --- a/src/components/AttachLink/AttachLink.tsx +++ b/src/components/AttachLink/AttachLink.tsx @@ -13,9 +13,9 @@ const AttachLink: React.FC = ({ callback, children }) => { const handleOpen = (): void => { setIsOpen(true); }; - + const defaultButton = ( - + ); + + return ( + <> + + {React.isValidElement(child) + ? React.cloneElement(child, { onClick }) + : defaultButton + } + + ); +}; + +export default FileUpload; 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/components/AttachLink/AttachLink.tsx | 9 +++++---- src/components/FileUpload/FileUpload.tsx | 7 ++++--- src/containers/PollCreation/PollCreation.tsx | 2 +- src/containers/PollCreation/PollCreationImage.tsx | 2 +- src/containers/Profile/ProfileInfo.tsx | 2 +- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/components/AttachLink/AttachLink.tsx b/src/components/AttachLink/AttachLink.tsx index eb7aebd..e73f5c1 100644 --- a/src/components/AttachLink/AttachLink.tsx +++ b/src/components/AttachLink/AttachLink.tsx @@ -1,5 +1,5 @@ import React, { useState } from 'react'; -import { Button, } from '@material-ui/core'; +import Button from '@material-ui/core/Button'; import LinkIcon from '@material-ui/icons/Link'; import Modal from './Modal'; @@ -30,9 +30,10 @@ const AttachLink: React.FC = ({ callback, children }) => { return ( <> - {React.isValidElement(child) - ? React.cloneElement(child, { onClick: handleOpen }) - : defaultButton + { + React.isValidElement(child) + ? React.cloneElement(child, { onClick: handleOpen }) + : defaultButton } ); diff --git a/src/components/FileUpload/FileUpload.tsx b/src/components/FileUpload/FileUpload.tsx index dca70b1..67d280d 100644 --- a/src/components/FileUpload/FileUpload.tsx +++ b/src/components/FileUpload/FileUpload.tsx @@ -35,9 +35,10 @@ const FileUpload: React.FC = ({ callback, children }) => { return ( <> - {React.isValidElement(child) - ? React.cloneElement(child, { onClick }) - : defaultButton + { + React.isValidElement(child) + ? React.cloneElement(child, { onClick }) + : defaultButton } ); 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'; diff --git a/src/containers/Profile/ProfileInfo.tsx b/src/containers/Profile/ProfileInfo.tsx index 83555e9..82f640d 100644 --- a/src/containers/Profile/ProfileInfo.tsx +++ b/src/containers/Profile/ProfileInfo.tsx @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React from 'react'; import { Badge, Typography } from '@material-ui/core/'; import { makeStyles } from '@material-ui/core/styles'; import { User } from 'which-types'; -- cgit v1.2.3 From 474dd922ac0512f1e0f64c145e9f76d2b10a1ba5 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 13 Aug 2020 21:39:43 +0300 Subject: feat!: temporarily disable ScrollTopArrow --- src/index.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 1f70100..6c33db4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -7,7 +7,6 @@ import teal from '@material-ui/core/colors/teal'; import 'typeface-roboto'; import Header from './components/Header/Header'; -import ScrollTopArrow from './components/ScrollTopArrow/ScrollTopArrow'; import Page from './containers/Page/Page'; import { AuthProvider } from './hooks/useAuth'; @@ -33,7 +32,6 @@ const App: React.FC = () => {
- -- cgit v1.2.3