diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-08-13 21:44:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-13 21:44:12 +0300 |
commit | 6ace75beae6ab6a466c4d0a9a60ca30aaad0a87c (patch) | |
tree | 7c1611c8dd7d45a72bb2316f78ea3980b27e09a5 /src/containers/PollCreation | |
parent | d1e0dcd8538a61184eca50fbf7769c6d2943ff6b (diff) | |
parent | 474dd922ac0512f1e0f64c145e9f76d2b10a1ba5 (diff) | |
download | which-ui-6ace75beae6ab6a466c4d0a9a60ca30aaad0a87c.tar.gz |
Merge pull request #79 from which-ecosystem/improved-poll-creation
PollCreation redesign
Diffstat (limited to 'src/containers/PollCreation')
-rw-r--r-- | src/containers/PollCreation/PollCreation.tsx | 36 | ||||
-rw-r--r-- | src/containers/PollCreation/PollCreationImage.tsx | 85 |
2 files changed, 52 insertions, 69 deletions
diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx index 7501d3a..64ab7fd 100644 --- a/src/containers/PollCreation/PollCreation.tsx +++ b/src/containers/PollCreation/PollCreation.tsx @@ -1,4 +1,5 @@ import React, { useState } from 'react'; +import { useHistory } from 'react-router-dom'; import { makeStyles } from '@material-ui/core/styles'; import { Button, @@ -6,7 +7,6 @@ import { Divider, Container } from '@material-ui/core'; -import { Poll } from 'which-types'; import { useSnackbar } from 'notistack'; import axios from 'axios'; @@ -14,10 +14,7 @@ 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; -} +import { useFeed } from '../../hooks/APIClient'; const useStyles = makeStyles(theme => ({ root: { @@ -29,41 +26,50 @@ const useStyles = makeStyles(theme => ({ } })); -const PollCreation: React.FC<PropTypes> = ({ addPoll }) => { +const PollCreation: React.FC = () => { const classes = useStyles(); - const [left, setLeft] = useState<File>(); - const [right, setRight] = useState<File>(); + const history = useHistory(); + const [left, setLeft] = useState<File | string>(); + const [right, setRight] = useState<File | string>(); const { enqueueSnackbar } = useSnackbar(); const { user } = useAuth(); + const { mutate: updateFeed } = useFeed(); const readyToSubmit = left && right; - const uploadImage = (file?: File) => { + const uploadFile = (file: File): Promise<string> => { 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); + return url?.slice(0, url?.indexOf('?')) || ''; }); }; + const resolveFile = async (file?: File | string): Promise<string> => { + if (file instanceof File) return uploadFile(file); + return file || ''; + }; + const handleClick = async () => { if (readyToSubmit) { - const [leftUrl, rightUrl] = await Promise.all([uploadImage(left), uploadImage(right)]); + const [leftUrl, rightUrl] = await Promise.all([resolveFile(left), resolveFile(right)]); const contents = { left: { url: leftUrl }, right: { url: rightUrl } }; - post('/polls/', { contents }).then(response => { - addPoll(response.data); + post('/polls/', { contents }).then(() => { + updateFeed(); enqueueSnackbar('Your poll has been successfully created!', { variant: 'success' }); }); + + history.push('/feed'); } }; @@ -73,8 +79,8 @@ const PollCreation: React.FC<PropTypes> = ({ addPoll }) => { {user && <UserStrip user={user} info="" />} <Divider /> <div className={classes.images}> - <PollCreationImage file={left} setFile={setLeft} /> - <PollCreationImage file={right} setFile={setRight} /> + <PollCreationImage callback={setLeft} /> + <PollCreationImage callback={setRight} /> </div> <Button color="primary" diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx index d3203a6..1200b11 100644 --- a/src/containers/PollCreation/PollCreationImage.tsx +++ b/src/containers/PollCreation/PollCreationImage.tsx @@ -1,13 +1,17 @@ -import React, { useState, useEffect } from 'react'; -import { useFilePicker, utils } from 'react-sage'; +import React, { useState } from 'react'; import { makeStyles } from '@material-ui/core/styles'; -import CloudUploadIcon from '@material-ui/icons/CloudUpload'; -import { CardActionArea, CardMedia, Typography } from '@material-ui/core'; -import CancelOutlinedIcon from '@material-ui/icons/CancelOutlined'; +import { + CardActionArea, + CardMedia, + Typography +} from '@material-ui/core'; +import ClearIcon from '@material-ui/icons/CancelOutlined'; + +import AttachLink from '../../components/AttachLink/AttachLink'; +import FileUpload from '../../components/FileUpload/FileUpload'; interface PropTypes { - file: File | undefined; - setFile: (file: File | undefined) => void; + callback: (file?: File | string) => void; } const useStyles = makeStyles({ @@ -15,7 +19,8 @@ const useStyles = makeStyles({ display: 'flex', justifyContent: 'center', flexDirection: 'column', - alignItems: 'center' + alignItems: 'center', + width: '50%' }, clearIcon: { opacity: '.5', @@ -27,69 +32,41 @@ const useStyles = makeStyles({ display: 'flex', justifyContent: 'center', alignItems: 'center' - }, - text: { - textAlign: 'center' } }); -const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => { +const PollCreationImage: React.FC<PropTypes> = ({ callback }) => { const classes = useStyles(); - const { files, onClick, HiddenFileInput } = useFilePicker(); const [url, setUrl] = useState<string>(); - const [isMediaHover, setIsMediaHover] = useState(false); - const handleMouseEnter = (): void => { - setIsMediaHover(true); + const handleClear = (): void => { + setUrl(undefined); + callback(undefined); }; - const handleMouseLeave = (): void => { - setIsMediaHover(false); + const childrenCallback = (fileUrl: string, file?: File) => { + setUrl(fileUrl); + callback(file || fileUrl); }; - useEffect(() => { - if (files?.length) { - const chosenFile = files[0]; - setFile(chosenFile); - utils.loadFile(chosenFile).then(result => setUrl(result)); - } - }, [files, setFile]); - - - const handleClick = () => { - if (file) { - setFile(undefined); - } else onClick(); - }; - - const Upload = ( - <> - <CloudUploadIcon fontSize="large" color="primary" /> - <Typography variant="h5" className={classes.text}> Upload an image </Typography> - <HiddenFileInput accept=".jpg, .jpeg, .png, .gif" multiple={false} /> - </> + <div className={classes.root}> + <FileUpload callback={childrenCallback} /> + <Typography variant="h6"> or </Typography> + <AttachLink callback={childrenCallback} /> + </div> ); const Media = ( - <CardMedia - image={url} - className={classes.media} - onMouseEnter={handleMouseEnter} - onMouseLeave={handleMouseLeave} - > - {isMediaHover && <CancelOutlinedIcon className={classes.clearIcon} />} - </CardMedia> + <CardActionArea onClick={handleClear} className={classes.root}> + <CardMedia image={url} className={classes.media}> + <ClearIcon className={classes.clearIcon} /> + </CardMedia> + </CardActionArea> ); - return ( - <> - <CardActionArea onClick={handleClick} className={classes.root}> - {file ? (url && Media) : Upload} - </CardActionArea> - </> - ); + return url ? Media : Upload; }; export default PollCreationImage; |