Skip to content

Commit

Permalink
feat: App title in the header and current app selected in the Drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
Loxeris committed May 13, 2024
1 parent b91daa5 commit 7733bfb
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 14 deletions.
10 changes: 2 additions & 8 deletions src/components/applications/JobMonitor.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
"use client";
import * as React from "react";
import CssBaseline from "@mui/material/CssBaseline";
import { Box } from "@mui/material";
import { ThemeProvider as MUIThemeProvider } from "@mui/material/styles";
import { JobDataTable } from "../ui/JobDataTable";
import Dashboard from "../layout/Dashboard";
import { useMUITheme } from "@/hooks/theme";
import ApplicationHeader from "@/components/ui/ApplicationHeader";

/**
* Build the Job Monitor application
* @returns Job Monitor content
*/
export default function JobMonitor() {
const theme = useMUITheme();

return (
<div>
<h2>Job Monitor</h2>
<ApplicationHeader type="Job Monitor" />
<JobDataTable />
</div>
);
Expand Down
7 changes: 2 additions & 5 deletions src/components/applications/UserDashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
"use client";
import * as React from "react";
import CssBaseline from "@mui/material/CssBaseline";
import { Box } from "@mui/material";
import { ThemeProvider as MUIThemeProvider } from "@mui/material/styles";
import { useOidcAccessToken } from "@axa-fr/react-oidc";
import Dashboard from "../layout/Dashboard";
import { useOIDCContext } from "@/hooks/oidcConfiguration";
import { useMUITheme } from "@/hooks/theme";
import ApplicationHeader from "@/components/ui/ApplicationHeader";

/**
* Build the User Dashboard page
Expand All @@ -22,6 +18,7 @@ export default function UserDashboard() {

return (
<div>
<ApplicationHeader type="Dashboard" />
<h2>Hello {accessTokenPayload["preferred_username"]}</h2>

<p>To start with, select an application in the side bar</p>
Expand Down
31 changes: 31 additions & 0 deletions src/components/ui/ApplicationHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from "react";
import { Stack, Typography } from "@mui/material";
import { useApplicationTitle } from "@/hooks/application";

/**
* Application Header component with the application title and type
* @param type the type of the application
* @returns the application header
*/
export default function ApplicationHeader({ type }: { type: string }) {
const appTitle = useApplicationTitle();
return (
<header>
<Stack spacing={2} direction={"row"} alignItems={"end"}>
{appTitle && (
<Typography
color="text.primary"
variant={"h4"}
fontWeight={"bold"}
width={"fit-content"}
>
{appTitle}
</Typography>
)}
<Typography color="text.secondary" variant={"h4"} width={"fit-content"}>
{type}
</Typography>
</Stack>
</header>
);
}
4 changes: 4 additions & 0 deletions src/components/ui/DrawerItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ThemeProvider as MUIThemeProvider } from "@mui/material/styles";
import { ThemeProvider } from "@/contexts/ThemeProvider";
import { useMUITheme } from "@/hooks/theme";
import { useSearchParamsUtils } from "@/hooks/searchParamsUtils";
import { useApplicationId } from "@/hooks/application";

/**
* Represents a drawer item component.
Expand Down Expand Up @@ -50,6 +51,8 @@ export default function DrawerItem({
// Represents the closest edge to the mouse cursor
const [closestEdge, setClosestEdge]: any = useState<Edge | null>(null);

const appId = useApplicationId();

useEffect(() => {
if (!dragRef.current || !handleRef.current) return;
const element = dragRef.current;
Expand Down Expand Up @@ -148,6 +151,7 @@ export default function DrawerItem({
onClick={() => setParam("appId", id)}
sx={{ pl: 2, borderRadius: 2, pr: 1 }}
ref={dragRef}
selected={appId === id}
>
<ListItemIcon>
<Icon component={icon} />
Expand Down
37 changes: 37 additions & 0 deletions src/hooks/application.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useContext, useMemo } from "react";
import { useSearchParamsUtils } from "@/hooks/searchParamsUtils";
import { ApplicationsContext } from "@/contexts/ApplicationsProvider";

/**
* Custom hook to access the application id from the URL
* @returns the application id
*/
export function useApplicationId() {
const { getParam } = useSearchParamsUtils();

return useMemo(() => {
return getParam("appId");
}, [getParam]);
}

/**
* Custom hook to access the application title based on the application id
* @returns the application title
*/
export function useApplicationTitle() {
const [sections] = useContext(ApplicationsContext);
const appId = useApplicationId();

return useMemo(() => {
if (!sections || !appId) return null;

const app = sections.reduce(
(acc, section) => {
if (acc) return acc;
return section.items.find((app) => app.id === appId);
},
undefined as { title: string } | undefined,
);
return app?.title;
}, [sections, appId]);
}
12 changes: 12 additions & 0 deletions src/hooks/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ export const useMUITheme = () => {
},
},
},
MuiListItemButton: {
styleOverrides: {
root: {
"&.Mui-selected, &.Mui-selected:hover": {
backgroundColor:
muiTheme.palette.mode === "light"
? lighten(grey[200], 0.2)
: darken(grey[800], 0.2),
},
},
},
},
};

return muiTheme;
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/jobMonitor.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("Job Monitor", () => {
});

it("should render the drawer", () => {
cy.get("h2").contains("Job Monitor").should("be.visible");
cy.get("header").contains("Job Monitor").should("be.visible");
});

it("should handle filter addition", () => {
Expand Down
19 changes: 19 additions & 0 deletions test/unit-tests/UserDashboard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ jest.mock("@axa-fr/react-oidc", () => ({
useOidcAccessToken: jest.fn(),
}));

jest.mock("jsoncrush", () => ({
crush: jest.fn().mockImplementation((data) => `crushed-${data}`),
uncrush: jest.fn().mockImplementation((data) => data.replace("crushed-", "")),
}));

const params = new URLSearchParams();

jest.mock("next/navigation", () => {
return {
usePathname: () => ({
pathname: "",
}),
useRouter: () => ({
push: jest.fn(),
}),
useSearchParams: () => params,
};
});

describe("<UserDashboard />", () => {
it("renders not authenticated message when accessTokenPayload is not defined", () => {
(useOidc as jest.Mock).mockReturnValue({ isAuthenticated: false });
Expand Down

0 comments on commit 7733bfb

Please sign in to comment.