diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-13 17:27:32 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-13 17:27:32 +0300 |
commit | d1e0dcd8538a61184eca50fbf7769c6d2943ff6b (patch) | |
tree | 9c2ba42d34e469d292fc1fe807e3f814a872a69e /src/containers/PollCreation/PollCreation.tsx | |
parent | 2dc5fc00347256982136deea98d483c444002595 (diff) | |
parent | 52799ec4e4cd5801423ee0d2aa56039c061afdb4 (diff) | |
download | which-ui-d1e0dcd8538a61184eca50fbf7769c6d2943ff6b.tar.gz |
Merge pull request #78 from which-ecosystem/redesign
Move PollSubmission to separate page and add FAB
Diffstat (limited to 'src/containers/PollCreation/PollCreation.tsx')
-rw-r--r-- | src/containers/PollCreation/PollCreation.tsx | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx new file mode 100644 index 0000000..7501d3a --- /dev/null +++ b/src/containers/PollCreation/PollCreation.tsx @@ -0,0 +1,93 @@ +import React, { useState } from 'react'; +import { makeStyles } from '@material-ui/core/styles'; +import { + Button, + Card, + Divider, + Container +} from '@material-ui/core'; +import { Poll } from 'which-types'; +import { useSnackbar } from 'notistack'; +import axios from 'axios'; + +import PollCreationImage from './PollCreationImage'; +import UserStrip from '../../components/UserStrip/UserStrip'; +import { get, post } from '../../requests'; +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 PollCreation: React.FC<PropTypes> = ({ addPoll }) => { + const classes = useStyles(); + const [left, setLeft] = useState<File>(); + const [right, setRight] = useState<File>(); + const { enqueueSnackbar } = useSnackbar(); + const { user } = useAuth(); + + const readyToSubmit = left && right; + + const uploadImage = (file?: File) => { + const headers = { 'Content-Type': 'image/png' }; + return get('/files') + .then(response => response.data) + .then(uploadUrl => axios.put(uploadUrl, file, { headers })) + .then(response => { + const { config: { url } } = response; + return url && url.slice(0, url.indexOf('.png') + 4); + }); + }; + + const handleClick = async () => { + if (readyToSubmit) { + const [leftUrl, rightUrl] = await Promise.all([uploadImage(left), uploadImage(right)]); + + const contents = { + left: { url: leftUrl }, + right: { url: rightUrl } + }; + + post('/polls/', { contents }).then(response => { + addPoll(response.data); + enqueueSnackbar('Your poll has been successfully created!', { + variant: 'success' + }); + }); + } + }; + + return ( + <Container maxWidth="sm" disableGutters> + <Card className={classes.root}> + {user && <UserStrip user={user} info="" />} + <Divider /> + <div className={classes.images}> + <PollCreationImage file={left} setFile={setLeft} /> + <PollCreationImage file={right} setFile={setRight} /> + </div> + <Button + color="primary" + disabled={!readyToSubmit} + variant="contained" + onClick={handleClick} + fullWidth + > + Submit + </Button> + </Card> + </Container> + ); +}; + +export default PollCreation; |