From 46e360a23969bde69f6d80b5ff27401a39169be6 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
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<User>();
   const [polls, setPolls] = useState<Poll[]>([]);
   const [totalVotes, setTotalVotes] = useState<number>(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 (
     <Container maxWidth="sm" disableGutters>
-- 
cgit v1.2.3