Skip to content

Commit 7b2d5e0

Browse files
Update types for typescript fronts (#514)
* expose `GridSuiteModule` * expose lang & theme types * Add missing types for common store actions * Use correct types in router * fix nullable user in store * fix router args types
1 parent f8f86f2 commit 7b2d5e0

File tree

11 files changed

+164
-65
lines changed

11 files changed

+164
-65
lines changed

demo/src/app.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ import searchEquipments from '../data/EquipmentSearchBar';
9595
import { EquipmentItem } from '../../src/components/ElementSearchDialog/equipment-item';
9696
import OverflowableText from '../../src/components/OverflowableText';
9797

98-
import { setShowAuthenticationRouterLogin } from '../../src/redux/actions';
98+
import { setShowAuthenticationRouterLogin } from '../../src/redux/authActions';
9999
import TableTab from './TableTab';
100100
import FlatParametersTab from './FlatParametersTab';
101101

src/components/AuthenticationRouter/AuthenticationRouter.tsx

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
*/
77

88
import { Dispatch, useCallback } from 'react';
9-
import { Navigate, Route, Routes } from 'react-router-dom';
9+
import {
10+
Location,
11+
Navigate,
12+
NavigateFunction,
13+
Route,
14+
Routes,
15+
} from 'react-router-dom';
1016
import { Alert, AlertTitle, Grid } from '@mui/material';
1117
import { FormattedMessage } from 'react-intl';
1218
import { UserManager } from 'oidc-client';
@@ -21,22 +27,27 @@ import SilentRenewCallbackHandler from '../SilentRenewCallbackHandler';
2127
import Login from '../Login';
2228
import Logout from '../Login/Logout';
2329

30+
import { AuthenticationActions } from '../../redux/authActions';
31+
32+
export type AuthenticationRouterErrorState = {
33+
userName?: string;
34+
userValidationError?: { error: Error };
35+
logoutError?: { error: Error };
36+
unauthorizedUserInfo?: string;
37+
};
38+
39+
export type UserManagerState = {
40+
instance: UserManager | null;
41+
error: string | null;
42+
};
43+
2444
export interface AuthenticationRouterProps {
25-
userManager: {
26-
instance: UserManager;
27-
error: string;
28-
};
29-
signInCallbackError: { message: string };
30-
authenticationRouterError: {
31-
userName: string;
32-
userValidationError?: { error: Error };
33-
logoutError?: { error: Error };
34-
unauthorizedUserInfo?: string;
35-
error: string;
36-
};
45+
userManager: UserManagerState;
46+
signInCallbackError: Error | null;
47+
authenticationRouterError: AuthenticationRouterErrorState | null;
3748
showAuthenticationRouterLogin: boolean;
38-
dispatch: Dispatch<unknown>;
39-
navigate: () => void;
49+
dispatch: Dispatch<AuthenticationActions>;
50+
navigate: NavigateFunction;
4051
location: Location;
4152
}
4253

@@ -48,16 +59,17 @@ function AuthenticationRouter({
4859
dispatch,
4960
navigate,
5061
location,
51-
}: AuthenticationRouterProps) {
52-
const handleSigninCallbackClosure = useCallback(
53-
() => handleSigninCallback(dispatch, navigate, userManager.instance),
54-
[dispatch, navigate, userManager.instance]
55-
);
56-
const handleSilentRenewCallbackClosure = useCallback(
57-
() => handleSilentRenewCallback(userManager.instance),
58-
[userManager.instance]
59-
);
60-
62+
}: Readonly<AuthenticationRouterProps>) {
63+
const handleSigninCallbackClosure = useCallback(() => {
64+
if (userManager.instance != null) {
65+
handleSigninCallback(dispatch, navigate, userManager.instance);
66+
}
67+
}, [dispatch, navigate, userManager.instance]);
68+
const handleSilentRenewCallbackClosure = useCallback(() => {
69+
if (userManager.instance != null) {
70+
handleSilentRenewCallback(userManager.instance);
71+
}
72+
}, [userManager.instance]);
6173
return (
6274
<Grid
6375
container
@@ -185,4 +197,5 @@ function AuthenticationRouter({
185197
</Grid>
186198
);
187199
}
200+
188201
export default AuthenticationRouter;

src/components/AuthenticationRouter/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
7-
import defaultAuthenticationRouter from './AuthenticationRouter';
7+
import AuthenticationRouter from './AuthenticationRouter';
88

9-
export default defaultAuthenticationRouter;
9+
export default AuthenticationRouter;
10+
export type {
11+
AuthenticationRouterErrorState,
12+
AuthenticationRouterProps,
13+
UserManagerState,
14+
} from './AuthenticationRouter';

src/components/SignInCallbackHandler/SignInCallbackHandler.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useEffect } from 'react';
99
import { UserManager } from 'oidc-client';
1010

1111
export interface SignInCallbackHandlerProps {
12-
userManager: UserManager;
12+
userManager: UserManager | null;
1313
handleSignInCallback: () => void;
1414
}
1515

src/components/SilentRenewCallbackHandler/SilentRenewCallbackHandler.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useEffect } from 'react';
99
import { UserManager } from 'oidc-client';
1010

