diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-06-14 20:44:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-14 20:44:40 +0300 |
commit | 65e41d1d8a3844a6d7268340f5d88b5957e2355d (patch) | |
tree | 3d455f133b5afa1e6a1c30c4589ef3db39dd4c4e | |
parent | 99b44bc80fa3228131a05fccb13f75ff8a46b116 (diff) | |
parent | fbe489c83e9ef4c03b87624a4dec66de61af364a (diff) | |
download | which-ui-65e41d1d8a3844a6d7268340f5d88b5957e2355d.tar.gz |
Merge pull request #33 from ilyayudovin/pages
divide src into Pages and Components directories
-rw-r--r-- | src/components/Feed/Feed.tsx (renamed from src/Feed/Feed.tsx) | 20 | ||||
-rw-r--r-- | src/components/Header/Header.tsx (renamed from src/Header/Header.tsx) | 0 | ||||
-rw-r--r-- | src/components/Header/SearchBar.tsx (renamed from src/Header/SearchBar.tsx) | 0 | ||||
-rw-r--r-- | src/components/PollCard/PollCard.tsx (renamed from src/PollCard/PollCard.tsx) | 2 | ||||
-rw-r--r-- | src/index.tsx | 25 | ||||
-rw-r--r-- | src/pages/AuthPage/AuthPage.tsx | 14 | ||||
-rw-r--r-- | src/pages/AuthPage/SignInForm.tsx (renamed from src/Form/SignInForm.tsx) | 4 | ||||
-rw-r--r-- | src/pages/FeedPage/FeedPage.tsx | 19 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfileInfo.tsx (renamed from src/ProfileInfo/ProfileInfo.tsx) | 28 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfilePage.tsx | 32 |
10 files changed, 92 insertions, 52 deletions
diff --git a/src/Feed/Feed.tsx b/src/components/Feed/Feed.tsx index d7bfce1..8dc3ec1 100644 --- a/src/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 { 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/Header/Header.tsx b/src/components/Header/Header.tsx index 0ee6b5f..0ee6b5f 100644 --- a/src/Header/Header.tsx +++ b/src/components/Header/Header.tsx diff --git a/src/Header/SearchBar.tsx b/src/components/Header/SearchBar.tsx index 182a1a4..182a1a4 100644 --- a/src/Header/SearchBar.tsx +++ b/src/components/Header/SearchBar.tsx diff --git a/src/PollCard/PollCard.tsx b/src/components/PollCard/PollCard.tsx index b639f25..8995a30 100644 --- a/src/PollCard/PollCard.tsx +++ b/src/components/PollCard/PollCard.tsx @@ -7,7 +7,7 @@ import { Avatar, CardHeader } from '@material-ui/core/'; -import { Poll } from '../types'; +import { Poll } from '../../types'; interface PropTypes { poll: Poll; diff --git a/src/index.tsx b/src/index.tsx index 0855038..55bf773 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -9,11 +9,10 @@ import { CssBaseline } from '@material-ui/core'; import teal from '@material-ui/core/colors/teal'; import 'typeface-roboto'; -import Header from './Header/Header'; -import Feed from './Feed/Feed'; -import ProfileInfo from './ProfileInfo/ProfileInfo'; - -import SignInForm from './Form/SignInForm'; +import Header from './components/Header/Header'; +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'; @@ -38,6 +37,11 @@ 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) { @@ -56,15 +60,10 @@ const App: React.FC = () => { page === 'profile' ? ( user - ? ( - <> - <ProfileInfo id={user?._id || ''} setUser={setUser} /> - <Feed page={page} /> - </> - ) - : <SignInForm setUser={setUser} /> + ? <ProfilePage logOut={logOut} id={user?._id} /> + : <AuthPage setUser={setUser} /> ) - : <Feed page={page} /> + : <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/Form/SignInForm.tsx b/src/pages/AuthPage/SignInForm.tsx index efe85ed..6e27535 100644 --- a/src/Form/SignInForm.tsx +++ b/src/pages/AuthPage/SignInForm.tsx @@ -2,8 +2,8 @@ import React, { useRef } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import TextField from '@material-ui/core/TextField'; import Button from '@material-ui/core/Button'; -import { User } from '../types'; -import { get } from '../requests'; +import { User } from '../../types'; +import { get } from '../../requests'; interface PropTypes { setUser: (newUser: User) => void; 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/ProfileInfo/ProfileInfo.tsx b/src/pages/ProfilePage/ProfileInfo.tsx index 693f550..c2f242a 100644 --- a/src/ProfileInfo/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'; +import { User } from '../../types'; 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 new file mode 100644 index 0000000..1dd71d3 --- /dev/null +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -0,0 +1,32 @@ +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 { + logOut: () => void; + id: string; +} + +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 ( + <> + <ProfileInfo user={userInfo} logOut={logOut} /> + <Feed polls={polls} /> + </> + ); +}; + +export default ProfilePage; |