diff options
author | eug-vs <eug-vs@keemail.me> | 2020-06-14 20:39:16 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-06-14 20:39:16 +0300 |
commit | 61a424debfbfa98570e070fbf25d03aa9c56d679 (patch) | |
tree | 0c032edf74571350fd0f5f9251a3493a487b6008 /src | |
parent | c7f2999ee797ea5e3bfb29517a4f13206162cc6f (diff) | |
download | which-ui-61a424debfbfa98570e070fbf25d03aa9c56d679.tar.gz |
refactor: structurize pages
Diffstat (limited to 'src')
-rw-r--r-- | src/components/Feed/Feed.tsx | 18 | ||||
-rw-r--r-- | src/index.tsx | 17 | ||||
-rw-r--r-- | src/pages/AuthPage/AuthPage.tsx | 14 | ||||
-rw-r--r-- | src/pages/AuthPage/SignInForm.tsx (renamed from src/pages/ProfilePage/SignInForm.tsx) | 0 | ||||
-rw-r--r-- | src/pages/FeedPage/FeedPage.tsx | 19 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfileInfo.tsx | 26 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfilePage.tsx | 37 |
7 files changed, 79 insertions, 52 deletions
diff --git a/src/components/Feed/Feed.tsx b/src/components/Feed/Feed.tsx index 604c167..8dc3ec1 100644 --- a/src/components/Feed/Feed.tsx +++ b/src/components/Feed/Feed.tsx @@ -1,11 +1,10 @@ -import React, { useState, useEffect } from 'react'; +import React from 'react'; import { makeStyles } from '@material-ui/core/styles'; import { Poll } from '../../types'; import PollCard from '../PollCard/PollCard'; -import { get } from '../../requests'; interface PropTypes { - page: string; + polls: Poll[]; } const useStyles = makeStyles(theme => ({ @@ -16,20 +15,9 @@ const useStyles = makeStyles(theme => ({ } })); -const Feed: React.FC<PropTypes> = ({ page }) => { - const [polls, setPolls] = useState<Poll[]>([]); +const Feed: React.FC<PropTypes> = ({ polls }) => { const classes = useStyles(); - let endpoint = '/polls'; - // TODO: Make this work - if (page === 'feed') endpoint = '/polls'; - - useEffect(() => { - get(endpoint).then(response => { - setPolls(response.data); - }); - }, [endpoint]); - return ( <div className={classes.root}> {polls.map(poll => <PollCard poll={poll} key={poll._id} />)} diff --git a/src/index.tsx b/src/index.tsx index 5888170..5ab1910 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -10,8 +10,9 @@ import teal from '@material-ui/core/colors/teal'; import 'typeface-roboto'; import Header from './components/Header/Header'; -import Feed from './components/Feed/Feed'; import ProfilePage from './pages/ProfilePage/ProfilePage'; +import FeedPage from './pages/FeedPage/FeedPage'; +import AuthPage from './pages/AuthPage/AuthPage'; import { User } from './types'; import { get } from './requests'; @@ -36,6 +37,12 @@ const App: React.FC = () => { const [user, setUser] = React.useState<User | undefined>(); const classes = useStyles(); + const logOut = () => { + localStorage.removeItem('userId'); + setUser(undefined); + }; + + useEffect(() => { const userId = localStorage.getItem('userId'); if (userId) { @@ -52,8 +59,12 @@ const App: React.FC = () => { <div className={classes.root}> { page === 'profile' - ? <ProfilePage setUser={setUser} user={user} /> - : <Feed page={page} /> + ? ( + user + ? <ProfilePage logOut={logOut} id={user?._id} /> + : <AuthPage setUser={setUser} /> + ) + : <FeedPage /> } </div> </ThemeProvider> diff --git a/src/pages/AuthPage/AuthPage.tsx b/src/pages/AuthPage/AuthPage.tsx new file mode 100644 index 0000000..d9c43d3 --- /dev/null +++ b/src/pages/AuthPage/AuthPage.tsx @@ -0,0 +1,14 @@ +import React from 'react'; +import { User } from '../../types'; +import SignInForm from './SignInForm'; + +interface PropTypes { + setUser: (newUser: User | undefined) => void; +} + +const AuthPage: React.FC<PropTypes> = ({ setUser }) => { + return <SignInForm setUser={setUser} />; +}; + +export default AuthPage; + diff --git a/src/pages/ProfilePage/SignInForm.tsx b/src/pages/AuthPage/SignInForm.tsx index 6e27535..6e27535 100644 --- a/src/pages/ProfilePage/SignInForm.tsx +++ b/src/pages/AuthPage/SignInForm.tsx diff --git a/src/pages/FeedPage/FeedPage.tsx b/src/pages/FeedPage/FeedPage.tsx new file mode 100644 index 0000000..03bacfd --- /dev/null +++ b/src/pages/FeedPage/FeedPage.tsx @@ -0,0 +1,19 @@ +import React, { useState, useEffect } from 'react'; +import { Poll } from '../../types'; +import Feed from '../../components/Feed/Feed'; +import { get } from '../../requests'; + +const FeedPage: React.FC = () => { + const [polls, setPolls] = useState<Poll[]>([]); + + useEffect(() => { + get('/polls').then(response => { + setPolls(response.data); + }); + }, []); + + return <Feed polls={polls} />; +}; + +export default FeedPage; + diff --git a/src/pages/ProfilePage/ProfileInfo.tsx b/src/pages/ProfilePage/ProfileInfo.tsx index 8fce8df..c2f242a 100644 --- a/src/pages/ProfilePage/ProfileInfo.tsx +++ b/src/pages/ProfilePage/ProfileInfo.tsx @@ -1,13 +1,12 @@ -import React, { useState } from 'react'; +import React from 'react'; import { Avatar } from '@material-ui/core/'; import { makeStyles } from '@material-ui/core/styles'; import Button from '@material-ui/core/Button/Button'; import { User } from '../../types'; -import { get } from '../../requests'; interface PropTypes { - id: string; - setUser: (newUser: User | undefined) => void; + user: User | undefined; + logOut: () => void; } const useStyles = makeStyles({ @@ -36,25 +35,14 @@ const useStyles = makeStyles({ } }); -const ProfileInfo: React.FC<PropTypes> = ({ id, setUser }) => { - const [userInfo, setUserInfo] = useState<User>(); - - get(`/users/${id}`).then(response => { - setUserInfo(response.data); - }); - +const ProfileInfo: React.FC<PropTypes> = ({ user, logOut }) => { const classes = useStyles(); - const LogOut = () => { - localStorage.clear(); - setUser(undefined); - }; - return ( <div> - <Avatar className={classes.avatar} src={userInfo?.avatarUrl} /> + <Avatar className={classes.avatar} src={user?.avatarUrl} /> <div className={classes.name}> - {userInfo?.name} + {user?.name} </div> <div className={classes.profileMenu}> <div style={{ borderBottom: '1px solid green', color: 'green' }} className={classes.menuButton}> @@ -67,7 +55,7 @@ const ProfileInfo: React.FC<PropTypes> = ({ id, setUser }) => { Following </div> </div> - <Button variant="contained" onClick={LogOut}>Log Out</Button> + <Button variant="contained" onClick={logOut}>Log Out</Button> </div> ); }; diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index ee95769..023f37c 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -1,25 +1,32 @@ -import React from 'react'; -import { User } from '../../types'; -import SignInForm from './SignInForm'; +import React, { useState } from 'react'; +import { User, Poll } from '../../types'; import ProfileInfo from './ProfileInfo'; import Feed from '../../components/Feed/Feed'; +import { get } from '../../requests'; interface PropTypes { - setUser: (newUser: User | undefined) => void; - user: User | undefined; + logOut: () => void; + id: string; } -const ProfilePage: React.FC<PropTypes> = ({ setUser, user }) => { +const ProfilePage: React.FC<PropTypes> = ({ logOut, id }) => { + const [userInfo, setUserInfo] = useState<User>(); + const [polls, setPolls] = useState<Poll[]>([]); + + get(`/users/${id}`).then(response => { + setUserInfo(response.data); + }); + + get(`/profiles/${id}`).then(response => { + setPolls(response.data); + }); + return ( - user - ? ( - <> - <ProfileInfo id={user?._id} setUser={setUser} /> - <Feed page="Feed" /> - </> - ) - : <SignInForm setUser={setUser} /> - ); + <> + <ProfileInfo user={userInfo} logOut={logOut} /> + <Feed polls={polls} /> + </> + ) }; export default ProfilePage; |