Skip to content

Commit b82f444

Browse files
deduplicate config & local-storage
1 parent 7fc7192 commit b82f444

File tree

8 files changed

+56
-151
lines changed

8 files changed

+56
-151
lines changed

src/components/App/app-top-bar.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import {
1616
} from 'react';
1717
import { capitalize, Tab, Tabs, useTheme } from '@mui/material';
1818
import { ManageAccounts, PeopleAlt } from '@mui/icons-material';
19-
import { AppMetadataCommon, logout, TopBar } from '@gridsuite/commons-ui';
20-
import { useParameterState } from '../parameters';
2119
import {
22-
APP_NAME,
20+
AppMetadataCommon,
21+
logout,
2322
PARAM_LANGUAGE,
2423
PARAM_THEME,
25-
} from '../../utils/config-params';
24+
TopBar,
25+
} from '@gridsuite/commons-ui';
26+
import { useParameterState } from '../parameters';
27+
import { APP_NAME } from '../../utils/config-params';
2628
import { NavLink, useMatches, useNavigate } from 'react-router-dom';
2729
import { useDispatch, useSelector } from 'react-redux';
2830
import { FormattedMessage } from 'react-intl';

src/components/App/app-wrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
LIGHT_THEME,
2727
login_en,
2828
login_fr,
29+
PARAM_THEME,
2930
SnackbarProvider,
3031
top_bar_en,
3132
top_bar_fr,
@@ -35,7 +36,6 @@ import { Provider, useSelector } from 'react-redux';
3536
import messages_en from '../../translations/en.json';
3637
import messages_fr from '../../translations/fr.json';
3738
import { store } from '../../redux/store';
38-
import { PARAM_THEME } from '../../utils/config-params';
3939
import { AppState } from '../../redux/reducer';
4040
import { AppWithAuthRouter } from '../../routes';
4141

