-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: layout and add access right management
- Loading branch information
Showing
64 changed files
with
1,106 additions
and
832 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { FC, PropsWithChildren, memo, useMemo } from "react"; | ||
|
||
import api from "../../entrepot/api"; | ||
import RQKeys from "../../modules/entrepot/RQKeys"; | ||
import { CartesApiException } from "../../modules/jsonFetch"; | ||
import { DatastoreLayoutProps } from "./DatastoreLayout"; | ||
import { useAuthStore } from "../../stores/AuthStore"; | ||
import { CommunityDetailResponseDto, CommunityMemberDtoRightsEnum } from "../../@types/entrepot"; | ||
import Forbidden from "../../pages/error/Forbidden"; | ||
import { CommunityProvider } from "../../contexts/community"; | ||
import { Datastore } from "../../@types/app"; | ||
import { datastoreNavItems } from "../../config/navItems/datastoreNavItems"; | ||
import AppLayout from "./AppLayout"; | ||
import PageNotFoundWithLayout from "../../pages/error/PageNotFoundWithLayout"; | ||
import Main from "./Main"; | ||
import LoadingText from "../Utils/LoadingText"; | ||
import { DatastoreProvider } from "../../contexts/datastore"; | ||
|
||
export interface CommunityLayoutProps extends Omit<DatastoreLayoutProps, "datastoreId"> { | ||
accessRight?: CommunityMemberDtoRightsEnum; | ||
communityId: string; | ||
} | ||
|
||
const CommunityLayout: FC<PropsWithChildren<CommunityLayoutProps>> = (props) => { | ||
const { accessRight, children, communityId, ...rest } = props; | ||
|
||
const { user } = useAuthStore(); | ||
const { data, error, failureReason, isFetching, isLoading, status } = useQuery<[CommunityDetailResponseDto, Datastore | undefined], CartesApiException>({ | ||
queryKey: RQKeys.community(communityId), | ||
queryFn: async ({ signal }) => { | ||
const community = await api.community.get(communityId, { signal }); | ||
let datastore: Datastore | undefined; | ||
if (community.datastore?._id) { | ||
datastore = await api.datastore.get(community.datastore?._id, { signal }); | ||
} | ||
return [community, datastore]; | ||
}, | ||
staleTime: 20000, | ||
enabled: !!communityId, | ||
}); | ||
|
||
const [community, datastore] = data ?? []; | ||
const navItems = useMemo(() => datastoreNavItems(datastore), [datastore]); | ||
|
||
const isAuthorized = useMemo(() => { | ||
const communityMember = user?.communities_member.find((member) => member.community?._id === communityId); | ||
if (!communityMember) { | ||
return false; // is not part of the community | ||
} | ||
const { community, rights } = communityMember; | ||
const isSupervisor = community?.supervisor === user?.id; | ||
return isSupervisor || !accessRight || rights?.includes(accessRight); | ||
}, [accessRight, user?.communities_member, communityId, user?.id]); | ||
|
||
if (isLoading) { | ||
return ( | ||
<AppLayout {...rest}> | ||
<Main> | ||
<LoadingText withSpinnerIcon /> | ||
</Main> | ||
</AppLayout> | ||
); | ||
} | ||
|
||
if (error?.code === 404 || failureReason?.code === 404 || !community) { | ||
return <PageNotFoundWithLayout />; | ||
} | ||
|
||
return ( | ||
<AppLayout {...rest} navItems={navItems}> | ||
<CommunityProvider community={community}> | ||
<DatastoreProvider datastore={datastore} isFetching={isFetching} status={status}> | ||
{isAuthorized ? children : <Forbidden />} | ||
</DatastoreProvider> | ||
</CommunityProvider> | ||
</AppLayout> | ||
); | ||
}; | ||
|
||
export default memo(CommunityLayout); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { fr } from "@codegouvfr/react-dsfr"; | ||
import { Breadcrumb, BreadcrumbProps } from "@codegouvfr/react-dsfr/Breadcrumb"; | ||
import { Notice } from "@codegouvfr/react-dsfr/Notice"; | ||
import { PropsWithChildren, ReactNode, memo, useContext, useMemo } from "react"; | ||
|
||
import getBreadcrumb from "../../modules/entrepot/breadcrumbs/Breadcrumb"; | ||
import { useRoute } from "../../router/router"; | ||
import SessionExpiredAlert from "../Utils/SessionExpiredAlert"; | ||
import useDocumentTitle from "../../hooks/useDocumentTitle"; | ||
import { datastoreContext } from "../../contexts/datastore"; | ||
|
||
export interface MainProps { | ||
customBreadcrumbProps?: BreadcrumbProps; | ||
infoBannerMsg?: ReactNode; | ||
title?: string; | ||
} | ||
|
||
function Main(props: PropsWithChildren<MainProps>) { | ||
const { children, customBreadcrumbProps, infoBannerMsg, title } = props; | ||
const route = useRoute(); | ||
useDocumentTitle(title); | ||
const datastoreValue = useContext(datastoreContext); | ||
|
||
const breadcrumbProps = useMemo(() => { | ||
if (customBreadcrumbProps !== undefined) { | ||
return customBreadcrumbProps; | ||
} | ||
|
||
return getBreadcrumb(route, datastoreValue.datastore); | ||
}, [route, datastoreValue.datastore, customBreadcrumbProps]); | ||
|
||
return ( | ||
<main id="main" role="main"> | ||
{/* doit être le premier élément atteignable après le lien d'évitement (Accessibilité) : https://www.systeme-de-design.gouv.fr/elements-d-interface/composants/bandeau-d-information-importante */} | ||
{infoBannerMsg && <Notice title={infoBannerMsg} isClosable={true} />} | ||
|
||
<div className={fr.cx("fr-container", "fr-my-2w")}> | ||
{breadcrumbProps && <Breadcrumb {...breadcrumbProps} />} | ||
|
||
<div className={fr.cx("fr-mb-4v")}> | ||
<SessionExpiredAlert /> | ||
</div> | ||
|
||
{children} | ||
</div> | ||
</main> | ||
); | ||
} | ||
|
||
export default memo(Main); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { createContext, ReactNode, useContext } from "react"; | ||
import { CommunityDetailResponseDto } from "../@types/entrepot"; | ||
|
||
export const communityContext = createContext<CommunityDetailResponseDto | null>(null); | ||
|
||
export function useCommunity() { | ||
const community = useContext(communityContext); | ||
if (!community) { | ||
throw new Error("useCommunity must be used within a CommunityProvider"); | ||
} | ||
return community; | ||
} | ||
|
||
export function CommunityProvider({ children, community }: { children: ReactNode; community: CommunityDetailResponseDto }) { | ||
return <communityContext.Provider value={community}>{children}</communityContext.Provider>; | ||
} |
Oops, something went wrong.