diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components/AttachLink/AttachLink.tsx | 41 | ||||
-rw-r--r-- | src/components/AttachLink/Modal.tsx (renamed from src/components/UploadImage/UploadImage.tsx) | 8 | ||||
-rw-r--r-- | src/containers/Page/Page.tsx | 2 | ||||
-rw-r--r-- | src/containers/PollCreation/PollCreationImage.tsx | 17 | ||||
-rw-r--r-- | src/containers/Profile/ProfileInfo.tsx | 13 |
5 files changed, 51 insertions, 30 deletions
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<PropTypes> = ({ callback, children }) => { + const [isOpen, setIsOpen] = useState<boolean>(false); + + const handleOpen = (): void => { + setIsOpen(true); + }; + + const defaultButton = ( + <Button + onClick={handleOpen} + variant="outlined" + color="primary" + startIcon={<LinkIcon />} + > + Attach a link + </Button> + ); + + const child = children && React.Children.toArray(children)[0]; + + return ( + <> + <Modal callback={callback} isOpen={isOpen} setIsOpen={setIsOpen} /> + {React.isValidElement(child) + ? React.cloneElement(child, { onClick: handleOpen }) + : defaultButton + } + </> + ); +}; + +export default AttachLink; diff --git a/src/components/UploadImage/UploadImage.tsx b/src/components/AttachLink/Modal.tsx index 238d5cd..fa58fc4 100644 --- a/src/components/UploadImage/UploadImage.tsx +++ b/src/components/AttachLink/Modal.tsx @@ -15,7 +15,7 @@ interface PropTypes { callback: (url: string) => void; } -const UploadImage: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => { +const Modal: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => { const [url, setUrl] = useState<string>(''); const handleClose = () => { @@ -44,10 +44,10 @@ const UploadImage: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => { return ( <div> <Dialog open={isOpen} onClose={handleClose}> - <DialogTitle>Upload an Image</DialogTitle> + <DialogTitle>Upload via link</DialogTitle> <DialogContent> <DialogContentText> - Unfortunetly we do not support uploading images yet. Please provide a valid URL to your image: + Provide a valid URL to your image: </DialogContentText> <TextField autoFocus @@ -73,4 +73,4 @@ const UploadImage: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => { ); }; -export default UploadImage; +export default Modal; 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<PropTypes> = ({ file, setFile }) => { const { files, onClick, HiddenFileInput } = useFilePicker(); const [url, setUrl] = useState<string>(); const [isMediaHover, setIsMediaHover] = useState<boolean>(false); - const [isModalOpen, setIsModalOpen] = useState<boolean>(false); const handleMouseEnter = (): void => { setIsMediaHover(true); @@ -63,10 +62,6 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => { } }, [files, setFile]); - const handleOpenModal = () => { - setIsModalOpen(true); - }; - const callback = (result: string) => { setUrl(result); setFile(result); @@ -85,15 +80,7 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => { Upload </Button> <Typography variant="h6"> or </Typography> - <Button - onClick={handleOpenModal} - variant="outlined" - color="primary" - startIcon={<Link />} - > - Attach a link - </Button> - <UploadImage callback={callback} isOpen={isModalOpen} setIsOpen={setIsModalOpen} /> + <AttachLink callback={callback} /> </> ); 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<PropTypes> = ({ 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<PropTypes> = ({ ? <Skeleton animation="wave" variant="circle" width={150} height={150} className={classes.avatar} /> : userInfo?._id === user?._id ? ( - <div> + <AttachLink callback={patchAvatar}> <div className={classes.avatarContainer}> <Badge overlap="circle" @@ -116,7 +111,6 @@ const ProfileInfo: React.FC<PropTypes> = ({ vertical: 'bottom', horizontal: 'right' }} - onClick={handleClick} badgeContent={( <div className={classes.badge}> <CameraAltIcon /> @@ -126,8 +120,7 @@ const ProfileInfo: React.FC<PropTypes> = ({ <Avatar className={classes.avatar} user={userInfo} /> </Badge> </div> - <UploadImage isOpen={input} setIsOpen={setInput} callback={patchAvatar} /> - </div> + </AttachLink> ) : <Avatar className={classes.avatar} user={userInfo} /> } |