From 8b5df9285b3917c63b7904b434354e3d4b58858d Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Sun, 5 Jul 2020 17:28:08 +0300
Subject: fix: resolve instagram url correctly

---
 src/components/UploadImage/UploadImage.tsx | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)

(limited to 'src/components/UploadImage/UploadImage.tsx')

diff --git a/src/components/UploadImage/UploadImage.tsx b/src/components/UploadImage/UploadImage.tsx
index 836b2a0..5b2f38e 100644
--- a/src/components/UploadImage/UploadImage.tsx
+++ b/src/components/UploadImage/UploadImage.tsx
@@ -6,9 +6,7 @@ import {
   DialogActions,
   DialogContent,
   DialogContentText,
-  DialogTitle,
-  FormControlLabel,
-  Switch
+  DialogTitle
 } from '@material-ui/core';
 
 interface PropTypes {
@@ -19,15 +17,18 @@ interface PropTypes {
 
 const UploadImage: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => {
   const [url, setUrl] = useState<string>('');
-  const [isInstagramLink, setIsInstagramLink] = useState<boolean>(false);
-
 
   const handleClose = () => {
     setIsOpen(false);
   };
 
   const handleSubmit = () => {
-    const result = isInstagramLink ? `${url.slice(0, url.length - 29)}/media/?size=l` : url;
+    let result = url;
+    if (url.startsWith('https://www.instagram.com/')) {
+      const lastSlashIndex = url.lastIndexOf('/');
+      const baseUrl = url.slice(0, lastSlashIndex);
+      result = `${baseUrl}/media/?size=l`;
+    }
     callback(result || '');
     handleClose();
   };
@@ -36,10 +37,6 @@ const UploadImage: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => {
     setUrl(event.target.value);
   };
 
-  const handleSwitch = () => {
-    setIsInstagramLink(!isInstagramLink);
-  };
-
   return (
     <div>
       <Dialog open={isOpen} onClose={handleClose}>
@@ -58,10 +55,6 @@ const UploadImage: React.FC<PropTypes> = ({ setIsOpen, isOpen, callback }) => {
             autoComplete="off"
             onChange={handleChange}
           />
-          <FormControlLabel
-            control={<Switch color="primary" onClick={handleSwitch} checked={isInstagramLink} size="small" />}
-            label="It's an Instagram link"
-          />
         </DialogContent>
         <DialogActions>
           <Button onClick={handleClose} color="primary">
-- 
cgit v1.2.3

