aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-08-13 20:14:53 +0300
committereug-vs <eug-vs@keemail.me>2020-08-13 20:14:53 +0300
commitf62b506a05e4024d15a68a7441f320bae557df06 (patch)
tree4dfd66e03cfe2fb47ab963bbf408eb0a3e0f8f1f
parentd1e0dcd8538a61184eca50fbf7769c6d2943ff6b (diff)
downloadwhich-ui-f62b506a05e4024d15a68a7441f320bae557df06.tar.gz
refactor: prepare PollCreation
-rw-r--r--src/containers/PollCreation/PollCreation.tsx30
-rw-r--r--src/containers/PollCreation/PollCreationImage.tsx63
2 files changed, 61 insertions, 32 deletions
diff --git a/src/containers/PollCreation/PollCreation.tsx b/src/containers/PollCreation/PollCreation.tsx
index 7501d3a..1286c6f 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,29 +26,36 @@ 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 },
@@ -59,11 +63,13 @@ const PollCreation: React.FC<PropTypes> = ({ addPoll }) => {
};
post('/polls/', { contents }).then(response => {
- addPoll(response.data);
+ updateFeed();
enqueueSnackbar('Your poll has been successfully created!', {
variant: 'success'
});
});
+
+ history.push('/feed');
}
};
diff --git a/src/containers/PollCreation/PollCreationImage.tsx b/src/containers/PollCreation/PollCreationImage.tsx
index d3203a6..f19162d 100644
--- a/src/containers/PollCreation/PollCreationImage.tsx
+++ b/src/containers/PollCreation/PollCreationImage.tsx
@@ -1,13 +1,18 @@
import React, { useState, useEffect } from 'react';
import { useFilePicker, utils } from 'react-sage';
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,
+ Button
+} from '@material-ui/core';
+import { CloudUpload, CancelOutlined, Link, Publish } from '@material-ui/icons/';
+import UploadImage from '../../components/UploadImage/UploadImage';
interface PropTypes {
- file: File | undefined;
- setFile: (file: File | undefined) => void;
+ file?: File | string;
+ setFile: (file?: File | string) => void;
}
const useStyles = makeStyles({
@@ -15,7 +20,8 @@ const useStyles = makeStyles({
display: 'flex',
justifyContent: 'center',
flexDirection: 'column',
- alignItems: 'center'
+ alignItems: 'center',
+ width: '50%'
},
clearIcon: {
opacity: '.5',
@@ -38,7 +44,8 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => {
const classes = useStyles();
const { files, onClick, HiddenFileInput } = useFilePicker();
const [url, setUrl] = useState<string>();
- const [isMediaHover, setIsMediaHover] = useState(false);
+ const [isMediaHover, setIsMediaHover] = useState<boolean>(false);
+ const [isModalOpen, setIsModalOpen] = useState<boolean>(false);
const handleMouseEnter = (): void => {
setIsMediaHover(true);
@@ -56,19 +63,37 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => {
}
}, [files, setFile]);
-
- const handleClick = () => {
- if (file) {
- setFile(undefined);
- } else onClick();
+ const handleOpenModal = () => {
+ setIsModalOpen(true);
};
+ const callback = (result: string) => {
+ setUrl(result);
+ setFile(result);
+ };
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} />
+ <Button
+ onClick={onClick}
+ variant="contained"
+ color="primary"
+ size="large"
+ startIcon={<CloudUpload />}
+ >
+ Upload
+ </Button>
+ <Typography variant="h6"> or </Typography>
+ <Button
+ onClick={handleOpenModal}
+ variant="outlined"
+ color="primary"
+ startIcon={<Link />}
+ >
+ Attach a link
+ </Button>
+ <UploadImage callback={callback} isOpen={isModalOpen} setIsOpen={setIsModalOpen} />
</>
);
@@ -79,16 +104,14 @@ const PollCreationImage: React.FC<PropTypes> = ({ file, setFile }) => {
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
- {isMediaHover && <CancelOutlinedIcon className={classes.clearIcon} />}
+ {isMediaHover && <CancelOutlined className={classes.clearIcon} />}
</CardMedia>
);
return (
- <>
- <CardActionArea onClick={handleClick} className={classes.root}>
- {file ? (url && Media) : Upload}
- </CardActionArea>
- </>
+ <div className={classes.root}>
+ {file ? (url && Media) : Upload}
+ </div>
);
};