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