From 54fd3d57054feaf13ec2fae532e645ed0bc87349 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Tue, 22 Aug 2023 18:20:04 -0300 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=94=A7=20fix(headerComponent):=20remo?= =?UTF-8?q?ve=20unused=20imports=20and=20function=20call=20to=20improve=20?= =?UTF-8?q?code=20cleanliness=20and=20performance=20=E2=9C=A8=20feat(heade?= =?UTF-8?q?rComponent):=20remove=20conditional=20rendering=20for=20api=20k?= =?UTF-8?q?eys=20button=20to=20always=20display=20it=20in=20the=20header?= =?UTF-8?q?=20=F0=9F=94=A7=20fix(ApiKeysPage):=20add=20missing=20import=20?= =?UTF-8?q?for=20Header=20component=20to=20fix=20compilation=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/headerComponent/index.tsx | 26 +++++++++---------- src/frontend/src/pages/ApiKeysPage/index.tsx | 3 +++ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/frontend/src/components/headerComponent/index.tsx b/src/frontend/src/components/headerComponent/index.tsx index a9c91ebea..33fcaf58e 100644 --- a/src/frontend/src/components/headerComponent/index.tsx +++ b/src/frontend/src/components/headerComponent/index.tsx @@ -1,4 +1,4 @@ -import { useContext, useEffect, useState } from "react"; +import { useContext } from "react"; import { FaDiscord, FaGithub, FaTwitter } from "react-icons/fa"; import { Link, useLocation, useNavigate } from "react-router-dom"; import AlertDropdown from "../../alerts/alertDropDown"; @@ -7,7 +7,6 @@ import { alertContext } from "../../contexts/alertContext"; import { AuthContext } from "../../contexts/authContext"; import { darkContext } from "../../contexts/darkContext"; import { TabsContext } from "../../contexts/tabsContext"; -import { getRepoStars } from "../../controllers/API"; import IconComponent from "../genericIconComponent"; import { Button } from "../ui/button"; import { Separator } from "../ui/separator"; @@ -128,18 +127,17 @@ export default function Header(): JSX.Element { /> - {!autoLogin && ( - - )} + + diff --git a/src/frontend/src/pages/ApiKeysPage/index.tsx b/src/frontend/src/pages/ApiKeysPage/index.tsx index 37e148caa..852d898b1 100644 --- a/src/frontend/src/pages/ApiKeysPage/index.tsx +++ b/src/frontend/src/pages/ApiKeysPage/index.tsx @@ -18,6 +18,7 @@ import SecretKeyModal from "../../modals/SecretKeyModal"; import moment from "moment"; import { ApiKey } from "../../types/components"; +import Header from "../../components/headerComponent"; export default function ApiKeysPage() { const [loadingKeys, setLoadingKeys] = useState(true); @@ -80,6 +81,8 @@ export default function ApiKeysPage() { return ( <> +
+ {userData && (
From ada752309a1e16c43ea260e33f6ebe085abeb23c Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Tue, 22 Aug 2023 21:49:37 -0300 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20fix(App.tsx):=20add=20setIsA?= =?UTF-8?q?dmin=20function=20to=20AuthContext=20to=20update=20isAdmin=20st?= =?UTF-8?q?ate=20when=20user=20data=20is=20fetched=20=E2=9C=A8=20feat(App.?= =?UTF-8?q?tsx):=20set=20isAdmin=20state=20based=20on=20user's=20is=5Fsupe?= =?UTF-8?q?ruser=20value=20to=20conditionally=20render=20admin-related=20c?= =?UTF-8?q?omponents=20=F0=9F=90=9B=20fix(headerComponent/index.tsx):=20fi?= =?UTF-8?q?x=20conditional=20rendering=20of=20Sign=20out,=20Home,=20and=20?= =?UTF-8?q?Admin=20page=20buttons=20based=20on=20autoLogin=20and=20isAdmin?= =?UTF-8?q?=20values=20=F0=9F=90=9B=20fix(authContext.tsx):=20remove=20unn?= =?UTF-8?q?ecessary=20useEffect=20to=20update=20isAdmin=20state=20when=20a?= =?UTF-8?q?ccessToken=20or=20isAdmin=20changes=20=F0=9F=94=A5=20chore(api.?= =?UTF-8?q?tsx):=20remove=20unused=20sleep=20function=20=E2=9C=A8=20feat(A?= =?UTF-8?q?dminPage/index.tsx):=20update=20text=20in=20AdminPage=20to=20pr?= =?UTF-8?q?ovide=20a=20better=20description=20of=20the=20page's=20purpose?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(routes.tsx):=20wrap=20AdminPage=20and=20A?= =?UTF-8?q?piKeysPage=20components=20with=20ProtectedAdminRoute=20componen?= =?UTF-8?q?t=20to=20restrict=20access=20to=20admin-only=20routes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/App.tsx | 4 +- .../src/components/headerComponent/index.tsx | 60 ++++++++++--------- src/frontend/src/contexts/authContext.tsx | 8 --- src/frontend/src/controllers/API/api.tsx | 4 -- src/frontend/src/pages/AdminPage/index.tsx | 2 +- src/frontend/src/routes.tsx | 8 +-- 6 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/frontend/src/App.tsx b/src/frontend/src/App.tsx index 057a16fff..e9c58934f 100644 --- a/src/frontend/src/App.tsx +++ b/src/frontend/src/App.tsx @@ -134,7 +134,7 @@ export default function App() { }; //this function is to get the user logged in when the page is refreshed - const { setUserData, getAuthentication, login, setAutoLogin, logout } = + const { setUserData, getAuthentication, login, setAutoLogin, logout, setIsAdmin } = useContext(AuthContext); useEffect(() => { @@ -154,6 +154,8 @@ export default function App() { .then((user) => { setUserData(user); setLoading(false); + const isSuperUser = user.is_superuser; + setIsAdmin(isSuperUser); }) .catch((error) => {}); } diff --git a/src/frontend/src/components/headerComponent/index.tsx b/src/frontend/src/components/headerComponent/index.tsx index 11c1507a3..25e4dc34f 100644 --- a/src/frontend/src/components/headerComponent/index.tsx +++ b/src/frontend/src/components/headerComponent/index.tsx @@ -26,34 +26,37 @@ export default function Header(): JSX.Element { ⛓️ - {location.pathname === "/admin" && ( - - )} + {autoLogin === false && ( - + + )} + + {location.pathname === "/admin" && ( + { + navigate("/"); + }} + className="text-sm font-medium text-muted-foreground transition-colors hover:text-primary cursor-pointer" + > + Home + )} {isAdmin && !autoLogin && location.pathname !== "/admin" && ( - + )} {flows.findIndex((f) => tabId === f.id) !== -1 && tabId !== "" && ( @@ -138,17 +141,18 @@ export default function Header(): JSX.Element { />
- - + {!autoLogin && ( + + )}
diff --git a/src/frontend/src/contexts/authContext.tsx b/src/frontend/src/contexts/authContext.tsx index 1fcfef58e..4fdca3ad0 100644 --- a/src/frontend/src/contexts/authContext.tsx +++ b/src/frontend/src/contexts/authContext.tsx @@ -47,14 +47,6 @@ export function AuthProvider({ children }): React.ReactElement { fetchStars(); }, []); - useEffect(() => { - if (accessToken) { - getLoggedUser().then((user) => { - const isSuperUser = user.is_superuser; - setIsAdmin(isSuperUser); - }); - } - }, [accessToken, isAdmin]); function getAuthentication() { const storedRefreshToken = cookies.get("refresh_token"); diff --git a/src/frontend/src/controllers/API/api.tsx b/src/frontend/src/controllers/API/api.tsx index 2d05228cc..fc4358ed0 100644 --- a/src/frontend/src/controllers/API/api.tsx +++ b/src/frontend/src/controllers/API/api.tsx @@ -103,9 +103,5 @@ function ApiInterceptor() { return null; } -// Function to sleep for a given duration in milliseconds -function sleep(ms: number) { - return new Promise((resolve) => setTimeout(resolve, ms)); -} export { ApiInterceptor, api }; diff --git a/src/frontend/src/pages/AdminPage/index.tsx b/src/frontend/src/pages/AdminPage/index.tsx index 64b766416..83f79a25e 100644 --- a/src/frontend/src/pages/AdminPage/index.tsx +++ b/src/frontend/src/pages/AdminPage/index.tsx @@ -196,7 +196,7 @@ export default function AdminPage() { Welcome back!

- Here's a list of all users! + Navigate through this section to efficiently oversee all application users. From here, you can seamlessly manage user accounts.

diff --git a/src/frontend/src/routes.tsx b/src/frontend/src/routes.tsx index 04e2f80f4..9b1d35b55 100644 --- a/src/frontend/src/routes.tsx +++ b/src/frontend/src/routes.tsx @@ -88,9 +88,9 @@ const Router = () => { - + } /> @@ -106,9 +106,9 @@ const Router = () => { + - + } >