Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Feb 21, 2024
1 parent af0f007 commit 6f03f01
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/components/app-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import App from './app';
import React, { FunctionComponent } from 'react';
import { CssBaseline } from '@mui/material';
import {
createTheme,
StyledEngineProvider,
Expand All @@ -33,7 +34,6 @@ import messages_fr from '../translations/fr.json';
import messages_plugins_en from '../plugins/translations/en.json';
import messages_plugins_fr from '../plugins/translations/fr.json';
import { store } from '../redux/store';
import CssBaseline from '@mui/material/CssBaseline';
import { PARAM_THEME } from '../utils/config-params';
import { IntlConfig } from 'react-intl/src/types';
import { AppState } from '../redux/reducer';
Expand Down
28 changes: 13 additions & 15 deletions src/components/parameters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import {
Tab,
Tabs,
Typography,
TypographyTypeMap,
} from '@mui/material';
import { CSSObject, Theme } from '@emotion/react';
import { updateConfigParameter } from '../utils/rest-api';
import { useSnackMessage } from '@gridsuite/commons-ui';
import { AppState } from '../redux/reducer';
import { TypographyTypeMap } from '@mui/material/Typography/Typography';
import { AppState, AppStateKey } from '../redux/reducer';

const styles = {
title: (theme: Theme): CSSObject => ({
Expand All @@ -48,30 +48,28 @@ const styles = {
} as CSSObject,
};

export function useParameterState<
K extends keyof AppState,
T extends AppState[K]
>(paramName: K): [T, (value: T) => void] {
export function useParameterState<K extends AppStateKey>(
paramName: K
): [AppState[K], (value: AppState[K]) => void] {
const { snackError } = useSnackMessage();

const paramGlobalState = useSelector((state: AppState) => state[paramName]);

const [paramLocalState, setParamLocalState] = useState(paramGlobalState);

useEffect(() => {
setParamLocalState(paramGlobalState);
}, [paramGlobalState]);

const handleChangeParamLocalState = useCallback(
(value: T) => {
(value: AppState[K]) => {
setParamLocalState(value);
updateConfigParameter(paramName, value).catch((error) => {
setParamLocalState(paramGlobalState);
snackError({
messageTxt: error.message,
headerId: 'paramsChangingError',
updateConfigParameter(paramName, value as string) //TODO how to check/cast?
.catch((error) => {
setParamLocalState(paramGlobalState);
snackError({
messageTxt: error.message,
headerId: 'paramsChangingError',
});
});
});
},
[paramName, snackError, setParamLocalState, paramGlobalState]
);
Expand Down
9 changes: 5 additions & 4 deletions src/redux/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { PARAM_LANGUAGE } from '../utils/config-params';
import { Action } from 'redux';
import { AppState } from './reducer';

export const SELECT_THEME = 'SELECT_THEME';
export type ThemeAction = Readonly<Action<typeof SELECT_THEME>> & {
Expand All @@ -18,20 +19,20 @@ export function selectTheme(theme: string): ThemeAction {

export const SELECT_LANGUAGE = 'SELECT_LANGUAGE';
export type LanguageAction = Readonly<Action<typeof SELECT_LANGUAGE>> & {
[PARAM_LANGUAGE]: string;
[PARAM_LANGUAGE]: AppState['language'];
};
export function selectLanguage(language: string): LanguageAction {
export function selectLanguage(language: AppState['language']): LanguageAction {
return { type: SELECT_LANGUAGE, [PARAM_LANGUAGE]: language };
}

export const SELECT_COMPUTED_LANGUAGE = 'SELECT_COMPUTED_LANGUAGE';
export type ComputedLanguageAction = Readonly<
Action<typeof SELECT_COMPUTED_LANGUAGE>
> & {
computedLanguage: string;
computedLanguage: AppState['computedLanguage'];
};
export function selectComputedLanguage(
computedLanguage: string
computedLanguage: AppState['computedLanguage']
): ComputedLanguageAction {
return {
type: SELECT_COMPUTED_LANGUAGE,
Expand Down
2 changes: 1 addition & 1 deletion src/rest/study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ export function getServersInfos(token: Token): Promise<ServerAbout[]> {
).catch((error) => {
console.error(`Error while fetching the servers infos : ${error}`);
throw error;
});
}) as Promise<ServerAbout[]>;
}
21 changes: 16 additions & 5 deletions src/utils/rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { APP_NAME, getAppName } from './config-params';
import {
APP_NAME,
getAppName,
PARAM_LANGUAGE,
PARAM_THEME,
} from './config-params';
import { store } from '../redux/store';
import ReconnectingWebSocket, { Event } from 'reconnecting-websocket';
import { AppState } from '../redux/reducer';
import { User } from './auth';
import { LanguageParameters } from './language';

export interface ErrorWithStatus extends Error {
status?: number;
Expand Down Expand Up @@ -259,10 +265,15 @@ export function fetchAppsAndUrls(): Promise<MetadataJson[]> {
}

// https://github.com/gridsuite/config-server/blob/main/src/main/java/org/gridsuite/config/server/dto/ParameterInfos.java
export type ConfigParameter = {
name: string;
value: string;
};
export type ConfigParameter =
| {
readonly name: typeof PARAM_LANGUAGE;
value: LanguageParameters;
}
| {
readonly name: typeof PARAM_THEME;
value: string;
};
export type ConfigParameters = ConfigParameter[];
export function fetchConfigParameters(
appName: string = APP_NAME
Expand Down

0 comments on commit 6f03f01

Please sign in to comment.