aboutsummaryrefslogtreecommitdiff
path: root/src/containers/PollCreation
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-08-13 21:33:03 +0300
committereug-vs <eug-vs@keemail.me>2020-08-13 21:33:03 +0300
commit625ae3280bdf83b66a66873344acad4103d30006 (patch)
tree26fa4bd8aa18331e71a03b70db906926ac6a56cd /src/containers/PollCreation
parent4109cd1b2fca7e4934f0aba1c3a7fabab62270bb (diff)
downloadwhich-ui-625ae3280bdf83b66a66873344acad4103d30006.tar.gz
feat: create FileUpload component
Diffstat (limited to 'src/containers/PollCreation')
-rw-r--r--src/containers/PollCreation/PollCreation.tsx4
-rw-r--r--src/containers/PollCreation/PollCreationImage.tsx77
2 files changed, 24 insertions, 57 deletions
diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx
index 1286c6f..34fc7c3 100644
--- a/src/containers/PollCreation/PollCreation.tsx
+++ b/src/containers/PollCreation/PollCreation.tsx
@@ -79,8 +79,8 @@ const PollCreation: React.FC = () => {
{user && <UserStrip user={user} info="" />}
<Divider />
<div className={classes.images}>
- <PollCreationImage file={left} setFile={setLeft} />
- <PollCreationImage file={right} setFile={setRight} />
+ <PollCreationImage callback={setLeft} />
+ <PollCreationImage callback={setRight} />
</div>
<Button
color="primary"
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;