src/components/App/app.tsx

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import { useDispatch, useSelector } from 'react-redux';
1515
import { Grid } from '@mui/material';
1616
import {
1717
CardErrorBoundary,
18+
COMMON_APP_NAME,
1819
ConfigParameters,
20+
getComputedLanguage,
21+
PARAM_LANGUAGE,
22+
PARAM_THEME,
1923
useSnackMessage,
2024
} from '@gridsuite/commons-ui';
2125
import {
@@ -24,16 +28,9 @@ import {
2428
selectTheme,
2529
} from '../../redux/actions';
2630
import { AppState } from '../../redux/reducer';
27-
import { configSrv, configNotificationSrv } from '../../services';
28-
import {
29-
APP_NAME,
30-
COMMON_APP_NAME,
31-
PARAM_LANGUAGE,
32-
PARAM_THEME,
33-
} from '../../utils/config-params';
34-
import { getComputedLanguage } from '../../utils/language';
31+
import { configNotificationSrv, configSrv } from '../../services';
32+
import { APP_NAME } from '../../utils/config-params';
3533
import AppTopBar from './app-top-bar';
36-
import ReconnectingWebSocket from 'reconnecting-websocket';
3734
import { useDebugRender } from '../../utils/hooks';
3835
import { AppDispatch } from '../../redux/store';
3936

@@ -69,31 +66,28 @@ const App: FunctionComponent<PropsWithChildren<{}>> = (props, context) => {
6966
[dispatch]
7067
);
7168

72-
const connectNotificationsUpdateConfig =
73-
useCallback((): ReconnectingWebSocket => {
74-
const ws =
75-
configNotificationSrv.connectNotificationsWsUpdateConfig(
76-
APP_NAME
77-
);
78-
ws.onmessage = function (event) {
79-
let eventData = JSON.parse(event.data);
80-
if (eventData?.headers?.parameterName) {
81-
configSrv
82-
.fetchConfigParameter(eventData.headers.parameterName)
83-
.then((param) => updateParams([param]))
84-
.catch((error) =>
85-
snackError({
86-
messageTxt: error.message,
87-
headerId: 'paramsRetrievingError',
88-
})
89-
);
90-
}
91-
};
92-
ws.onerror = function (event) {
93-
console.error('Unexpected Notification WebSocket error', event);
94-
};
95-
return ws;
96-
}, [updateParams, snackError]);
69+
const connectNotificationsUpdateConfig = useCallback(() => {
70+
const ws =
71+
configNotificationSrv.connectNotificationsWsUpdateConfig(APP_NAME);
72+
ws.onmessage = function (event) {
73+
let eventData = JSON.parse(event.data);
74+
if (eventData?.headers?.parameterName) {
75+
configSrv
76+
.fetchConfigParameter(eventData.headers.parameterName)
77+
.then((param) => updateParams([param]))
78+
.catch((error) =>
79+
snackError({
80+
messageTxt: error.message,
81+
headerId: 'paramsRetrievingError',
82+
})
83+
);
84+
}
85+
};
86+
ws.onerror = function (event) {
87+
console.error('Unexpected Notification WebSocket error', event);
88+
};
89+
return ws;
90+
}, [updateParams, snackError]);
9791

9892
useEffect(() => {
9993
if (user !== null) {

src/redux/actions.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { GsTheme, UserManagerState } from '@gridsuite/commons-ui';
9-
import { PARAM_LANGUAGE } from '../utils/config-params';
8+
import {
9+
GsLang,
10+
GsTheme,
11+
PARAM_LANGUAGE,
12+
PARAM_THEME,
13+
UserManagerState,
14+
} from '@gridsuite/commons-ui';
1015
import { Action } from 'redux';
1116
import { AppState } from './reducer';
1217

@@ -54,7 +59,7 @@ export function updateUserManagerError(
5459

5560
export const SELECT_THEME = 'SELECT_THEME';
5661
export type ThemeAction = Readonly<Action<typeof SELECT_THEME>> & {
57-
theme: GsTheme;
62+
[PARAM_THEME]: AppState[typeof PARAM_THEME];
5863
};
5964

6065
export function selectTheme(theme: GsTheme): ThemeAction {
@@ -63,10 +68,10 @@ export function selectTheme(theme: GsTheme): ThemeAction {
6368

6469
export const SELECT_LANGUAGE = 'SELECT_LANGUAGE';
6570
export type LanguageAction = Readonly<Action<typeof SELECT_LANGUAGE>> & {
66-
[PARAM_LANGUAGE]: AppState['language'];
71+
[PARAM_LANGUAGE]: AppState[typeof PARAM_LANGUAGE];
6772
};
6873

69-
export function selectLanguage(language: AppState['language']): LanguageAction {
74+
export function selectLanguage(language: GsLang): LanguageAction {
7075
return { type: SELECT_LANGUAGE, [PARAM_LANGUAGE]: language };
7176
}
7277

src/redux/local-storage.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

src/redux/reducer.ts

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

88
import { createReducer, Draft } from '@reduxjs/toolkit';
9-
import {
10-
getLocalStorageComputedLanguage,
11-
getLocalStorageLanguage,
12-
getLocalStorageTheme,
13-
saveLocalStorageTheme,
14-
} from './local-storage';
15-
169
import {
1710
ComputedLanguageAction,
1811
LanguageAction,
@@ -30,12 +23,18 @@ import {
3023
AuthenticationActions,
3124
AuthenticationRouterErrorAction,
3225
AuthenticationRouterErrorState,
26+
getLocalStorageComputedLanguage,
27+
getLocalStorageLanguage,
28+
getLocalStorageTheme,
3329
GsLang,
3430
GsLangUser,
3531
GsTheme,
3632
LOGOUT_ERROR,
3733
LogoutErrorAction,
34+
PARAM_LANGUAGE,
35+
PARAM_THEME,
3836
RESET_AUTHENTICATION_ROUTER_ERROR,
37+
saveLocalStorageTheme,
3938
SHOW_AUTH_INFO_LOGIN,
4039
ShowAuthenticationRouterLoginAction,
4140
SIGNIN_CALLBACK_ERROR,
@@ -48,9 +47,9 @@ import {
4847
UserManagerState,
4948
UserValidationErrorAction,
5049
} from '@gridsuite/commons-ui';
51-
import { PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
5250
import { ReducerWithInitialState } from '@reduxjs/toolkit/dist/createReducer';
5351
import { User } from 'oidc-client';
52+
import { APP_NAME } from '../utils/config-params';
5453

5554
export type AppState = {
5655
user?: User;
@@ -76,9 +75,9 @@ const initialState: AppState = {
7675
showAuthenticationRouterLogin: false,
7776

7877
// params
79-
[PARAM_THEME]: getLocalStorageTheme(),
80-
[PARAM_LANGUAGE]: getLocalStorageLanguage(),
81-
computedLanguage: getLocalStorageComputedLanguage(),
78+
[PARAM_THEME]: getLocalStorageTheme(APP_NAME),
79+
[PARAM_LANGUAGE]: getLocalStorageLanguage(APP_NAME),
80+
computedLanguage: getLocalStorageComputedLanguage(APP_NAME),
8281
};
8382

8483
export type Actions =
@@ -99,7 +98,7 @@ export const reducer: ReducerWithInitialState<AppState> = createReducer(
9998
SELECT_THEME,
10099
(state: Draft<AppState>, action: ThemeAction) => {
101100
state.theme = action.theme;
102-
saveLocalStorageTheme(state.theme);
101+
saveLocalStorageTheme(APP_NAME, state.theme);
103102
}
104103
);
105104

src/utils/config-params.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,4 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77

8-
import { LiteralUnion } from 'type-fest';
9-
10-
export const COMMON_APP_NAME = 'common';
118
export const APP_NAME = 'admin';
12-
13-
export const PARAM_THEME = 'theme';
14-
export const PARAM_LANGUAGE = 'language';
15-
16-
const COMMON_CONFIG_PARAMS_NAMES = new Set([PARAM_THEME, PARAM_LANGUAGE]);
17-
18-
export type AppConfigParameter = LiteralUnion<
19-
typeof PARAM_THEME | typeof PARAM_LANGUAGE,
20-
string
21-
>;
22-
23-
export type AppConfigType = typeof COMMON_APP_NAME | typeof APP_NAME;
24-
25-
/**
26-
* Permit knowing if a parameter is common/shared between webapps or is specific to this application.
27-
* @param paramName the parameter name/key
28-
*/
29-
export function getAppName(paramName: AppConfigParameter): AppConfigType {
30-
return COMMON_CONFIG_PARAMS_NAMES.has(paramName)
31-
? COMMON_APP_NAME
32-
: APP_NAME;
33-
}

src/utils/language.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)