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

