From f97989967ee0b88a8c64f226a4b28a79eeef5fd2 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Mon, 10 Aug 2020 11:19:53 +0300
Subject: refactor: remove "Page" from container names

---
 src/containers/Profile/Profile.tsx | 56 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 src/containers/Profile/Profile.tsx

(limited to 'src/containers/Profile/Profile.tsx')

diff --git a/src/containers/Profile/Profile.tsx b/src/containers/Profile/Profile.tsx
new file mode 100644
index 0000000..af52e5e
--- /dev/null
+++ b/src/containers/Profile/Profile.tsx
@@ -0,0 +1,56 @@
+import React, { useEffect, useCallback } from 'react';
+import { useHistory, useParams } from 'react-router-dom';
+import { Poll } from 'which-types';
+import { Container } from '@material-ui/core';
+
+import ProfileInfo from './ProfileInfo';
+import PollsList from '../../components/PollsList/PollsList';
+import Loading from '../../components/Loading/Loading';
+import { useAuth } from '../../hooks/useAuth';
+import { useUser, useProfile } from '../../hooks/APIClient';
+
+
+const Profile: React.FC = () => {
+  const history = useHistory();
+  const { username } = useParams();
+  const { user } = useAuth();
+
+  const { data: userInfo, mutate: setUserInfo } = useUser(username);
+  const { data: polls, mutate: mutatePolls, isValidating } = useProfile(userInfo?._id);
+
+  useEffect(() => {
+    if (!username) {
+      if (user) history.push(`/profile/${user.username}`);
+      else history.push('/login');
+    }
+  }, [username, history, user]);
+
+
+  const totalVotes = useCallback(
+    polls.reduce(
+      (total: number, current: Poll) => {
+        const { left, right } = current.contents;
+        return total + left.votes + right.votes;
+      }, 0
+    ),
+    [polls]
+  );
+
+  return (
+    <Container maxWidth="sm" disableGutters>
+      <ProfileInfo
+        userInfo={userInfo}
+        setUserInfo={setUserInfo}
+        savedPolls={polls.length}
+        totalVotes={totalVotes}
+      />
+      {
+        isValidating && !polls
+        ? <Loading />
+        : <PollsList polls={polls} mutate={mutatePolls} />
+      }
+    </Container>
+  );
+};
+
+export default Profile;
-- 
cgit v1.2.3


From 35fcdceb8f04fe333d45c5b1cb7ba395352c92d6 Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Mon, 10 Aug 2020 13:22:00 +0300
Subject: feat: only allow 1 snackbar on mobile

---
 src/containers/Profile/Profile.tsx | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

(limited to 'src/containers/Profile/Profile.tsx')

diff --git a/src/containers/Profile/Profile.tsx b/src/containers/Profile/Profile.tsx
index af52e5e..f7678de 100644
--- a/src/containers/Profile/Profile.tsx
+++ b/src/containers/Profile/Profile.tsx
@@ -16,7 +16,7 @@ const Profile: React.FC = () => {
   const { user } = useAuth();
 
   const { data: userInfo, mutate: setUserInfo } = useUser(username);
-  const { data: polls, mutate: mutatePolls, isValidating } = useProfile(userInfo?._id);
+  const { data: polls, mutate: mutatePolls, isValidating } = useProfile(username);
 
   useEffect(() => {
     if (!username) {
-- 
cgit v1.2.3


From 78218c0f3427ad79de003ac59cffb99b08f0ae7d Mon Sep 17 00:00:00 2001
From: eug-vs <eug-vs@keemail.me>
Date: Mon, 10 Aug 2020 13:47:02 +0300
Subject: fix: resolve eslint errors

---
 src/containers/Profile/Profile.tsx | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

(limited to 'src/containers/Profile/Profile.tsx')

diff --git a/src/containers/Profile/Profile.tsx b/src/containers/Profile/Profile.tsx
index f7678de..7e929fb 100644
--- a/src/containers/Profile/Profile.tsx
+++ b/src/containers/Profile/Profile.tsx
@@ -1,4 +1,4 @@
-import React, { useEffect, useCallback } from 'react';
+import React, { useEffect, useMemo } from 'react';
 import { useHistory, useParams } from 'react-router-dom';
 import { Poll } from 'which-types';
 import { Container } from '@material-ui/core';
@@ -26,15 +26,12 @@ const Profile: React.FC = () => {
   }, [username, history, user]);
 
 
-  const totalVotes = useCallback(
-    polls.reduce(
-      (total: number, current: Poll) => {
-        const { left, right } = current.contents;
-        return total + left.votes + right.votes;
-      }, 0
-    ),
-    [polls]
-  );
+  const totalVotes = useMemo(() => polls.reduce(
+    (total: number, current: Poll) => {
+      const { left, right } = current.contents;
+      return total + left.votes + right.votes;
+    }, 0
+  ), [polls]);
 
   return (
     <Container maxWidth="sm" disableGutters>
@@ -46,8 +43,8 @@ const Profile: React.FC = () => {
       />
       {
         isValidating && !polls
-        ? <Loading />
-        : <PollsList polls={polls} mutate={mutatePolls} />
+          ? <Loading />
+          : <PollsList polls={polls} mutate={mutatePolls} />
       }
     </Container>
   );
-- 
cgit v1.2.3

