Skip to content

Commit

Permalink
feat: Fix provider order using react router 9
Browse files Browse the repository at this point in the history
Due to changes in react router 9, some of the providers needed to be moved to the root layout.
  • Loading branch information
LuukvH committed Jun 20, 2024
1 parent 08e5a96 commit 6026e4b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 39 deletions.
10 changes: 3 additions & 7 deletions src/web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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: <MainNavbar />,
element: <MainLayout />,
errorElement: <ErrorPage />,
children: [
{
Expand All @@ -30,11 +30,7 @@ const router = createBrowserRouter([
]);

function App() {
return (
<React.Fragment>
<RouterProvider router={router} />
</React.Fragment>
);
return <RouterProvider router={router} />;
}

export default App;
33 changes: 33 additions & 0 deletions src/web/src/components/AuthProviderWithNavigate.tsx
Original file line number Diff line number Diff line change
@@ -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<AuthProviderWithNavigateProps> = ({ 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 (
<Auth0Provider
domain={domain}
clientId={clientId}
authorizationParams={{
audience: "https://api.kdvmanager.nl/",
redirect_uri: window.location.origin,
}}
onRedirectCallback={onRedirectCallback}
>
{children}
</Auth0Provider>
);
};

export default AuthProviderWithNavigate;
20 changes: 20 additions & 0 deletions src/web/src/components/MainLayout.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<AuthProviderWithNavigate>
<QueryClientProvider client={queryClient}>
<MainNavbar>
<Outlet />
</MainNavbar>
</QueryClientProvider>
</AuthProviderWithNavigate>
);
}

export default AuthProviderLayout;
10 changes: 6 additions & 4 deletions src/web/src/components/MainNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<MainNavbarProps> = ({ children }) => {
const navigate = useNavigate();
const { loginWithRedirect, logout } = useAuth0();
const [anchorElNav, setAnchorElNav] = React.useState<null | HTMLElement>(null);
Expand Down Expand Up @@ -190,10 +192,10 @@ function MainNavbar() {
</Toolbar>
</Container>
</AppBar>
<Outlet />
{children}
</>
);
}
};
export default withAuthenticationRequired(MainNavbar, {
onRedirecting: () => (
<Box
Expand Down
37 changes: 9 additions & 28 deletions src/web/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import React from "react";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { Auth0Provider } from "@auth0/auth0-react";
import { createRoot } from "react-dom/client";
import { SnackbarProvider } from "notistack";
import NiceModal from "@ebay/nice-modal-react";
Expand All @@ -13,36 +10,20 @@ import i18n from "./i18n";
import { LocalizationProvider } from "@mui/x-date-pickers/LocalizationProvider/LocalizationProvider";
import { AdapterDayjs } from "@mui/x-date-pickers/AdapterDayjs";

const queryClient = new QueryClient();
const domain = (import.meta.env.VITE_APP_AUTH0_DOMAIN as string) || "";
const clientId = (import.meta.env.VITE_APP_AUTH0_CLIENT_ID as string) || "";

const container = document.getElementById("root") as Element;
const root = createRoot(container);

root.render(
<React.StrictMode>
<Auth0Provider
domain={domain}
clientId={clientId}
authorizationParams={{
audience: "https://api.kdvmanager.nl/",
redirect_uri: window.location.origin,
}}
>
<QueryClientProvider client={queryClient}>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<I18nextProvider i18n={i18n}>
<SnackbarProvider>
<NiceModal.Provider>
<App />
</NiceModal.Provider>
</SnackbarProvider>
</I18nextProvider>
</LocalizationProvider>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
</Auth0Provider>
<LocalizationProvider dateAdapter={AdapterDayjs}>
<I18nextProvider i18n={i18n}>
<SnackbarProvider>
<NiceModal.Provider>
<App />
</NiceModal.Provider>
</SnackbarProvider>
</I18nextProvider>
</LocalizationProvider>
</React.StrictMode>,
);

Expand Down

0 comments on commit 6026e4b

Please sign in to comment.