aboutsummaryrefslogtreecommitdiff
path: root/src/containers/PollCreation/PollCreationImage.tsx
blob: 1200b11700d1fd809dc6d6f9dcaa7e77ce0b16c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { useState } from 'react';
import { makeStyles } from '@material-ui/core/styles';
import {
  CardActionArea,
  CardMedia,
  Typography
} from '@material-ui/core';
import ClearIcon from '@material-ui/icons/CancelOutlined';

import AttachLink from '../../components/AttachLink/AttachLink';
import FileUpload from '../../components/FileUpload/FileUpload';

interface PropTypes {
  callback: (file?: File | string) => void;
}

const useStyles = makeStyles({
  root: {
    display: 'flex',
    justifyContent: 'center',
    flexDirection: 'column',
    alignItems: 'center',
    width: '50%'
  },
  clearIcon: {
    opacity: '.5',
    fontSize: 50
  },
  media: {
    height: '100%',
    width: '100%',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center'
  }
});


const PollCreationImage: React.FC<PropTypes> = ({ callback }) => {
  const classes = useStyles();
  const [url, setUrl] = useState<string>();

  const handleClear = (): void => {
    setUrl(undefined);
    callback(undefined);
  };

  const childrenCallback = (fileUrl: string, file?: File) => {
    setUrl(fileUrl);
    callback(file || fileUrl);
  };

  const Upload = (
    <div className={classes.root}>
      <FileUpload callback={childrenCallback} />
      <Typography variant="h6"> or </Typography>
      <AttachLink callback={childrenCallback} />
    </div>
  );

  const Media = (
    <CardActionArea onClick={handleClear} className={classes.root}>
      <CardMedia image={url} className={classes.media}>
        <ClearIcon className={classes.clearIcon} />
      </CardMedia>
    </CardActionArea>
  );

  return url ? Media : Upload;
};

export default PollCreationImage;