From ceb0a7648c7a24530142600bf5f809c5d1ecc3f1 Mon Sep 17 00:00:00 2001 From: capyq Date: Thu, 1 Aug 2024 12:37:35 +0200 Subject: [PATCH 1/3] split component into atomic component Signed-off-by: capyq --- .../AuthenticationRouter.tsx | 123 ++---------------- .../AuthenticationRouterErrorDisplay.tsx | 52 ++++++++ .../alert/ErrorInLogoutAlert.tsx | 23 ++++ .../alert/ErrorInUserValidationAlert.tsx | 25 ++++ .../alert/UnauthorizedAccessAlert.tsx | 18 +++ .../authenticationType.ts | 26 ++++ src/components/AuthenticationRouter/index.ts | 2 +- 7 files changed, 159 insertions(+), 110 deletions(-) create mode 100644 src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx create mode 100644 src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx create mode 100644 src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx create mode 100644 src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx create mode 100644 src/components/AuthenticationRouter/authenticationType.ts diff --git a/src/components/AuthenticationRouter/AuthenticationRouter.tsx b/src/components/AuthenticationRouter/AuthenticationRouter.tsx index 2772b73d..d9cd7053 100644 --- a/src/components/AuthenticationRouter/AuthenticationRouter.tsx +++ b/src/components/AuthenticationRouter/AuthenticationRouter.tsx @@ -5,51 +5,19 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -import { Dispatch, useCallback } from 'react'; -import { - Location, - Navigate, - NavigateFunction, - Route, - Routes, -} from 'react-router-dom'; -import { Alert, AlertTitle, Grid } from '@mui/material'; -import { FormattedMessage } from 'react-intl'; -import { UserManager } from 'oidc-client'; +import { useCallback } from 'react'; +import { Navigate, Route, Routes } from 'react-router-dom'; +import { Grid } from '@mui/material'; import SignInCallbackHandler from '../SignInCallbackHandler'; import { handleSigninCallback, handleSilentRenewCallback, login, - logout, } from '../../utils/AuthService'; import SilentRenewCallbackHandler from '../SilentRenewCallbackHandler'; import Login from '../Login'; -import Logout from '../Login/Logout'; - -import { AuthenticationActions } from '../../redux/authActions'; - -export type AuthenticationRouterErrorState = { - userName?: string; - userValidationError?: { error: Error }; - logoutError?: { error: Error }; - unauthorizedUserInfo?: string; -}; - -export type UserManagerState = { - instance: UserManager | null; - error: string | null; -}; - -export interface AuthenticationRouterProps { - userManager: UserManagerState; - signInCallbackError: Error | null; - authenticationRouterError: AuthenticationRouterErrorState | null; - showAuthenticationRouterLogin: boolean; - dispatch: Dispatch; - navigate: NavigateFunction; - location: Location; -} +import { AuthenticationRouterProps } from './authenticationType'; +import AuthenticationRouterErrorDisplay from './AuthenticationRouterErrorDisplay'; function AuthenticationRouter({ userManager, @@ -61,12 +29,12 @@ function AuthenticationRouter({ location, }: Readonly) { const handleSigninCallbackClosure = useCallback(() => { - if (userManager.instance != null) { + if (userManager.instance) { handleSigninCallback(dispatch, navigate, userManager.instance); } }, [dispatch, navigate, userManager.instance]); const handleSilentRenewCallbackClosure = useCallback(() => { - if (userManager.instance != null) { + if (userManager.instance) { handleSilentRenewCallback(userManager.instance); } }, [userManager.instance]); @@ -77,10 +45,10 @@ function AuthenticationRouter({ alignItems="center" direction="column" > - {userManager.error !== null && ( + {userManager.error && (

Error : Getting userManager; {userManager.error}

)} - {signInCallbackError !== null && ( + {signInCallbackError && (

Error : SignIn Callback Error; {signInCallbackError.message} @@ -125,74 +93,11 @@ function AuthenticationRouter({ {authenticationRouterError !== null && ( - <> - - - logout(dispatch, userManager.instance) - } - /> - - - {authenticationRouterError.logoutError != null && ( - - - - - -

- { - authenticationRouterError.logoutError - .error.message - } -

-
- )} - {authenticationRouterError?.userValidationError != - null && ( - - - - - -

- { - authenticationRouterError - .userValidationError.error.message - } -

-
- )} - {authenticationRouterError?.unauthorizedUserInfo != - null && ( - - - - - - - )} -
- + )} ); diff --git a/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx b/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx new file mode 100644 index 00000000..6a7e57a0 --- /dev/null +++ b/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx @@ -0,0 +1,52 @@ +import { Grid } from '@mui/material'; +import Logout from '../Login/Logout'; +import ErrorInLogoutAlert from './alert/ErrorInLogoutAlert'; +import ErrorInUserValidationAlert from './alert/ErrorInUserValidationAlert'; +import UnauthorizedAccessAlert from './alert/UnauthorizedAccessAlert'; +import { + AuthenticationRouterErrorState, + AuthenticationRouterProps, + UserManagerState, +} from './authenticationType'; +import { logout } from '../../utils/AuthService'; + +type AuthenticationRouterErrorDisplayProps = { + errorState: AuthenticationRouterErrorState; + instance: UserManagerState['instance']; + dispatch: AuthenticationRouterProps['dispatch']; +}; + +function AuthenticationRouterErrorDisplay({ + errorState, + instance, + dispatch, +}: AuthenticationRouterErrorDisplayProps) { + return ( + <> + + logout(dispatch, instance)} + /> + + + {errorState.logoutError && ( + + )} + {errorState.userValidationError && ( + + )} + {errorState.unauthorizedUserInfo && ( + + )} + + + ); +} +export default AuthenticationRouterErrorDisplay; diff --git a/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx b/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx new file mode 100644 index 00000000..0a90f6fe --- /dev/null +++ b/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx @@ -0,0 +1,23 @@ +import { Alert, AlertTitle } from '@mui/material'; +import { FormattedMessage } from 'react-intl'; + +type ErrorInLogoutAlertProps = { + userName?: string; + message: string; +}; +function ErrorInLogoutAlert({ userName, message }: ErrorInLogoutAlertProps) { + return ( + + + + + +

{message}

+
+ ); +} + +export default ErrorInLogoutAlert; diff --git a/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx b/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx new file mode 100644 index 00000000..72e41de2 --- /dev/null +++ b/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx @@ -0,0 +1,25 @@ +import { Alert, AlertTitle } from '@mui/material'; +import { FormattedMessage } from 'react-intl'; + +type ErrorInUserValidationAlertProps = { + userName?: string; + message: string; +}; +function ErrorInUserValidationAlert({ + userName, + message, +}: ErrorInUserValidationAlertProps) { + return ( + + + + + +

{message}

+
+ ); +} +export default ErrorInUserValidationAlert; diff --git a/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx b/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx new file mode 100644 index 00000000..18197694 --- /dev/null +++ b/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx @@ -0,0 +1,18 @@ +import { Alert, AlertTitle } from '@mui/material'; +import { FormattedMessage } from 'react-intl'; + +type UnauthorizedAccessAlertProps = { userName?: string }; +function UnauthorizedAccessAlert({ userName }: UnauthorizedAccessAlertProps) { + return ( + + + + + + + ); +} +export default UnauthorizedAccessAlert; diff --git a/src/components/AuthenticationRouter/authenticationType.ts b/src/components/AuthenticationRouter/authenticationType.ts new file mode 100644 index 00000000..3bd3fc15 --- /dev/null +++ b/src/components/AuthenticationRouter/authenticationType.ts @@ -0,0 +1,26 @@ +import { UserManager } from 'oidc-client'; +import { Dispatch } from 'react'; +import { NavigateFunction, Location } from 'react-router-dom'; +import { AuthenticationActions } from '../../redux/authActions'; + +export type AuthenticationRouterErrorState = { + userName?: string; + userValidationError?: { error: Error }; + logoutError?: { error: Error }; + unauthorizedUserInfo?: string; +}; + +export type UserManagerState = { + instance: UserManager | null; + error: string | null; +}; + +export interface AuthenticationRouterProps { + userManager: UserManagerState; + signInCallbackError: Error | null; + authenticationRouterError: AuthenticationRouterErrorState | null; + showAuthenticationRouterLogin: boolean; + dispatch: Dispatch; + navigate: NavigateFunction; + location: Location; +} diff --git a/src/components/AuthenticationRouter/index.ts b/src/components/AuthenticationRouter/index.ts index 1126b6ef..6be5d7a4 100644 --- a/src/components/AuthenticationRouter/index.ts +++ b/src/components/AuthenticationRouter/index.ts @@ -11,4 +11,4 @@ export type { AuthenticationRouterErrorState, AuthenticationRouterProps, UserManagerState, -} from './AuthenticationRouter'; +} from './authenticationType'; From 20fae71f0dd3f5a81f8a1a74d16a4698a9559048 Mon Sep 17 00:00:00 2001 From: capyq Date: Thu, 1 Aug 2024 13:31:01 +0200 Subject: [PATCH 2/3] adding licence Signed-off-by: capyq --- .../AuthenticationRouterErrorDisplay.tsx | 7 +++++++ .../AuthenticationRouter/alert/ErrorInLogoutAlert.tsx | 7 +++++++ .../alert/ErrorInUserValidationAlert.tsx | 7 +++++++ .../AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx | 7 +++++++ src/components/AuthenticationRouter/authenticationType.ts | 7 +++++++ 5 files changed, 35 insertions(+) diff --git a/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx b/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx index 6a7e57a0..2a232aff 100644 --- a/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx +++ b/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + import { Grid } from '@mui/material'; import Logout from '../Login/Logout'; import ErrorInLogoutAlert from './alert/ErrorInLogoutAlert'; diff --git a/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx b/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx index 0a90f6fe..f725a503 100644 --- a/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx +++ b/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + import { Alert, AlertTitle } from '@mui/material'; import { FormattedMessage } from 'react-intl'; diff --git a/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx b/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx index 72e41de2..a25f56a0 100644 --- a/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx +++ b/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + import { Alert, AlertTitle } from '@mui/material'; import { FormattedMessage } from 'react-intl'; diff --git a/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx b/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx index 18197694..39976c87 100644 --- a/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx +++ b/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + import { Alert, AlertTitle } from '@mui/material'; import { FormattedMessage } from 'react-intl'; diff --git a/src/components/AuthenticationRouter/authenticationType.ts b/src/components/AuthenticationRouter/authenticationType.ts index 3bd3fc15..92e765b8 100644 --- a/src/components/AuthenticationRouter/authenticationType.ts +++ b/src/components/AuthenticationRouter/authenticationType.ts @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2024, RTE (http://www.rte-france.com) + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + import { UserManager } from 'oidc-client'; import { Dispatch } from 'react'; import { NavigateFunction, Location } from 'react-router-dom'; From 5f923ba7d043d04ffa72001a7671bff0e77c278c Mon Sep 17 00:00:00 2001 From: capyq Date: Thu, 8 Aug 2024 11:13:19 +0200 Subject: [PATCH 3/3] refactor:update after merge Signed-off-by: capyq --- .../AuthenticationRouterErrorDisplay.tsx | 26 ++++--------------- .../alert/ErrorInLogoutAlert.tsx | 5 +--- .../alert/ErrorInUserValidationAlert.tsx | 10 ++----- .../alert/UnauthorizedAccessAlert.tsx | 5 +--- src/components/AuthenticationRouter/index.ts | 6 +---- 5 files changed, 10 insertions(+), 42 deletions(-) diff --git a/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx b/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx index 2a232aff..2ffaf272 100644 --- a/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx +++ b/src/components/AuthenticationRouter/AuthenticationRouterErrorDisplay.tsx @@ -10,11 +10,7 @@ import Logout from '../Login/Logout'; import ErrorInLogoutAlert from './alert/ErrorInLogoutAlert'; import ErrorInUserValidationAlert from './alert/ErrorInUserValidationAlert'; import UnauthorizedAccessAlert from './alert/UnauthorizedAccessAlert'; -import { - AuthenticationRouterErrorState, - AuthenticationRouterProps, - UserManagerState, -} from './authenticationType'; +import { AuthenticationRouterErrorState, AuthenticationRouterProps, UserManagerState } from './authenticationType'; import { logout } from '../../utils/AuthService'; type AuthenticationRouterErrorDisplayProps = { @@ -23,25 +19,15 @@ type AuthenticationRouterErrorDisplayProps = { dispatch: AuthenticationRouterProps['dispatch']; }; -function AuthenticationRouterErrorDisplay({ - errorState, - instance, - dispatch, -}: AuthenticationRouterErrorDisplayProps) { +function AuthenticationRouterErrorDisplay({ errorState, instance, dispatch }: AuthenticationRouterErrorDisplayProps) { return ( <> - logout(dispatch, instance)} - /> + logout(dispatch, instance)} /> {errorState.logoutError && ( - + )} {errorState.userValidationError && ( )} - {errorState.unauthorizedUserInfo && ( - - )} + {errorState.unauthorizedUserInfo && } ); diff --git a/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx b/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx index f725a503..9dbd91db 100644 --- a/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx +++ b/src/components/AuthenticationRouter/alert/ErrorInLogoutAlert.tsx @@ -18,10 +18,7 @@ function ErrorInLogoutAlert({ userName, message }: ErrorInLogoutAlertProps) { - +

{message}

); diff --git a/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx b/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx index a25f56a0..50996e4a 100644 --- a/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx +++ b/src/components/AuthenticationRouter/alert/ErrorInUserValidationAlert.tsx @@ -12,19 +12,13 @@ type ErrorInUserValidationAlertProps = { userName?: string; message: string; }; -function ErrorInUserValidationAlert({ - userName, - message, -}: ErrorInUserValidationAlertProps) { +function ErrorInUserValidationAlert({ userName, message }: ErrorInUserValidationAlertProps) { return ( - +

{message}

); diff --git a/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx b/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx index 39976c87..491bdb8c 100644 --- a/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx +++ b/src/components/AuthenticationRouter/alert/UnauthorizedAccessAlert.tsx @@ -15,10 +15,7 @@ function UnauthorizedAccessAlert({ userName }: UnauthorizedAccessAlertProps) { - + ); } diff --git a/src/components/AuthenticationRouter/index.ts b/src/components/AuthenticationRouter/index.ts index 6be5d7a4..ddd9601f 100644 --- a/src/components/AuthenticationRouter/index.ts +++ b/src/components/AuthenticationRouter/index.ts @@ -7,8 +7,4 @@ import AuthenticationRouter from './AuthenticationRouter'; export default AuthenticationRouter; -export type { - AuthenticationRouterErrorState, - AuthenticationRouterProps, - UserManagerState, -} from './authenticationType'; +export type { AuthenticationRouterErrorState, AuthenticationRouterProps, UserManagerState } from './authenticationType';