From 7ad127942bb12ee9de691e10dc9386849459ea46 Mon Sep 17 00:00:00 2001 From: ilyayudovin Date: Sun, 28 Jun 2020 19:09:20 +0300 Subject: feat: add poll submission component --- src/components/UploadImage/UploadImage.tsx | 28 +++++----- src/index.tsx | 2 +- src/pages/FeedPage/FeedPage.tsx | 14 +++-- src/pages/FeedPage/PollSubmission.tsx | 87 ++++++++++++++++++++++++++++++ src/pages/FeedPage/PollSubmissionImage.tsx | 60 +++++++++++++++++++++ src/pages/FeedPage/types.ts | 7 +++ src/pages/ProfilePage/ProfileInfo.tsx | 12 ++++- src/types.d.ts | 1 - 8 files changed, 190 insertions(+), 21 deletions(-) create mode 100644 src/pages/FeedPage/PollSubmission.tsx create mode 100644 src/pages/FeedPage/PollSubmissionImage.tsx create mode 100644 src/pages/FeedPage/types.ts (limited to 'src') diff --git a/src/components/UploadImage/UploadImage.tsx b/src/components/UploadImage/UploadImage.tsx index 42ee989..464a9cf 100644 --- a/src/components/UploadImage/UploadImage.tsx +++ b/src/components/UploadImage/UploadImage.tsx @@ -1,4 +1,4 @@ -import React, { useRef } from 'react'; +import React, { useRef, useState } from 'react'; import Button from '@material-ui/core/Button'; import TextField from '@material-ui/core/TextField'; import Dialog from '@material-ui/core/Dialog'; @@ -6,35 +6,32 @@ import DialogActions from '@material-ui/core/DialogActions'; import DialogContent from '@material-ui/core/DialogContent'; import DialogContentText from '@material-ui/core/DialogContentText'; import DialogTitle from '@material-ui/core/DialogTitle'; -import { User } from 'which-types'; -import { patch } from '../../requests'; interface PropTypes { displayD: boolean; setDisplayD: (d: boolean) => void; - setUserInfo: (a: User) => void; - setUser: (a: User) => void + callback: (a: string) => void; } const UploadImage: React.FC = ({ - displayD, setDisplayD, setUserInfo, setUser + displayD, setDisplayD, callback }) => { - const urlRef = useRef(null); + const urlRef = useRef(null); + const [url, setUrl] = useState(''); const handleClose = () => { setDisplayD(false); }; - const updateAvatar = () => { - const id = localStorage.getItem('userId'); - const newAvatar = urlRef.current?.value; - patch(`/users/${id}`, { avatarUrl: newAvatar }).then(res => { - setUserInfo(res.data); - setUser(res.data); - }); + const update = () => { + callback(urlRef.current?.value || ''); setDisplayD(false); }; + const handleChange = (event:React.ChangeEvent) => { + setUrl(event.target.value); + }; + return (
@@ -52,13 +49,14 @@ const UploadImage: React.FC = ({ fullWidth autoComplete="off" inputRef={urlRef} + onChange={handleChange} /> - diff --git a/src/index.tsx b/src/index.tsx index 49c177b..02f7969 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -104,7 +104,7 @@ const App: React.FC = () => { setUser={setUser} /> ) } - { page.prefix === 'feed' && } + { page.prefix === 'feed' && } { page.prefix === 'auth' && }
diff --git a/src/pages/FeedPage/FeedPage.tsx b/src/pages/FeedPage/FeedPage.tsx index b7d719e..afe2ef7 100644 --- a/src/pages/FeedPage/FeedPage.tsx +++ b/src/pages/FeedPage/FeedPage.tsx @@ -1,14 +1,17 @@ import React, { useState, useEffect } from 'react'; -import { Poll } from 'which-types'; +import { Poll, User } from 'which-types'; import Feed from '../../components/Feed/Feed'; import { get } from '../../requests'; +import PollSubmission from './PollSubmission'; + interface PropTypes { navigate: (prefix: string, id: string) => void; + user: User | undefined; } -const FeedPage: React.FC = ({ navigate }) => { +const FeedPage: React.FC = ({ navigate, user }) => { const [polls, setPolls] = useState([]); useEffect(() => { @@ -17,7 +20,12 @@ const FeedPage: React.FC = ({ navigate }) => { }); }, []); - return ; + return ( + <> + {user && } + + + ); }; export default FeedPage; diff --git a/src/pages/FeedPage/PollSubmission.tsx b/src/pages/FeedPage/PollSubmission.tsx new file mode 100644 index 0000000..c76e9fb --- /dev/null +++ b/src/pages/FeedPage/PollSubmission.tsx @@ -0,0 +1,87 @@ +import React, { useState } from 'react'; +import { makeStyles } from '@material-ui/core/styles'; +import Collapse from '@material-ui/core/Collapse'; +import { + Button, Card, CardMedia, ClickAwayListener, Divider +} from '@material-ui/core'; +import { User } from 'which-types'; +import PollSubmissionImage from './PollSubmissionImage'; +import UserStrip from '../../components/UserStrip/UserStrip'; +import { post } from '../../requests'; +import { Contents } from './types'; + + +interface PropTypes{ + user: User; +} +const useStyles = makeStyles(theme => ({ + root: { + textAlign: 'center', + cursor: 'pointer' + }, + card: { + height: 400, + display: 'flex' + }, + images: { + height: theme.spacing(50), + width: 300, + display: 'flex', + justifyContent: 'center', + alignItems: 'center', + cursor: 'pointer' + }, + button: { + width: '100%' + } +})); + +const PollSubmission: React.FC = ({ user }) => { + const classes = useStyles(); + const [expanded, setExpanded] = useState(false); + const [contents, setContents] = useState({ + left: { + url: '' + }, + right: { + url: '' + } + }); + + const handleClickAway = () => { + setExpanded(false); + }; + + const handleClick = () => { + if (expanded) { + post('/polls/', { authorId: user._id, contents }).then(res => { + console.log(res.data); + }); + } + setExpanded(!expanded); + }; + + return ( + + + + {}} /> + + + + + + + + + + ); +}; + +export default PollSubmission; diff --git a/src/pages/FeedPage/PollSubmissionImage.tsx b/src/pages/FeedPage/PollSubmissionImage.tsx new file mode 100644 index 0000000..c7e638c --- /dev/null +++ b/src/pages/FeedPage/PollSubmissionImage.tsx @@ -0,0 +1,60 @@ +import React, { useState } from 'react'; +import { makeStyles } from '@material-ui/core/styles'; +import CloudUploadIcon from '@material-ui/icons/CloudUpload'; +import { CardActionArea, CardMedia } from '@material-ui/core'; +import UploadImage from '../../components/UploadImage/UploadImage'; +import { Contents } from './types'; + +interface PropTypes { + setContents: (a: Contents) => void; + which: 'left' | 'right'; +} + +const useStyles = makeStyles(theme => ({ + images: { + height: theme.spacing(50), + width: 300, + display: 'flex', + justifyContent: 'center', + alignItems: 'center', + cursor: 'pointer' + } +})); + +const PollSubmissionImage: React.FC = ({ setContents, which }) => { + const classes = useStyles(); + const [display, setDisplay] = useState(false); + const [image, setImage] = useState(''); + + const handleClick = () => { + setDisplay(!display); + }; + + const patchUrl = (url: string) => { + setImage(url); + let nextImage; + which === 'left' ? nextImage = 'right' : nextImage = 'left'; + setContents({ + [which]: { + url + }, + [nextImage]: { + url + } + }); + }; + + + return ( + <> + + + + + + + + ); +}; + +export default PollSubmissionImage; diff --git a/src/pages/FeedPage/types.ts b/src/pages/FeedPage/types.ts new file mode 100644 index 0000000..a3c95ff --- /dev/null +++ b/src/pages/FeedPage/types.ts @@ -0,0 +1,7 @@ +export interface ImageData { + url: string; +} +export interface Contents { + left?: ImageData; + right?: ImageData; +} diff --git a/src/pages/ProfilePage/ProfileInfo.tsx b/src/pages/ProfilePage/ProfileInfo.tsx index 2b9227e..fada65f 100644 --- a/src/pages/ProfilePage/ProfileInfo.tsx +++ b/src/pages/ProfilePage/ProfileInfo.tsx @@ -7,6 +7,7 @@ import VerifiedIcon from '@material-ui/icons/CheckCircleOutline'; import MoreMenu from './MoreMenu'; import Highlight from './Highlight'; import UploadImage from '../../components/UploadImage/UploadImage'; +import { patch } from '../../requests'; interface PropTypes { @@ -82,12 +83,21 @@ const ProfileInfo: React.FC = ({ const classes = useStyles(); const [input, setInput] = useState(false); + const dateSince = new Date(user?.createdAt || '').toLocaleDateString(); const handleClick = () => { setInput(!input); }; + const patchAvatar = (url: string) => { + const id = localStorage.getItem('userId'); + patch(`/users/${id}`, { avatarUrl: url }).then(res => { + setUserInfo(res.data); + setUser(res.data); + }); + }; + return (
{ @@ -112,7 +122,7 @@ const ProfileInfo: React.FC = ({
- + ) : diff --git a/src/types.d.ts b/src/types.d.ts index 73346ce..4b1ffd6 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -2,4 +2,3 @@ export interface Page { prefix: string; id: string; } - -- cgit v1.2.3 From 28c80d1c2e33706a3a754b3e5e26dc2685cf8592 Mon Sep 17 00:00:00 2001 From: ilyayudovin Date: Sun, 28 Jun 2020 19:32:48 +0300 Subject: fix: able to add 2 different images to poll submission --- src/pages/FeedPage/PollSubmission.tsx | 4 ++-- src/pages/FeedPage/PollSubmissionImage.tsx | 15 ++++----------- 2 files changed, 6 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/pages/FeedPage/PollSubmission.tsx b/src/pages/FeedPage/PollSubmission.tsx index c76e9fb..40ca3d7 100644 --- a/src/pages/FeedPage/PollSubmission.tsx +++ b/src/pages/FeedPage/PollSubmission.tsx @@ -68,8 +68,8 @@ const PollSubmission: React.FC = ({ user }) => { {}} /> - - + + - diff --git a/src/pages/FeedPage/FeedPage.tsx b/src/pages/FeedPage/FeedPage.tsx index a7bbbbd..329647e 100644 --- a/src/pages/FeedPage/FeedPage.tsx +++ b/src/pages/FeedPage/FeedPage.tsx @@ -22,7 +22,7 @@ const FeedPage: React.FC = ({ navigate, user }) => { return ( <> - {user && } + {user && } ); diff --git a/src/pages/FeedPage/PollSubmission.tsx b/src/pages/FeedPage/PollSubmission.tsx index 65e33b6..3a73d45 100644 --- a/src/pages/FeedPage/PollSubmission.tsx +++ b/src/pages/FeedPage/PollSubmission.tsx @@ -38,7 +38,7 @@ const useStyles = makeStyles(theme => ({ } })); -const PollSubmission: React.FC = ({ user , polls, setPolls}) => { +const PollSubmission: React.FC = ({ user, polls, setPolls }) => { const classes = useStyles(); const [expanded, setExpanded] = useState(false); const [contents, setContents] = useState({ @@ -56,9 +56,9 @@ const PollSubmission: React.FC = ({ user , polls, setPolls}) => { const handleClick = () => { if (expanded) { - if(contents.left.url && contents.right.url ) { - post('/polls/', {authorId: user._id, contents}).then(res => { - polls.unshift({...res.data}); + if (contents.left.url && contents.right.url) { + post('/polls/', { authorId: user._id, contents }).then(res => { + polls.unshift({ ...res.data }); setPolls([...polls]); }); } @@ -73,13 +73,13 @@ const PollSubmission: React.FC = ({ user , polls, setPolls}) => { {}} /> - - + + diff --git a/src/pages/FeedPage/PollSubmissionImage.tsx b/src/pages/FeedPage/PollSubmissionImage.tsx index 2dadbee..757083b 100644 --- a/src/pages/FeedPage/PollSubmissionImage.tsx +++ b/src/pages/FeedPage/PollSubmissionImage.tsx @@ -1,7 +1,7 @@ import React, { useState } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import CloudUploadIcon from '@material-ui/icons/CloudUpload'; -import { CardActionArea, CardMedia } from '@material-ui/core'; +import { CardActionArea, CardMedia, Typography } from '@material-ui/core'; import UploadImage from '../../components/UploadImage/UploadImage'; import { Contents } from './types'; import CancelOutlinedIcon from '@material-ui/icons/CancelOutlined'; @@ -13,24 +13,29 @@ interface PropTypes { } const useStyles = makeStyles(theme => ({ - images: { - height: theme.spacing(50), - width: 300, + root: { display: 'flex', justifyContent: 'center', - alignItems: 'center', - cursor: 'pointer', - boxShadow: 'inset 0 0 10px;' + flexDirection: 'column', + alignItems: 'center' }, clearIcon: { opacity: '.5', - fontSize: 100 + fontSize: 50 + }, + media: { + height: '100%', + width: '100%', + display: 'flex', + justifyContent: 'center', + alignItems: 'center' } })); + const PollSubmissionImage: React.FC = ({ setContents, which, contents }) => { const classes = useStyles(); - const [display, setDisplay] = useState(false); + const [isModalOpen, setIsModalOpen] = useState(false); const [image, setImage] = useState(''); const [clearIconDisplay, setClearIconDisplay] = useState(false); @@ -41,9 +46,7 @@ const PollSubmissionImage: React.FC = ({ setContents, which, contents }; const handleClick = () => { - image === '' - ? setDisplay(!display) - : patchUrl(''); + image ? patchUrl('') : setIsModalOpen(!isModalOpen); }; const handleMouseEnter = () => { @@ -54,27 +57,31 @@ const PollSubmissionImage: React.FC = ({ setContents, which, contents setClearIconDisplay(false); }; - return ( + + const Upload = ( <> - - - { - image === '' - ? - : clearIconDisplay - ? - : null - } - - - + + Upload an image + ); + + const Media = ( + + {clearIconDisplay && } + + ) + + return ( + + {image ? Media : Upload} + + ); }; export default PollSubmissionImage; diff --git a/src/pages/ProfilePage/ProfileInfo.tsx b/src/pages/ProfilePage/ProfileInfo.tsx index ec6c387..9fe5912 100644 --- a/src/pages/ProfilePage/ProfileInfo.tsx +++ b/src/pages/ProfilePage/ProfileInfo.tsx @@ -121,7 +121,7 @@ const ProfileInfo: React.FC = ({ - + ) : -- cgit v1.2.3 From c68b0e0c3ad72a48ba421dbbc9b70d177f8ecbfc Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 29 Jun 2020 21:34:27 +0300 Subject: style: fix eslint errors --- src/pages/FeedPage/PollSubmission.tsx | 2 +- src/pages/FeedPage/PollSubmissionImage.tsx | 25 ++++++++++++++----------- 2 files changed, 15 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/pages/FeedPage/PollSubmission.tsx b/src/pages/FeedPage/PollSubmission.tsx index 38f70c3..9b1ac95 100644 --- a/src/pages/FeedPage/PollSubmission.tsx +++ b/src/pages/FeedPage/PollSubmission.tsx @@ -68,7 +68,7 @@ const PollSubmission: React.FC = ({ user, polls, setPolls }) => { diff --git a/src/pages/FeedPage/PollSubmissionImage.tsx b/src/pages/FeedPage/PollSubmissionImage.tsx index 3c2eb8e..1e9fa0e 100644 --- a/src/pages/FeedPage/PollSubmissionImage.tsx +++ b/src/pages/FeedPage/PollSubmissionImage.tsx @@ -3,15 +3,12 @@ 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 _ from 'lodash'; import UploadImage from '../../components/UploadImage/UploadImage'; -import { Contents } from './types'; interface PropTypes { - contents: Contents; - setContents: (newContents: Contents) => void; - which: 'left' | 'right'; + url: string; + setUrl: (url: string) => void; } const useStyles = makeStyles({ @@ -35,29 +32,22 @@ const useStyles = makeStyles({ }); -const PollSubmissionImage: React.FC = ({ setContents, which, contents }) => { +const PollSubmissionImage: React.FC = ({ url, setUrl }) => { const classes = useStyles(); const [isModalOpen, setIsModalOpen] = useState(false); - const [image, setImage] = useState(''); - const [clearIconDisplay, setClearIconDisplay] = useState(false); - - const patchUrl = (url: string): void => { - setImage(url); - const newContents = _.set(contents, which, { url }); - setContents({ ...newContents }); - }; + const [isMediaHover, setIsMediaHover] = useState(false); const handleClick = (): void => { - if (image) patchUrl(''); + if (url) setUrl(''); else setIsModalOpen(!isModalOpen); }; const handleMouseEnter = (): void => { - setClearIconDisplay(true); + setIsMediaHover(true); }; const handleMouseLeave = (): void => { - setClearIconDisplay(false); + setIsMediaHover(false); }; @@ -65,24 +55,24 @@ const PollSubmissionImage: React.FC = ({ setContents, which, contents <> Upload an image - + ); const Media = ( - {clearIconDisplay && } + {isMediaHover && } ); return ( - {image ? Media : Upload} + {url ? Media : Upload} ); }; -- cgit v1.2.3