aboutsummaryrefslogtreecommitdiff
path: root/src/containers/Feed/PollSubmission.tsx
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-08-10 11:19:53 +0300
committereug-vs <eug-vs@keemail.me>2020-08-10 11:19:53 +0300
commitf97989967ee0b88a8c64f226a4b28a79eeef5fd2 (patch)
tree9311246c260705c3c89fded54fb0bb688c98beec /src/containers/Feed/PollSubmission.tsx
parentcab8de5c6b246e1aa1376fa2b8666f09b44b6469 (diff)
downloadwhich-ui-f97989967ee0b88a8c64f226a4b28a79eeef5fd2.tar.gz
refactor: remove "Page" from container names
Diffstat (limited to 'src/containers/Feed/PollSubmission.tsx')
-rw-r--r--src/containers/Feed/PollSubmission.tsx92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/containers/Feed/PollSubmission.tsx b/src/containers/Feed/PollSubmission.tsx
new file mode 100644
index 0000000..347eecc
--- /dev/null
+++ b/src/containers/Feed/PollSubmission.tsx
@@ -0,0 +1,92 @@
+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: {
+ marginBottom: theme.spacing(4)
+ },
+ images: {
+ height: theme.spacing(50),
+ display: 'flex'
+ }
+}));
+
+const emptyContents: Contents = {
+ left: { url: '' },
+ right: { url: '' }
+};
+
+const PollSubmission: React.FC<PropTypes> = ({ addPoll }) => {
+ const classes = useStyles();
+ const [expanded, setExpanded] = useState(false);
+ const [contents, setContents] = useState<Contents>(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 (
+ <ClickAwayListener onClickAway={handleClickAway}>
+ <Card className={classes.root}>
+ <Collapse in={expanded} timeout="auto" unmountOnExit>
+ {user && <UserStrip user={user} info="" />}
+ <Divider />
+ <div className={classes.images}>
+ <PollSubmissionImage url={contents.left.url} setUrl={setUrl('left')} />
+ <PollSubmissionImage url={contents.right.url} setUrl={setUrl('right')} />
+ </div>
+ </Collapse>
+ <Button
+ color="primary"
+ disabled={expanded && !readyToSubmit}
+ variant={expanded ? 'contained' : 'outlined'}
+ onClick={handleClick}
+ fullWidth
+ >
+ {expanded ? 'Submit' : 'Create a Poll'}
+ </Button>
+ </Card>
+ </ClickAwayListener>
+ );
+};
+
+export default PollSubmission;