diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-13 21:44:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-13 21:44:12 +0300 |
commit | 6ace75beae6ab6a466c4d0a9a60ca30aaad0a87c (patch) | |
tree | 7c1611c8dd7d45a72bb2316f78ea3980b27e09a5 /src/components/AttachLink | |
parent | d1e0dcd8538a61184eca50fbf7769c6d2943ff6b (diff) | |
parent | 474dd922ac0512f1e0f64c145e9f76d2b10a1ba5 (diff) | |
download | which-ui-6ace75beae6ab6a466c4d0a9a60ca30aaad0a87c.tar.gz |
Merge pull request #79 from which-ecosystem/improved-poll-creation
PollCreation redesign
Diffstat (limited to 'src/components/AttachLink')
-rw-r--r-- | src/components/AttachLink/AttachLink.tsx | 42 | ||||
-rw-r--r-- | src/components/AttachLink/Modal.tsx | 76 |
2 files changed, 118 insertions, 0 deletions
diff --git a/src/components/AttachLink/AttachLink.tsx b/src/components/AttachLink/AttachLink.tsx new file mode 100644 index 0000000..e73f5c1 --- /dev/null +++ b/src/components/AttachLink/AttachLink.tsx @@ -0,0 +1,42 @@ +import React, { useState } from 'react'; +import Button from '@material-ui/core/Button'; +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/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<PropTypes> = ({ setIsOpen, isOpen, callback }) => { + const [url, setUrl] = useState<string>(''); + + 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<HTMLInputElement>) => { + setUrl(event.target.value); + }; + + return ( + <div> + <Dialog open={isOpen} onClose={handleClose}> + <DialogTitle>Upload via link</DialogTitle> + <DialogContent> + <DialogContentText> + Provide a valid URL to your image: + </DialogContentText> + <TextField + autoFocus + margin="dense" + id="name" + label="Image URL" + type="text" + fullWidth + autoComplete="off" + onChange={handleChange} + /> + </DialogContent> + <DialogActions> + <Button onClick={handleClose} color="primary"> + Cancel + </Button> + <Button onClick={handleSubmit} color="primary" disabled={!url.length}> + Submit + </Button> + </DialogActions> + </Dialog> + </div> + ); +}; + +export default Modal; |