import React, { useState } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import Collapse from '@material-ui/core/Collapse'; import { Button, Card, ClickAwayListener, Divider } from '@material-ui/core'; import { Poll, Which } from 'which-types'; import { useSnackbar } from 'notistack'; import PollSubmissionImage from './PollSubmissionImage'; import UserStrip from '../../components/UserStrip/UserStrip'; import { post } from '../../requests'; import { Contents } from './types'; import { useAuth } from '../../hooks/useAuth'; interface PropTypes{ addPoll: (poll: Poll) => void; } const useStyles = makeStyles(theme => ({ root: { height: theme.spacing(50), display: 'flex' } })); const emptyContents: Contents = { left: { url: '' }, right: { url: '' } }; const PollSubmission: React.FC = ({ addPoll }) => { const classes = useStyles(); const [expanded, setExpanded] = useState(false); const [contents, setContents] = useState(emptyContents); const { enqueueSnackbar } = useSnackbar(); const { user } = useAuth(); const readyToSubmit = contents.left.url && contents.right.url; const setUrl = (which: Which) => (url: string): void => { setContents({ ...contents, [which]: { url } }); }; const handleClickAway = () => { setExpanded(false); }; const handleClick = () => { if (expanded && readyToSubmit) { post('/polls/', { contents }).then(response => { addPoll(response.data); enqueueSnackbar('Your poll has been successfully created!', { variant: 'success' }); }); setContents({ ...emptyContents }); } setExpanded(!expanded); }; return ( {user && }
); }; export default PollSubmission;