import React, { useState } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Card, CardActionArea, CardMedia, Avatar, CardHeader } from '@material-ui/core/'; import { Which, Poll } from 'which-types'; import PercentageBar from './PercentageBar'; import { post } from '../../requests'; interface PropTypes { initialPoll: Poll; navigate: (prefix: string, id: string) => void; } const DATE_FORMAT = { month: 'long', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit' }; const useStyles = makeStyles(theme => ({ root: { maxWidth: theme.spacing(75), height: 488, margin: '40px auto' }, images: { height: theme.spacing(50), width: 300 }, imagesBlock: { display: 'flex' }, avatar: { cursor: 'pointer' }, rateLine: { position: 'relative', width: '100%', height: theme.spacing(2), backgroundColor: theme.palette.primary.light, transitionDuration: '0.5s' }, highlight: { backgroundColor: `${theme.palette.primary.main} !important` }, fillRateLine: { height: theme.spacing(2), backgroundColor: theme.palette.primary.light, transitionDuration: '0.5s' } })); const PollCard: React.FC = ({ initialPoll, navigate }) => { const [poll, setPoll] = useState(initialPoll); const classes = useStyles(); const { author, contents: { left, right }, userChoice } = poll; const date: string = new Date(poll.createdAt).toLocaleString('default', DATE_FORMAT); const handleNavigate = () => { navigate('profile', poll.author._id); }; const vote = (which: Which) => { if (userChoice) return; post('votes/', { which, pollId: poll._id }).then(() => { poll.contents[which].votes += 1; poll.userChoice = which; setPoll({ ...poll }); }); }; const handleLeft = () => vote('left'); const handleRight = () => vote('right'); const leftPercentage = Math.round(100 * (left.votes / (left.votes + right.votes))); const rightPercentage = 100 - leftPercentage; const dominant: Which = left.votes >= right.votes ? 'left' : 'right'; return ( )} title={author.username} subheader={date} />
); }; export default PollCard;