-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split no OICD case in another App wrapper to fix undefined context
- Loading branch information
1 parent
9fab2ab
commit b194441
Showing
4 changed files
with
92 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { CustomGlobalStyle } from '@components/CustomGlobalStyle' | ||
import { FrontendErrorBoundary } from '@components/FrontendErrorBoundary' | ||
import { GlobalStyle, THEME, ThemeProvider } from '@mtes-mct/monitor-ui' | ||
import { LandingPage } from '@pages/LandingPage' | ||
import { UnsupportedBrowserPage } from '@pages/UnsupportedBrowserPage' | ||
import { isBrowserSupported } from '@utils/isBrowserSupported' | ||
import { useCustomAuthWithoutOicd } from 'auth/hooks/useCustomAuthWithoutOicd' | ||
import { UserAccountContext } from 'context/UserAccountContext' | ||
import countries from 'i18n-iso-countries' | ||
import COUNTRIES_FR from 'i18n-iso-countries/langs/fr.json' | ||
import { RouterProvider } from 'react-router-dom' | ||
import { CustomProvider as RsuiteCustomProvider } from 'rsuite' | ||
import rsuiteFrFr from 'rsuite/locales/fr_FR' | ||
|
||
import { router } from './router' | ||
|
||
countries.registerLocale(COUNTRIES_FR) | ||
|
||
export function AppWithoutOicd() { | ||
const { isAuthorized, isLoading, userAccount } = useCustomAuthWithoutOicd() | ||
|
||
if (isLoading) { | ||
return <LandingPage /> | ||
} | ||
|
||
if (!isAuthorized || !userAccount) { | ||
return <LandingPage hasInsufficientRights /> | ||
} | ||
|
||
if (!isBrowserSupported()) { | ||
return <UnsupportedBrowserPage /> | ||
} | ||
|
||
return ( | ||
<UserAccountContext.Provider value={userAccount}> | ||
<ThemeProvider theme={THEME}> | ||
<GlobalStyle /> | ||
<CustomGlobalStyle /> | ||
|
||
<RsuiteCustomProvider locale={rsuiteFrFr}> | ||
<FrontendErrorBoundary> | ||
<RouterProvider router={router} /> | ||
</FrontendErrorBoundary> | ||
</RsuiteCustomProvider> | ||
</ThemeProvider> | ||
</UserAccountContext.Provider> | ||
) | ||
} |
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,38 @@ | ||
import { noop } from 'lodash' | ||
import { useEffect, useMemo, useState } from 'react' | ||
|
||
import { getCurrentUserAuthorization } from '../../domain/use_cases/authorization/getCurrentUserAuthorization' | ||
|
||
import type { UserAccountContextType } from '../../context/UserAccountContext' | ||
import type { UserAuthorization } from '../../domain/entities/authorization/types' | ||
|
||
export function useCustomAuthWithoutOicd(): { | ||
isAuthorized: boolean | ||
isLoading: boolean | ||
userAccount: UserAccountContextType | undefined | ||
} { | ||
const [userAuthorization, setUserAuthorization] = useState<UserAuthorization | undefined>(undefined) | ||
|
||
useEffect(() => { | ||
setTimeout(async () => { | ||
const nextUserAuthorization = await getCurrentUserAuthorization() | ||
|
||
setUserAuthorization(nextUserAuthorization) | ||
}, 250) | ||
}, []) | ||
|
||
const userAccount = useMemo( | ||
() => ({ | ||
email: 'bob@see.org', | ||
isSuperUser: userAuthorization?.isSuperUser ?? false, | ||
logout: noop | ||
}), | ||
[userAuthorization] | ||
) | ||
|
||
if (!userAuthorization || userAuthorization?.isLogged === undefined) { | ||
return { isAuthorized: false, isLoading: true, userAccount: undefined } | ||
} | ||
|
||
return { isAuthorized: true, isLoading: false, userAccount } | ||
} |
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