aboutsummaryrefslogtreecommitdiff
path: root/src/containers/FeedPage/PollSubmissionImage.tsx
blob: 8835989bf5abab4a8862c4b9de248039fae75fec (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import React, { useState } from 'react';
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 UploadImage from '../../components/UploadImage/UploadImage';

interface PropTypes {
  url: string;
  setUrl: (url: string) => void;
}

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


const PollSubmissionImage: React.FC<PropTypes> = ({ url, setUrl }) => {
  const classes = useStyles();
  const [isModalOpen, setIsModalOpen] = useState(false);
  const [isMediaHover, setIsMediaHover] = useState(false);

  const handleClick = (): void => {
    if (!isModalOpen) {
      if (url) setUrl('');
      else setIsModalOpen(!isModalOpen);
    }
  };

  const handleMouseEnter = (): void => {
    setIsMediaHover(true);
  };

  const handleMouseLeave = (): void => {
    setIsMediaHover(false);
  };


  const Upload = (
    <>
      <CloudUploadIcon fontSize="large" color="primary" />
      <Typography variant="h5" className={classes.text}> Upload an image </Typography>
    </>
  );

  const Media = (
    <CardMedia
      image={url}
      className={classes.media}
      onMouseEnter={handleMouseEnter}
      onMouseLeave={handleMouseLeave}
    >
      {isMediaHover && <CancelOutlinedIcon className={classes.clearIcon} />}
    </CardMedia>
  );

  return (
    <>
      <CardActionArea onClick={handleClick} className={classes.root}>
        {url ? Media : Upload}
      </CardActionArea>
      <UploadImage isOpen={isModalOpen} setIsOpen={setIsModalOpen} callback={setUrl} />
    </>
  );
};

export default PollSubmissionImage;