diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/components/Header/Header.tsx | 3 | ||||
-rw-r--r-- | src/hooks/useAuth.tsx | 2 | ||||
-rw-r--r-- | src/pages/AuthPage/SignInForm.tsx | 5 | ||||
-rw-r--r-- | src/pages/AuthPage/SignUpForm.tsx | 8 | ||||
-rw-r--r-- | src/pages/ProfilePage/MoreMenu.tsx | 9 | ||||
-rw-r--r-- | src/pages/ProfilePage/ProfilePage.tsx | 36 |
6 files changed, 36 insertions, 27 deletions
diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx index 546ecc3..72e40f8 100644 --- a/src/components/Header/Header.tsx +++ b/src/components/Header/Header.tsx @@ -40,7 +40,8 @@ const Header: React.FC = () => { }; const handleProfile = (): void => { - navigate('profile'); + if (user) navigate('profile'); + else navigate('auth'); }; const handleNotifications = (): void => {}; diff --git a/src/hooks/useAuth.tsx b/src/hooks/useAuth.tsx index 00d0c7c..de64c46 100644 --- a/src/hooks/useAuth.tsx +++ b/src/hooks/useAuth.tsx @@ -33,7 +33,6 @@ const useProvideAuth = () => { const me = response.data.user; const token = response.data.accessToken; setUser(me); - // navigate('profile', me._id); localStorage.setItem('userId', me._id); localStorage.setItem('token', token); if (!remember) localStorage.setItem('shouldClear', 'true'); @@ -45,7 +44,6 @@ const useProvideAuth = () => { setUser(null); localStorage.removeItem('userId'); localStorage.removeItem('token'); - // navigate('auth'); }; const isAuthenticated = () => Boolean(user); diff --git a/src/pages/AuthPage/SignInForm.tsx b/src/pages/AuthPage/SignInForm.tsx index 662a312..e68483b 100644 --- a/src/pages/AuthPage/SignInForm.tsx +++ b/src/pages/AuthPage/SignInForm.tsx @@ -7,6 +7,7 @@ import { Switch } from '@material-ui/core'; import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from '../../hooks/useNavigate'; const useStyles = makeStyles(theme => ({ root: { @@ -32,6 +33,7 @@ const SignInForm: React.FC = () => { const nameRef = useRef<HTMLInputElement>(); const passwordRef = useRef<HTMLInputElement>(); const { login } = useAuth(); + const { navigate } = useNavigate(); const handleCheck = () => { setRemember(!remember); @@ -42,7 +44,8 @@ const SignInForm: React.FC = () => { const password = passwordRef.current?.value; if (name && password) { login(name, password, remember).then(success => { - if (!success) setError(true); + if (success) navigate('profile'); + else setError(true); }); } }; diff --git a/src/pages/AuthPage/SignUpForm.tsx b/src/pages/AuthPage/SignUpForm.tsx index af7a0f8..1dacd45 100644 --- a/src/pages/AuthPage/SignUpForm.tsx +++ b/src/pages/AuthPage/SignUpForm.tsx @@ -4,6 +4,7 @@ import TextField from '@material-ui/core/TextField'; import Button from '@material-ui/core/Button'; import { post } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from '../../hooks/useNavigate'; const useStyles = makeStyles(theme => ({ @@ -30,15 +31,16 @@ const SignUpForm: React.FC = () => { const emailRef = useRef<HTMLInputElement>(); const passwordRef = useRef<HTMLInputElement>(); const { login } = useAuth(); + const { navigate } = useNavigate(); const onClick = () => { const username = usernameRef.current?.value; const password = passwordRef.current?.value; const email = emailRef.current?.value; if (username && password) { - post('/users', { username, password, email }).then(() => { - login(username, password); - }); + post('/users', { username, password, email }) + .then(() => login(username, password)) + .then(() => navigate('profile')); } else setError(true); }; diff --git a/src/pages/ProfilePage/MoreMenu.tsx b/src/pages/ProfilePage/MoreMenu.tsx index 7c17f1e..4e681f5 100644 --- a/src/pages/ProfilePage/MoreMenu.tsx +++ b/src/pages/ProfilePage/MoreMenu.tsx @@ -5,6 +5,7 @@ import MenuItem from '@material-ui/core/MenuItem'; import MoreHorizIcon from '@material-ui/icons/MoreHoriz'; import { makeStyles } from '@material-ui/core'; import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from '../../hooks/useNavigate'; const ITEM_HEIGHT = 48; @@ -20,6 +21,7 @@ const MoreMenu: React.FC = () => { const classes = useStyles(); const [anchorEl, setAnchorEl] = React.useState<HTMLButtonElement | null>(null); const { logout } = useAuth(); + const { navigate } = useNavigate(); const open = Boolean(anchorEl); @@ -27,6 +29,11 @@ const MoreMenu: React.FC = () => { setAnchorEl(event.currentTarget); }; + const handleLogout = () => { + logout(); + navigate('auth'); + }; + const handleClose = () => { setAnchorEl(null); }; @@ -55,7 +62,7 @@ const MoreMenu: React.FC = () => { } }} > - <MenuItem onClick={logout}>Log out</MenuItem> + <MenuItem onClick={handleLogout}>Log out</MenuItem> </Menu> </div> </div> diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index d354a45..ca39746 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -12,28 +12,26 @@ const ProfilePage: React.FC = () => { const [userInfo, setUserInfo] = useState<User>(); const [polls, setPolls] = useState<Poll[]>([]); const [totalVotes, setTotalVotes] = useState<number>(0); - const { page } = useNavigate(); + const { page, navigate } = useNavigate(); const { user } = useAuth(); - const id = page?.id || user?._id; - - useEffect(() => { - get(`/users/${id}`).then(response => { - setUserInfo(response.data); - }); - }, [id]); - useEffect(() => { - get(`/profiles/${id}`).then(response => { - setPolls(response.data); - setTotalVotes(response.data.reduce( - (total: number, current: Poll) => { - const { left, right } = current.contents; - return total + left.votes + right.votes; - }, 0 - )); - }); - }, [id, userInfo]); + const id = page?.id || user?._id + if (id) { + get(`/users/${id}`).then(response => { + setUserInfo(response.data); + }); + get(`/profiles/${id}`).then(response => { + setPolls(response.data); + setTotalVotes(response.data.reduce( + (total: number, current: Poll) => { + const { left, right } = current.contents; + return total + left.votes + right.votes; + }, 0 + )); + }); + } else navigate('auth'); + }, [navigate, page, user]); return ( <> |