From 46e360a23969bde69f6d80b5ff27401a39169be6 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 8 Aug 2020 09:07:08 +0300 Subject: feat: use router logic in ProfilePage --- src/pages/ProfilePage/ProfilePage.tsx | 39 ++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'src/pages/ProfilePage/ProfilePage.tsx') diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index 34c9efa..b81c70f 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -1,4 +1,5 @@ import React, { useState, useEffect } from 'react'; +import { useHistory, useParams } from 'react-router-dom'; import { User, Poll } from 'which-types'; import { Container } from '@material-ui/core'; @@ -6,28 +7,42 @@ import ProfileInfo from './ProfileInfo'; import Feed from '../../components/Feed/Feed'; import { get } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; -import { useNavigate } from '../../hooks/useNavigate'; const ProfilePage: React.FC = () => { const [userInfo, setUserInfo] = useState(); const [polls, setPolls] = useState([]); const [totalVotes, setTotalVotes] = useState(0); - const { page, navigate } = useNavigate(); - const { user } = useAuth(); const [isInfoLoading, setIsInfoLoading] = useState(false); const [isPollsLoading, setIsPollsLoading] = useState(false); + const history = useHistory(); + const { username } = useParams(); + const { user } = useAuth(); useEffect(() => { - const id = page?.id || user?._id; setIsInfoLoading(true); - setIsPollsLoading(true); - if (id) { - get(`/users/${id}`).then(response => { - setUserInfo(response.data); + + const redirect = () => { + if (user) history.push(`/profile/${user.username}`); + else history.push('/login'); + }; + + if (username) { + get(`/users?username=${username}`).then(response => { + if (!response.data.length) return redirect(); // TODO: handle this case + setUserInfo(response.data[0]); setIsInfoLoading(false); - }); - get(`/profiles/${id}`).then(response => { + }).catch(() => redirect()); + } else redirect() + + }, [username, user, history]); + + + useEffect(() => { + if (userInfo?._id) { + setIsPollsLoading(true); + + get(`/profiles/${userInfo._id}`).then(response => { setIsPollsLoading(false); setPolls([]); setPolls(response.data); @@ -38,8 +53,8 @@ const ProfilePage: React.FC = () => { }, 0 )); }); - } else navigate('auth'); - }, [navigate, page, user]); + } + }, [userInfo]) return ( -- cgit v1.2.3 From 0fe1f4138f870c4d00a02741bacf25165c94541e Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 8 Aug 2020 09:07:32 +0300 Subject: feat: support routing for UserStrip --- src/pages/ProfilePage/ProfilePage.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/pages/ProfilePage/ProfilePage.tsx') diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index b81c70f..ec917fd 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -7,6 +7,7 @@ import ProfileInfo from './ProfileInfo'; import Feed from '../../components/Feed/Feed'; import { get } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; +import urls from '../urls'; const ProfilePage: React.FC = () => { @@ -23,8 +24,8 @@ const ProfilePage: React.FC = () => { setIsInfoLoading(true); const redirect = () => { - if (user) history.push(`/profile/${user.username}`); - else history.push('/login'); + if (user) history.push(urls.profile(user.username)); + else history.push(urls.login); }; if (username) { -- cgit v1.2.3 From 10e146ef0215d41527f0466b0e139a6805b96540 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 8 Aug 2020 09:33:45 +0300 Subject: refactor: remove urls file --- src/pages/ProfilePage/ProfilePage.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/pages/ProfilePage/ProfilePage.tsx') diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index ec917fd..b81c70f 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -7,7 +7,6 @@ import ProfileInfo from './ProfileInfo'; import Feed from '../../components/Feed/Feed'; import { get } from '../../requests'; import { useAuth } from '../../hooks/useAuth'; -import urls from '../urls'; const ProfilePage: React.FC = () => { @@ -24,8 +23,8 @@ const ProfilePage: React.FC = () => { setIsInfoLoading(true); const redirect = () => { - if (user) history.push(urls.profile(user.username)); - else history.push(urls.login); + if (user) history.push(`/profile/${user.username}`); + else history.push('/login'); }; if (username) { -- cgit v1.2.3 From b446a83ff24ed5ea2233c544446557ee29f44364 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sat, 8 Aug 2020 09:49:38 +0300 Subject: refactor: use native Route component --- src/pages/ProfilePage/ProfilePage.tsx | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/pages/ProfilePage/ProfilePage.tsx') diff --git a/src/pages/ProfilePage/ProfilePage.tsx b/src/pages/ProfilePage/ProfilePage.tsx index b81c70f..ae94b9f 100644 --- a/src/pages/ProfilePage/ProfilePage.tsx +++ b/src/pages/ProfilePage/ProfilePage.tsx @@ -29,12 +29,11 @@ const ProfilePage: React.FC = () => { if (username) { get(`/users?username=${username}`).then(response => { - if (!response.data.length) return redirect(); // TODO: handle this case + if (!response.data.length) redirect(); // TODO: handle this case setUserInfo(response.data[0]); setIsInfoLoading(false); }).catch(() => redirect()); - } else redirect() - + } else redirect(); }, [username, user, history]); @@ -54,7 +53,7 @@ const ProfilePage: React.FC = () => { )); }); } - }, [userInfo]) + }, [userInfo]); return ( -- cgit v1.2.3