1111
export interface SilentRenewCallbackHandlerProps {
12-
userManager: UserManager;
12+
userManager: UserManager | null;
1313
handleSilentRenewCallback: () => void;
1414
}
1515

src/components/TopBar/AboutDialog.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function compareModules(c1: ModuleDefinition, c2: ModuleDefinition) {
134134
);
135135
}
136136

137-
type GridSuiteModule = {
137+
export type GridSuiteModule = {
138138
name: string;
139139
type: ModuleType;
140140
version?: string;

src/components/TopBar/TopBar.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,16 @@ export const LANG_FRENCH = 'fr';
159159
const EN = 'EN';
160160
const FR = 'FR';
161161

162-
export type GsLang =
163-
| typeof LANG_ENGLISH
164-
| typeof LANG_FRENCH
165-
| typeof LANG_SYSTEM;
162+
export type GsLangUser = typeof LANG_ENGLISH | typeof LANG_FRENCH;
163+
export type GsLang = GsLangUser | typeof LANG_SYSTEM;
166164
export type GsTheme = typeof LIGHT_THEME | typeof DARK_THEME;
167165

168166
export type TopBarProps = Omit<GridLogoProps, 'onClick'> &
169167
Omit<LogoutProps, 'disabled'> &
170168
Omit<AboutDialogProps, 'open' | 'onClose'> & {
171169
onParametersClick?: () => void;
172170
onLogoClick: GridLogoProps['onClick'];
173-
user: User;
171+
user?: User;
174172
onAboutClick?: () => void;
175173
logoAboutDialog?: ReactNode;
176174
appsAndUrls: CommonMetadata[];

src/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88
export { TreeViewFinder } from './components/TreeViewFinder';
99
export { TopBar } from './components/TopBar';
1010
export { default as AboutDialog } from './components/TopBar/AboutDialog';
11+
export type { GridSuiteModule } from './components/TopBar/AboutDialog';
1112
export { default as SnackbarProvider } from './components/SnackbarProvider';
1213
export { default as AuthenticationRouter } from './components/AuthenticationRouter';
14+
export type {
15+
AuthenticationRouterErrorState,
16+
AuthenticationRouterProps,
17+
UserManagerState,
18+
} from './components/AuthenticationRouter';
1319
export { MuiVirtualizedTable } from './components/MuiVirtualizedTable';
1420
export {
1521
KeyedColumnsRowIndexer,
@@ -96,6 +102,7 @@ export {
96102
LANG_ENGLISH,
97103
LANG_FRENCH,
98104
} from './components/TopBar/TopBar';
105+
export type { GsLang, GsLangUser, GsTheme } from './components/TopBar/TopBar';
99106

100107
export {
101108
USER,
@@ -107,7 +114,18 @@ export {
107114
USER_VALIDATION_ERROR,
108115
RESET_AUTHENTICATION_ROUTER_ERROR,
109116
SHOW_AUTH_INFO_LOGIN,
110-
} from './redux/actions';
117+
} from './redux/authActions';
118+
export type {
119+
AuthenticationActions,
120+
AuthenticationRouterErrorBase,
121+
AuthenticationRouterErrorAction,
122+
LogoutErrorAction,
123+
ShowAuthenticationRouterLoginAction,
124+
SignInCallbackErrorAction,
125+
UnauthorizedUserAction,
126+
UserAction,
127+
UserValidationErrorAction,
128+
} from './redux/authActions';
111129
export { default as report_viewer_en } from './components/translations/report-viewer-en';
112130
export { default as report_viewer_fr } from './components/translations/report-viewer-fr';
113131
export { default as login_en } from './components/translations/login-en';
@@ -227,6 +245,7 @@ export {
227245

228246
export { mergeSx } from './utils/styles';
229247
export { setCommonStore } from './redux/commonStore';
248+
export type { CommonStoreState } from './redux/commonStore';
230249
export type { EquipmentInfos } from './utils/EquipmentType';
231250

232251
export * from './services';

src/redux/actions.ts renamed to src/redux/authActions.ts

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,55 @@
66
*/
77
import { User } from 'oidc-client';
88

9+
// Is redux or isn't redux, that is the question, 🎭
10+
export interface Action<T> {
11+
type: T;
12+
}
13+
type ReadonlyAction<T> = Readonly<Action<T>>;
14+
915
export const USER = 'USER';
16+
export type UserAction = ReadonlyAction<typeof USER> & {
17+
user: User | null;
18+
};
1019

11-
export function setLoggedUser(user: User | null) {
20+
export function setLoggedUser(user: User | null): UserAction {
1221
return { type: USER, user };
1322
}
1423

1524
export const SIGNIN_CALLBACK_ERROR = 'SIGNIN_CALLBACK_ERROR';
25+
export type SignInCallbackErrorAction = ReadonlyAction<
26+
typeof SIGNIN_CALLBACK_ERROR
27+
> & {
28+
signInCallbackError: Error | null;
29+
};
1630

17-
export function setSignInCallbackError(signInCallbackError: string | null) {
31+
export function setSignInCallbackError(
32+
signInCallbackError: Error | null
33+
): SignInCallbackErrorAction {
1834
return {
1935
type: SIGNIN_CALLBACK_ERROR,
2036
signInCallbackError,
2137
};
2238
}
2339

40+
export type AuthenticationRouterErrorBase<T> = {
41+
authenticationRouterError: {
42+
userName?: string;
43+
} & T;
44+
};
45+
2446
export const UNAUTHORIZED_USER_INFO = 'UNAUTHORIZED_USER_INFO';
47+
export type UnauthorizedUserAction = ReadonlyAction<
48+
typeof UNAUTHORIZED_USER_INFO
49+
> &
50+
AuthenticationRouterErrorBase<{
51+
unauthorizedUserInfo: string;
52+
}>;
2553

2654
export function setUnauthorizedUserInfo(
2755
userName: string | undefined,
2856
unauthorizedUserInfo: string
29-
) {
57+
): UnauthorizedUserAction {
3058
return {
3159
type: UNAUTHORIZED_USER_INFO,
3260
authenticationRouterError: {
@@ -37,11 +65,15 @@ export function setUnauthorizedUserInfo(
3765
}
3866

3967
export const LOGOUT_ERROR = 'LOGOUT_ERROR';
68+
export type LogoutErrorAction = ReadonlyAction<typeof LOGOUT_ERROR> &
69+
AuthenticationRouterErrorBase<{
70+
logoutError: { error: Error };
71+
}>;
4072

4173
export function setLogoutError(
4274
userName: string | undefined,
4375
logoutError: { error: Error }
44-
) {
76+
): LogoutErrorAction {
4577
return {
4678
type: LOGOUT_ERROR,
4779
authenticationRouterError: {
@@ -52,11 +84,17 @@ export function setLogoutError(
5284
}
5385

5486
export const USER_VALIDATION_ERROR = 'USER_VALIDATION_ERROR';
87+
export type UserValidationErrorAction = ReadonlyAction<
88+
typeof USER_VALIDATION_ERROR
89+
> &
90+
AuthenticationRouterErrorBase<{
91+
userValidationError: { error: Error };
92+
}>;
5593

5694
export function setUserValidationError(
5795
userName: string | undefined,
5896
userValidationError: { error: Error }
59-
) {
97+
): UserValidationErrorAction {
6098
return {
6199
type: USER_VALIDATION_ERROR,
62100
authenticationRouterError: {
@@ -68,21 +106,40 @@ export function setUserValidationError(
68106

69107
export const RESET_AUTHENTICATION_ROUTER_ERROR =
70108
'RESET_AUTHENTICATION_ROUTER_ERROR';
109+
export type AuthenticationRouterErrorAction = ReadonlyAction<
110+
typeof RESET_AUTHENTICATION_ROUTER_ERROR
111+
> & {
112+
authenticationRouterError: null;
113+
};
71114

72-
export function resetAuthenticationRouterError() {
115+
export function resetAuthenticationRouterError(): AuthenticationRouterErrorAction {
73116
return {
74117
type: RESET_AUTHENTICATION_ROUTER_ERROR,
75118
authenticationRouterError: null,
76119
};
77120
}
78121

79122
export const SHOW_AUTH_INFO_LOGIN = 'SHOW_AUTH_INFO_LOGIN';
123+
export type ShowAuthenticationRouterLoginAction = ReadonlyAction<
124+
typeof SHOW_AUTH_INFO_LOGIN
125+
> & {
126+
showAuthenticationRouterLogin: boolean;
127+
};
80128

81129
export function setShowAuthenticationRouterLogin(
82130
showAuthenticationRouterLogin: boolean
83-
) {
131+
): ShowAuthenticationRouterLoginAction {
84132
return {
85133
type: SHOW_AUTH_INFO_LOGIN,
86134
showAuthenticationRouterLogin,
87135
};
88136
}
137+
138+
export type AuthenticationActions =
139+
| UserAction
140+
| SignInCallbackErrorAction
141+
| UnauthorizedUserAction
142+
| LogoutErrorAction
143+
| UserValidationErrorAction
144+
| AuthenticationRouterErrorAction
145+
| ShowAuthenticationRouterLoginAction;

src/redux/commonStore.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77

88
import { User } from 'oidc-client';
99

10+
export type CommonStoreState = {
11+
user: User | null;
12+
};
13+
1014
interface CommonStore {
11-
getState: () => { user: User };
15+
getState(): CommonStoreState;
1216
}
1317

1418
let commonStore: CommonStore | undefined;
@@ -23,5 +27,5 @@ export function setCommonStore(store: CommonStore): void {
2327
}
2428

2529
export function getUserToken() {
26-
return commonStore?.getState().user.id_token;
30+
return commonStore?.getState().user?.id_token;
2731
}

0 commit comments

Comments
 (0)