Skip to content

Commit

Permalink
refactor: my account menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Jun 3, 2024
1 parent 2288fee commit 4c33f0f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 80 deletions.
80 changes: 80 additions & 0 deletions packages/frontend/src/features/MyAccountMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Popover } from "#components/Popover";
import { css } from "#styled-system/css";
import { Center, Divider, Stack, styled } from "#styled-system/jsx";
import Button from "@codegouvfr/react-dsfr/Button";
import { useRef } from "react";
import { useLogout } from "../contexts/AuthContext";
import { electric } from "../db";

export const MyAccountMenu = () => {
const ref = useRef<HTMLAnchorElement>(null);

return (
<>
<styled.div hideBelow="lg">
<Popover.Root>
<Popover.Trigger asChild>
<Button ref={ref as any} priority="tertiary" iconId="fr-icon-account-circle-fill">
Mon compte
</Button>
</Popover.Trigger>
<Popover.Positioner>
<Popover.Content borderRadius="0">
<MyAccountMenuActions />
</Popover.Content>
</Popover.Positioner>
</Popover.Root>
</styled.div>
<styled.div hideFrom="lg">
<MyAccountMenuActions />
</styled.div>
</>
);
};
export const actionsContainerClassName = css({
gap: "0",
px: "16px",
"& > button": {
h: "48px",
m: 0,
color: "black",
fontSize: "14px",
"&:disabled": {
color: "text-disabled-grey",
},
},
});
const MyAccountMenuActions = () => {
const logout = useLogout();
const deleteLocalData = () => {
if (electric.isConnected) electric.disconnect();
localStorage.clear();
indexedDB.deleteDatabase("crvif.db");
window.location.reload();
};

const actions = [
{ text: "Partage des CR", onClick: () => {}, disabled: true },
{ text: "Clauses départementales", onClick: () => {}, disabled: true },
{ text: "Clauses nationales", onClick: () => {}, disabled: true },
{ text: "Se déconnecter", onClick: logout },
{ text: "Supprimer les données locales", onClick: deleteLocalData },
];

return (
<Stack className={actionsContainerClassName}>
{actions.map(({ text, onClick, disabled }, index) => (
<>
<Button disabled={disabled} onClick={onClick}>
{text}
</Button>
{index < actions.length - 1 && (
<Center>
<Divider w="85%" />
</Center>
)}
</>
))}
</Stack>
);
};
85 changes: 5 additions & 80 deletions packages/frontend/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Popover } from "#components/Popover";
import { ReportSearch } from "#components/ReportSearch.js";
import { css } from "#styled-system/css";
import { Box, Center, Divider, Flex, Stack, styled } from "#styled-system/jsx";
import { Box, Flex, Stack } from "#styled-system/jsx";
import Badge from "@codegouvfr/react-dsfr/Badge";
import Button from "@codegouvfr/react-dsfr/Button";
import Footer from "@codegouvfr/react-dsfr/Footer";
import Header from "@codegouvfr/react-dsfr/Header/Header";
import MuiDsfrThemeProvider from "@codegouvfr/react-dsfr/mui";
import { createRootRouteWithContext, Outlet, useRouter } from "@tanstack/react-router";
import { useRef, type PropsWithChildren } from "react";
import { type PropsWithChildren } from "react";
import type { RouterOutputs } from "../api";
import { useIsLoggedIn, useLogout } from "../contexts/AuthContext";
import { electric } from "../db";
import { useIsLoggedIn } from "../contexts/AuthContext";
import { useIsDesktop } from "../hooks/useIsDesktop";
import { MyAccountMenu, actionsContainerClassName } from "../features/MyAccountMenu";

export const Route = createRootRouteWithContext<Partial<RouterOutputs<"/api/login">>>()({
beforeLoad: (ctx) => {
Expand Down Expand Up @@ -79,7 +78,7 @@ const Layout = ({ children }: PropsWithChildren) => {
homeLinkProps={{ title: "Compte rendu vif", to: "/" }}
quickAccessItems={[
...(isLoggedIn
? [<LoggedInQuickAccessItems />]
? [<MyAccountMenu />]
: [
<Stack className={actionsContainerClassName}>
<Button linkProps={{ to: "/login" }}>Se connecter</Button>
Expand Down Expand Up @@ -110,77 +109,3 @@ const Layout = ({ children }: PropsWithChildren) => {
</Flex>
);
};

export const LoggedInQuickAccessItems = () => {
const ref = useRef<HTMLAnchorElement>(null);

return (
<>
<styled.div hideBelow="lg">
<Popover.Root>
<Popover.Trigger asChild>
<Button ref={ref as any} priority="tertiary" iconId="fr-icon-account-circle-fill">
Mon compte
</Button>
</Popover.Trigger>
<Popover.Positioner>
<Popover.Content borderRadius="0">
<MyAccountDesktop />
</Popover.Content>
</Popover.Positioner>
</Popover.Root>
</styled.div>
<styled.div hideFrom="lg">
<MyAccountDesktop />
</styled.div>
</>
);
};

const actionsContainerClassName = css({
gap: "0",
"& > button": {
h: "48px",
m: 0,
color: "black",
fontSize: "14px",
"&:disabled": {
color: "text-disabled-grey",
},
},
});

const MyAccountDesktop = () => {
const logout = useLogout();
const deleteLocalData = () => {
if (electric.isConnected) electric.disconnect();
localStorage.clear();
indexedDB.deleteDatabase("crvif.db");
window.location.reload();
};

const actions = [
{ text: "Partage des CR", onClick: () => {}, disabled: true },
{ text: "Clauses départementales", onClick: () => {}, disabled: true },
{ text: "Clauses nationales", onClick: () => {}, disabled: true },
{ text: "Se déconnecter", onClick: logout },
{ text: "Supprimer les données locales", onClick: deleteLocalData },
];

return (
<Stack className={actionsContainerClassName}>
{actions.map(({ text, onClick, disabled }, index) => (
<>
<Button disabled={disabled} onClick={onClick}>
{text}
</Button>
{index < actions.length - 1 && (
<Center>
<Divider w="85%" />
</Center>
)}
</>
))}
</Stack>
);
};

0 comments on commit 4c33f0f

Please sign in to comment.