From f62b506a05e4024d15a68a7441f320bae557df06 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 13 Aug 2020 20:14:53 +0300
Subject: refactor: prepare PollCreation

---
 src/containers/PollCreation/PollCreationImage.tsx | 63 ++++++++++++++++-------
 1 file changed, 43 insertions(+), 20 deletions(-)

(limited to 'src/containers/PollCreation/PollCreationImage.tsx')

diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx
index d3203a6..f19162d 100644
--- a/src/containers/PollCreation/PollCreationImage.tsx
+++ b/src/containers/PollCreation/PollCreationImage.tsx
@@ -1,13 +1,18 @@
 import React, { useState, useEffect } from 'react';
 import { useFilePicker, utils } from 'react-sage';
 import { makeStyles } from '@material-ui/core/styles';
-import CloudUploadIcon from '@material-ui/icons/CloudUpload';
-import { CardActionArea, CardMedia, Typography } from '@material-ui/core';
-import CancelOutlinedIcon from '@material-ui/icons/CancelOutlined';
+import {
+  CardActionArea,
+  CardMedia,
+  Typography,
+  Button
+} from '@material-ui/core';
+import { CloudUpload, CancelOutlined, Link, Publish } from '@material-ui/icons/';
+import UploadImage from '../../components/UploadImage/UploadImage';
 
 interface PropTypes {
-  file: File | undefined;
-  setFile: (file: File | undefined) => void;
+  file?: File | string;
+  setFile: (file?: File | string) => void;
 }
 
 const useStyles = makeStyles({
@@ -15,7 +20,8 @@ const useStyles = makeStyles({
     display: 'flex',
     justifyContent: 'center',
     flexDirection: 'column',
-    alignItems: 'center'
+    alignItems: 'center',
+    width: '50%'
   },
   clearIcon: {
     opacity: '.5',
@@ -38,7 +44,8 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => {
   const classes = useStyles();
   const { files, onClick, HiddenFileInput } = useFilePicker();
   const [url, setUrl] = useState<string>();
-  const [isMediaHover, setIsMediaHover] = useState(false);
+  const [isMediaHover, setIsMediaHover] = useState<boolean>(false);
+  const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
 
   const handleMouseEnter = (): void => {
     setIsMediaHover(true);
@@ -56,19 +63,37 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => {
     }
   }, [files, setFile]);
 
-
-  const handleClick = () => {
-    if (file) {
-      setFile(undefined);
-    } else onClick();
+  const handleOpenModal = () => {
+    setIsModalOpen(true);
   };
 
+  const callback = (result: string) => {
+    setUrl(result);
+    setFile(result);
+  };
 
   const Upload = (
     <>
-      <CloudUploadIcon fontSize="large" color="primary" />
-      <Typography variant="h5" className={classes.text}> Upload an image </Typography>
       <HiddenFileInput accept=".jpg, .jpeg, .png, .gif" multiple={false} />
+      <Button 
+        onClick={onClick}
+        variant="contained"
+        color="primary"
+        size="large"
+        startIcon={<CloudUpload />}
+      > 
+        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} />
     </>
   );
 
@@ -79,16 +104,14 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => {
       onMouseEnter={handleMouseEnter}
       onMouseLeave={handleMouseLeave}
     >
-      {isMediaHover && <CancelOutlinedIcon className={classes.clearIcon} />}
+      {isMediaHover && <CancelOutlined className={classes.clearIcon} />}
     </CardMedia>
   );
 
   return (
-    <>
-      <CardActionArea onClick={handleClick} className={classes.root}>
-        {file ? (url && Media) : Upload}
-      </CardActionArea>
-    </>
+    <div className={classes.root}>
+      {file ? (url && Media) : Upload}
+    </div>
   );
 };
 
-- 
cgit v1.2.3


From 4109cd1b2fca7e4934f0aba1c3a7fabab62270bb Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 13 Aug 2020 20:50:31 +0300
Subject: feat: create AttachLink component

---
 src/containers/PollCreation/PollCreationImage.tsx | 17 ++---------------
 1 file changed, 2 insertions(+), 15 deletions(-)

(limited to 'src/containers/PollCreation/PollCreationImage.tsx')

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} />
     </>
   );
 
-- 
cgit v1.2.3


From 625ae3280bdf83b66a66873344acad4103d30006 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 13 Aug 2020 21:33:03 +0300
Subject: feat: create FileUpload component

---
 src/containers/PollCreation/PollCreationImage.tsx | 77 +++++++----------------
 1 file changed, 22 insertions(+), 55 deletions(-)

(limited to 'src/containers/PollCreation/PollCreationImage.tsx')

diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx
index e2084bb..d669d91 100644
--- a/src/containers/PollCreation/PollCreationImage.tsx
+++ b/src/containers/PollCreation/PollCreationImage.tsx
@@ -1,18 +1,17 @@
-import React, { useState, useEffect } from 'react';
-import { useFilePicker, utils } from 'react-sage';
+import React, { useState } from 'react';
 import { makeStyles } from '@material-ui/core/styles';
 import {
   CardActionArea,
   CardMedia,
   Typography,
-  Button
 } from '@material-ui/core';
-import { CloudUpload, CancelOutlined, Link, Publish } from '@material-ui/icons/';
+import ClearIcon from '@material-ui/icons/CancelOutlined';
+
 import AttachLink from '../../components/AttachLink/AttachLink';
+import FileUpload from '../../components/FileUpload/FileUpload';
 
 interface PropTypes {
-  file?: File | string;
-  setFile: (file?: File | string) => void;
+  callback: (file?: File | string) => void;
 }
 
 const useStyles = makeStyles({
@@ -33,73 +32,41 @@ const useStyles = makeStyles({
     display: 'flex',
     justifyContent: 'center',
     alignItems: 'center'
-  },
-  text: {
-    textAlign: 'center'
   }
 });
 
 
-const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => {
+const PollCreationImage: React.FC<PropTypes> = ({ callback }) => {
   const classes = useStyles();
-  const { files, onClick, HiddenFileInput } = useFilePicker();
   const [url, setUrl] = useState<string>();
-  const [isMediaHover, setIsMediaHover] = useState<boolean>(false);
 
-  const handleMouseEnter = (): void => {
-    setIsMediaHover(true);
+  const handleClear = (): void => {
+    setUrl(undefined);
+    callback(undefined);
   };
 
-  const handleMouseLeave = (): void => {
-    setIsMediaHover(false);
-  };
-
-  useEffect(() => {
-    if (files?.length) {
-      const chosenFile = files[0];
-      setFile(chosenFile);
-      utils.loadFile(chosenFile).then(result => setUrl(result));
-    }
-  }, [files, setFile]);
-
-  const callback = (result: string) => {
-    setUrl(result);
-    setFile(result);
+  const childrenCallback = (fileUrl: string, file?: File) => {
+    setUrl(fileUrl);
+    callback(file || fileUrl);
   };
 
   const Upload = (
-    <>
-      <HiddenFileInput accept=".jpg, .jpeg, .png, .gif" multiple={false} />
-      <Button 
-        onClick={onClick}
-        variant="contained"
-        color="primary"
-        size="large"
-        startIcon={<CloudUpload />}
-      > 
-        Upload
-      </Button>
+    <div className={classes.root}>
+      <FileUpload callback={childrenCallback} />
       <Typography variant="h6"> or </Typography>
-      <AttachLink callback={callback} />
-    </>
+      <AttachLink callback={childrenCallback} />
+    </div>
   );
 
   const Media = (
-    <CardMedia
-      image={url}
-      className={classes.media}
-      onMouseEnter={handleMouseEnter}
-      onMouseLeave={handleMouseLeave}
-    >
-      {isMediaHover && <CancelOutlined className={classes.clearIcon} />}
-    </CardMedia>
+    <CardActionArea onClick={handleClear} className={classes.root}>
+      <CardMedia image={url} className={classes.media}>
+        <ClearIcon className={classes.clearIcon} />
+      </CardMedia>
+    </CardActionArea>
   );
 
-  return (
-    <div className={classes.root}>
-      {file ? (url && Media) : Upload}
-    </div>
-  );
+  return url ? Media : Upload;
 };
 
 export default PollCreationImage;
-- 
cgit v1.2.3


From 02c346c65f266c1ffc108a27795c4aee9bb7616b Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Thu, 13 Aug 2020 21:36:51 +0300
Subject: fix: resolve eslint errors

---
 src/containers/PollCreation/PollCreationImage.tsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src/containers/PollCreation/PollCreationImage.tsx')

diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx
index d669d91..1200b11 100644
--- a/src/containers/PollCreation/PollCreationImage.tsx
+++ b/src/containers/PollCreation/PollCreationImage.tsx
@@ -3,7 +3,7 @@ import { makeStyles } from '@material-ui/core/styles';
 import {
   CardActionArea,
   CardMedia,
-  Typography,
+  Typography
 } from '@material-ui/core';
 import ClearIcon from '@material-ui/icons/CancelOutlined';
 
-- 
cgit v1.2.3

