Skip to content

Commit

Permalink
Use useLocation instead of window.location
Browse files Browse the repository at this point in the history
Should fix re-rendering of `NavBar` when navigating to other pages.
  • Loading branch information
praseodym committed Feb 3, 2025
1 parent 96d2e95 commit 3a87701
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions frontend/app/component/navbar/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { IconUser } from "@kiesraad/icon";
import styles from "./NavBar.module.css";
import { NavBarLinks } from "./NavBarLinks";

type NavBarProps = { location?: { pathname: string; hash: string } };
type NavBarProps = { location: { pathname: string; hash: string } };

export function NavBar({ location = window.location }: NavBarProps) {
export function NavBar({ location }: NavBarProps) {
const isAdministrator = location.hash.includes("administrator");
const isCoordinator = location.hash.includes("coordinator");

Expand Down
5 changes: 3 additions & 2 deletions frontend/app/module/AdministratorLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Outlet } from "react-router";
import { Outlet, useLocation } from "react-router";

import { Footer } from "app/component/footer/Footer";

Expand All @@ -7,9 +7,10 @@ import { AppLayout } from "@kiesraad/ui";
import { NavBar } from "../component/navbar/NavBar";

export function AdministratorLayout() {
const location = useLocation();
return (
<AppLayout>
<NavBar />
<NavBar location={location} />
<Outlet />
<Footer />
</AppLayout>
Expand Down
6 changes: 4 additions & 2 deletions frontend/app/module/account/LoginLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Outlet } from "react-router";
import { Outlet, useLocation } from "react-router";

import { NavBar } from "app/component/navbar/NavBar";

import { AppLayout } from "@kiesraad/ui";

export function LoginLayout() {
const location = useLocation();

return (
<AppLayout>
<NavBar />
<NavBar location={location} />
<Outlet />
</AppLayout>
);
Expand Down
5 changes: 3 additions & 2 deletions frontend/app/module/election/ElectionLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Outlet } from "react-router";
import { Outlet, useLocation } from "react-router";

import { ElectionProvider, ElectionStatusProvider } from "@kiesraad/api";
import { useNumericParam } from "@kiesraad/util";
Expand All @@ -7,11 +7,12 @@ import { NavBar } from "../../component/navbar/NavBar";

export function ElectionLayout() {
const electionId = useNumericParam("electionId");
const location = useLocation();

return (
<ElectionProvider electionId={electionId}>
<ElectionStatusProvider electionId={electionId}>
<NavBar />
<NavBar location={location} />
<Outlet />
</ElectionStatusProvider>
</ElectionProvider>
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/module/election/page/OverviewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function OverviewPage() {
return (
<>
<PageTitle title={`${t("election.title.overview")} - Abacus`} />
<NavBar />
<NavBar location={location} />
<header>
<section>
<h1>{isAdminOrCoordinator ? t("election.manage") : t("election.title.plural")}</h1>
Expand Down

0 comments on commit 3a87701

Please sign in to comment.