From b31ed66aafbe1d5dbe70d0cdfd70864204510d81 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Tue, 30 Jun 2020 01:27:39 +0300
Subject: fix: re-implement navigation logic

---
 src/pages/ProfilePage/ProfilePage.tsx | 36 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 19 deletions(-)

(limited to 'src/pages/ProfilePage/ProfilePage.tsx')

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 (
     <>
-- 
cgit v1.2.3

