From 6026e4b7d21db0e7562a1b62323c75e31d0bdb1c Mon Sep 17 00:00:00 2001 From: LuukvH Date: Thu, 20 Jun 2024 21:28:13 +0200 Subject: [PATCH] feat: Fix provider order using react router 9 Due to changes in react router 9, some of the providers needed to be moved to the root layout. --- src/web/src/App.tsx | 10 ++--- .../components/AuthProviderWithNavigate.tsx | 33 +++++++++++++++++ src/web/src/components/MainLayout.tsx | 20 ++++++++++ src/web/src/components/MainNavbar.tsx | 10 +++-- src/web/src/index.tsx | 37 +++++-------------- 5 files changed, 71 insertions(+), 39 deletions(-) create mode 100644 src/web/src/components/AuthProviderWithNavigate.tsx create mode 100644 src/web/src/components/MainLayout.tsx diff --git a/src/web/src/App.tsx b/src/web/src/App.tsx index 79d31d8e..af1819b0 100644 --- a/src/web/src/App.tsx +++ b/src/web/src/App.tsx @@ -1,12 +1,12 @@ import React from "react"; import { RouterProvider, createBrowserRouter } from "react-router-dom"; import ErrorPage from "./components/ErrorPage"; -import MainNavbar from "./components/MainNavbar"; +import MainLayout from "./components/MainLayout"; const router = createBrowserRouter([ { path: "/", - element: , + element: , errorElement: , children: [ { @@ -30,11 +30,7 @@ const router = createBrowserRouter([ ]); function App() { - return ( - - - - ); + return ; } export default App; diff --git a/src/web/src/components/AuthProviderWithNavigate.tsx b/src/web/src/components/AuthProviderWithNavigate.tsx new file mode 100644 index 00000000..ceb85783 --- /dev/null +++ b/src/web/src/components/AuthProviderWithNavigate.tsx @@ -0,0 +1,33 @@ +import { Auth0Provider, type AppState } from "@auth0/auth0-react"; +import { useNavigate } from "react-router-dom"; + +type AuthProviderWithNavigateProps = { + children: React.ReactNode; +}; + +const AuthProviderWithNavigate: React.FC = ({ children }) => { + const navigate = useNavigate(); + + const domain = (import.meta.env.VITE_APP_AUTH0_DOMAIN as string) || ""; + const clientId = (import.meta.env.VITE_APP_AUTH0_CLIENT_ID as string) || ""; + + const onRedirectCallback = (appState?: AppState) => { + navigate(appState?.returnTo || window.location.pathname); + }; + + return ( + + {children} + + ); +}; + +export default AuthProviderWithNavigate; diff --git a/src/web/src/components/MainLayout.tsx b/src/web/src/components/MainLayout.tsx new file mode 100644 index 00000000..4b081e17 --- /dev/null +++ b/src/web/src/components/MainLayout.tsx @@ -0,0 +1,20 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import AuthProviderWithNavigate from "./AuthProviderWithNavigate"; +import MainNavbar from "./MainNavbar"; +import { Outlet } from "react-router-dom"; + +const queryClient = new QueryClient(); + +function AuthProviderLayout() { + return ( + + + + + + + + ); +} + +export default AuthProviderLayout; diff --git a/src/web/src/components/MainNavbar.tsx b/src/web/src/components/MainNavbar.tsx index 4cfc520b..64c109ed 100644 --- a/src/web/src/components/MainNavbar.tsx +++ b/src/web/src/components/MainNavbar.tsx @@ -13,12 +13,14 @@ import MenuItem from "@mui/material/MenuItem"; import AdbIcon from "@mui/icons-material/Adb"; import { useNavigate } from "react-router-dom"; import { useAuth0, withAuthenticationRequired } from "@auth0/auth0-react"; -import { Outlet } from "react-router-dom"; import { AppBar } from "@mui/material"; const settings = ["Profile", "Account", "Dashboard", "Logout"]; +type MainNavbarProps = { + children: React.ReactNode; +}; -function MainNavbar() { +const MainNavbar: React.FC = ({ children }) => { const navigate = useNavigate(); const { loginWithRedirect, logout } = useAuth0(); const [anchorElNav, setAnchorElNav] = React.useState(null); @@ -190,10 +192,10 @@ function MainNavbar() { - + {children} ); -} +}; export default withAuthenticationRequired(MainNavbar, { onRedirecting: () => ( - - - - - - - - - - - - - - + + + + + + + + + , );