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 ------------------------------ 3 files changed, 117 insertions(+), 76 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 (limited to 'src/components') 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; -- 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 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/components/FileUpload/FileUpload.tsx (limited to 'src/components') 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; -- 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 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src/components') 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 } ); -- cgit v1.2.3