aboutsummaryrefslogtreecommitdiff
path: root/src/PollCard/PollCard.tsx
blob: ee81b7d9188d0f34fe0dbb0d34ed8b54e40ed651 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import {
  Card,
  CardActionArea,
  CardMedia,
  Avatar,
  CardHeader
} from '@material-ui/core/';

interface ImageData {
  url: string;
  votes: number;
}

interface PropTypes {
  author: {
    name: string;
    avatarUrl: string;
  };
  contents: {
    left: ImageData;
    right: ImageData;
  };
}

interface PercentageBarPropTypes {
  value: number;
  which: 'left' | 'right';
}


const useStyles = makeStyles({
  root: {
    maxWidth: 600,
    height: 500,
    margin: '20px auto'
  },
  images: {
    height: 400,
    width: 300
  },
  imagesBlock: {
    display: 'flex'
  },
  percentage: {
    position: 'absolute',
    color: 'white',
    top: '86%',
    fontSize: 20
  },
  percentageLeft: {
    left: 30
  },
  percentageRight: {
    right: 30
  }
});


const PercentageBar: React.FC<PercentageBarPropTypes> = ({ value, which }) => {
  const classes = useStyles();
  const positionClassName = which === 'left' ? 'percentageLeft' : 'percentageRight';

  return (
    <div className={`${classes.percentage} ${classes[positionClassName]}`}>
      {value}
      %
    </div>
  );
};


const PollCard: React.FC<PropTypes> = ({ author, contents: { left, right } }) => {
  const classes = useStyles();

  const leftPercentage = Math.round(100 * (left.votes / (left.votes + right.votes)));
  const rightPercentage = 100 - leftPercentage;

  return (
    <Card className={classes.root}>
      <CardHeader
        avatar={(
          <Avatar aria-label="avatar">
            <img src={author.avatarUrl} alt={author.name[0].toUpperCase()} />
          </Avatar>
        )}
        title={author.name}
      />
      <div className={classes.imagesBlock}>
        <CardActionArea>
          <CardMedia
            className={classes.images}
            image={left.url}
          />
          <PercentageBar value={leftPercentage} which="left" />
        </CardActionArea>
        <CardActionArea>
          <CardMedia
            className={classes.images}
            image={right.url}
          />
          <PercentageBar value={rightPercentage} which="right" />
        </CardActionArea>
      </div>
    </Card>
  );
};
export default PollCard;