Skip to content

Commit 05daba1

Browse files
fix: use commons-ui config params api (app specific) (#127)
* fix: use commons-ui config params api (app specific) Signed-off-by: Joris Mancini <joris.mancini_externe@rte-france Signed-off-by: Joris Mancini <[email protected]> * chore: update commons-ui version Signed-off-by: Joris Mancini <[email protected]>.com> Signed-off-by: Joris Mancini <[email protected]> --------- Signed-off-by: Joris Mancini <joris.mancini_externe@rte-france Signed-off-by: Joris Mancini <[email protected]> Signed-off-by: Joris Mancini <[email protected]>.com>
1 parent 5e4330a commit 05daba1

File tree

5 files changed

+15
-40
lines changed

5 files changed

+15
-40
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"dependencies": {
1717
"@emotion/react": "^11.14.0",
1818
"@emotion/styled": "^11.14.0",
19-
"@gridsuite/commons-ui": "0.112.0",
19+
"@gridsuite/commons-ui": "0.120.0",
2020
"@hookform/resolvers": "^4.0.0",
2121
"@mui/icons-material": "^5.16.14",
2222
"@mui/lab": "5.0.0-alpha.175",

src/components/App/app.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import { Grid } from '@mui/material';
1111
import {
1212
AnnouncementNotification,
1313
CardErrorBoundary,
14+
fetchConfigParameter,
15+
fetchConfigParameters,
1416
NotificationsUrlKeys,
1517
useNotificationsListener,
1618
useSnackMessage,
1719
} from '@gridsuite/commons-ui';
1820
import { selectComputedLanguage, selectLanguage, selectTheme } from '../../redux/actions';
1921
import { AppState } from '../../redux/reducer';
20-
import { ConfigParameters, ConfigSrv } from '../../services';
22+
import { ConfigParameters } from '../../services';
2123
import { APP_NAME, COMMON_APP_NAME, PARAM_LANGUAGE, PARAM_THEME } from '../../utils/config-params';
2224
import { getComputedLanguage } from '../../utils/language';
2325
import AppTopBar from './app-top-bar';
@@ -56,7 +58,7 @@ export default function App({ children }: Readonly<PropsWithChildren<{}>>) {
5658
(event: MessageEvent) => {
5759
const eventData = JSON.parse(event.data);
5860
if (eventData?.headers?.parameterName) {
59-
ConfigSrv.fetchConfigParameter(eventData.headers.parameterName)
61+
fetchConfigParameter(APP_NAME, eventData.headers.parameterName)
6062
.then((param) => updateParams([param]))
6163
.catch((error) => snackError({ messageTxt: error.message, headerId: 'paramsRetrievingError' }));
6264
}
@@ -68,7 +70,7 @@ export default function App({ children }: Readonly<PropsWithChildren<{}>>) {
6870

6971
useEffect(() => {
7072
if (user !== null) {
71-
ConfigSrv.fetchConfigParameters(COMMON_APP_NAME)
73+
fetchConfigParameters(COMMON_APP_NAME)
7274
.then((params) => updateParams(params))
7375
.catch((error) =>
7476
snackError({
@@ -77,7 +79,7 @@ export default function App({ children }: Readonly<PropsWithChildren<{}>>) {
7779
})
7880
);
7981

80-
ConfigSrv.fetchConfigParameters(APP_NAME)
82+
fetchConfigParameters(APP_NAME)
8183
.then((params) => updateParams(params))
8284
.catch((error) =>
8385
snackError({

src/components/parameters.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import { useCallback, useEffect, useState } from 'react';
99
import { useSelector } from 'react-redux';
10-
import { ConfigSrv } from '../services';
11-
import { useSnackMessage } from '@gridsuite/commons-ui';
10+
import { updateConfigParameter, useSnackMessage } from '@gridsuite/commons-ui';
1211
import { AppState, AppStateKey } from '../redux/reducer';
12+
import { APP_NAME } from '../utils/config-params';
1313

1414
export function useParameterState<K extends AppStateKey>(paramName: K): [AppState[K], (value: AppState[K]) => void] {
1515
const { snackError } = useSnackMessage();
@@ -23,7 +23,7 @@ export function useParameterState<K extends AppStateKey>(paramName: K): [AppStat
2323
const handleChangeParamLocalState = useCallback(
2424
(value: AppState[K]) => {
2525
setParamLocalState(value);
26-
ConfigSrv.updateConfigParameter(paramName, value as string) //TODO how to check/cast?
26+
updateConfigParameter(APP_NAME, paramName, value as string) //TODO how to check/cast?
2727
.catch((error) => {
2828
setParamLocalState(paramGlobalState);
2929
snackError({

src/services/config.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
*/
77

88
import { GsLang, GsTheme } from '@gridsuite/commons-ui';
9-
import { APP_NAME, getAppName, PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
10-
import { backendFetch, backendFetchJson, getRestBase } from '../utils/api-rest';
11-
12-
const PREFIX_CONFIG_QUERIES = `${getRestBase()}/config`;
9+
import { PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
1310

1411
// https://github.com/gridsuite/config-server/blob/main/src/main/java/org/gridsuite/config/server/dto/ParameterInfos.java
1512
export type ConfigParameter =
@@ -22,27 +19,3 @@ export type ConfigParameter =
2219
value: GsTheme;
2320
};
2421
export type ConfigParameters = ConfigParameter[];
25-
26-
export function fetchConfigParameters(appName: string = APP_NAME): Promise<ConfigParameters> {
27-
console.debug(`Fetching UI configuration params for app : ${appName}`);
28-
const fetchParams = `${PREFIX_CONFIG_QUERIES}/v1/applications/${appName}/parameters`;
29-
return backendFetchJson(fetchParams) as Promise<ConfigParameters>;
30-
}
31-
32-
export function fetchConfigParameter(name: string): Promise<ConfigParameter> {
33-
const appName = getAppName(name);
34-
console.debug(`Fetching UI config parameter '${name}' for app '${appName}'`);
35-
const fetchParams = `${PREFIX_CONFIG_QUERIES}/v1/applications/${appName}/parameters/${name}`;
36-
return backendFetchJson(fetchParams) as Promise<ConfigParameter>;
37-
}
38-
39-
export function updateConfigParameter(name: string, value: Parameters<typeof encodeURIComponent>[0]): Promise<void> {
40-
const appName = getAppName(name);
41-
console.debug(`Updating config parameter '${name}=${value}' for app '${appName}'`);
42-
const updateParams = `${PREFIX_CONFIG_QUERIES}/v1/applications/${appName}/parameters/${name}?value=${encodeURIComponent(
43-
value
44-
)}`;
45-
return backendFetch(updateParams, {
46-
method: 'put',
47-
}) as Promise<unknown> as Promise<void>;
48-
}

0 commit comments

Comments
 (0)