Skip to content

Commit

Permalink
apply patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Jul 10, 2024
1 parent 12909e6 commit 037d952
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 69 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@gridsuite/commons-ui": "0.61.3",
"@gridsuite/commons-ui": "0.61.3+2",
"@hookform/resolvers": "^3.3.4",
"@mui/icons-material": "^5.15.14",
"@mui/lab": "^5.0.0-alpha.169",
Expand Down
12 changes: 7 additions & 5 deletions src/components/app-top-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import React, {
useEffect,
useState,
} from 'react';
import { LIGHT_THEME, logout, TopBar } from '@gridsuite/commons-ui';
import {
LIGHT_THEME,
logout,
TopBar,
UserManagerState,
} from '@gridsuite/commons-ui';
import Parameters, { useParameterState } from './parameters';
import { APP_NAME, PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
import { useDispatch, useSelector } from 'react-redux';
Expand All @@ -29,10 +34,7 @@ import { AppDispatch } from '../redux/store';

export type AppTopBarProps = {
user?: AppState['user'];
userManager: {
instance: unknown | null;
error: string | null;
};
userManager: UserManagerState;
};
const AppTopBar: FunctionComponent<AppTopBarProps> = (props) => {
const navigate = useNavigate();
Expand Down
4 changes: 2 additions & 2 deletions src/components/app-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
card_error_boundary_en,
card_error_boundary_fr,
CardErrorBoundary,
GsLangUser,
LIGHT_THEME,
login_en,
login_fr,
Expand All @@ -28,7 +29,6 @@ import {
import { IntlProvider } from 'react-intl';
import { BrowserRouter } from 'react-router-dom';
import { Provider, useSelector } from 'react-redux';
import { SupportedLanguages } from '../utils/language';
import messages_en from '../translations/en.json';
import messages_fr from '../translations/fr.json';
import messages_plugins_en from '../plugins/translations/en.json';
Expand Down Expand Up @@ -98,7 +98,7 @@ const getMuiTheme = (theme: string): Theme => {
}
};

const messages: Record<SupportedLanguages, IntlConfig['messages']> = {
const messages: Record<GsLangUser, IntlConfig['messages']> = {
en: {
...messages_en,
...login_en,
Expand Down
3 changes: 2 additions & 1 deletion src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from 'react-router-dom';
import { FormattedMessage } from 'react-intl';
import { Box, Typography } from '@mui/material';
import { UserManager } from 'oidc-client';
import {
AuthenticationRouter,
CardErrorBoundary,
Expand Down Expand Up @@ -140,7 +141,7 @@ const App: FunctionComponent = () => {
})
);

const initialize = useCallback((): Promise<unknown | undefined> => {
const initialize = useCallback((): Promise<UserManager> => {
if (process.env.REACT_APP_USE_AUTHENTICATION === 'true') {
return fetchAuthorizationCodeFlowFeatureFlag().then(
(authorizationCodeFlowEnabled) =>
Expand Down
8 changes: 6 additions & 2 deletions src/redux/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { GsTheme } from '@gridsuite/commons-ui';
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>> & {
theme: string;
theme: GsTheme;
};
export function selectTheme(theme: string): ThemeAction {

export function selectTheme(theme: GsTheme): ThemeAction {
return { type: SELECT_THEME, theme: theme };
}

export const SELECT_LANGUAGE = 'SELECT_LANGUAGE';
export type LanguageAction = Readonly<Action<typeof SELECT_LANGUAGE>> & {
[PARAM_LANGUAGE]: AppState['language'];
};

export function selectLanguage(language: AppState['language']): LanguageAction {
return { type: SELECT_LANGUAGE, [PARAM_LANGUAGE]: language };
}
Expand All @@ -31,6 +34,7 @@ export type ComputedLanguageAction = Readonly<
> & {
computedLanguage: AppState['computedLanguage'];
};

export function selectComputedLanguage(
computedLanguage: AppState['computedLanguage']
): ComputedLanguageAction {
Expand Down
28 changes: 19 additions & 9 deletions src/redux/local-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { DARK_THEME, LANG_SYSTEM } from '@gridsuite/commons-ui';
import {
DARK_THEME,
GsLang,
GsLangUser,
GsTheme,
LANG_SYSTEM,
} from '@gridsuite/commons-ui';
import { getComputedLanguage } from '../utils/language';
import { APP_NAME } from '../utils/config-params';
import { AppState } from './reducer';

const LOCAL_STORAGE_THEME_KEY = (APP_NAME + '_THEME').toUpperCase();
const LOCAL_STORAGE_LANGUAGE_KEY = (APP_NAME + '_LANGUAGE').toUpperCase();

export function getLocalStorageTheme(): string {
return localStorage.getItem(LOCAL_STORAGE_THEME_KEY) || DARK_THEME;
export function getLocalStorageTheme() {
return (
(localStorage.getItem(LOCAL_STORAGE_THEME_KEY) as GsTheme) || DARK_THEME
);
}

export function saveLocalStorageTheme(theme: string): void {
export function saveLocalStorageTheme(theme: GsTheme): void {
localStorage.setItem(LOCAL_STORAGE_THEME_KEY, theme);
}

export function getLocalStorageLanguage(): AppState['language'] {
return localStorage.getItem(LOCAL_STORAGE_LANGUAGE_KEY) || LANG_SYSTEM;
export function getLocalStorageLanguage() {
return (
(localStorage.getItem(LOCAL_STORAGE_LANGUAGE_KEY) as GsLang) ||
LANG_SYSTEM
);
}

export function saveLocalStorageLanguage(language: AppState['language']): void {
export function saveLocalStorageLanguage(language: GsLang): void {
localStorage.setItem(LOCAL_STORAGE_LANGUAGE_KEY, language);
}

export function getLocalStorageComputedLanguage(): AppState['computedLanguage'] {
export function getLocalStorageComputedLanguage(): GsLangUser {
return getComputedLanguage(getLocalStorageLanguage());
}
56 changes: 38 additions & 18 deletions src/redux/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { AnyAction, createReducer, Draft } from '@reduxjs/toolkit';
import { User } from 'oidc-client';
import { createReducer, Draft } from '@reduxjs/toolkit';
import {
getLocalStorageComputedLanguage,
getLocalStorageLanguage,
Expand All @@ -21,26 +20,37 @@ import {
ThemeAction,
} from './actions';
import {
AuthenticationRouterErrorAction,
AuthenticationRouterErrorState,
CommonActions,
CommonStoreState,
GsLang,
GsLangUser,
GsTheme,
LOGOUT_ERROR,
LogoutErrorAction,
RESET_AUTHENTICATION_ROUTER_ERROR,
SHOW_AUTH_INFO_LOGIN,
ShowAuthenticationRouterLoginAction,
SIGNIN_CALLBACK_ERROR,
SignInCallbackErrorAction,
UNAUTHORIZED_USER_INFO,
UnauthorizedUserAction,
USER,
USER_VALIDATION_ERROR,
UserAction,
UserValidationErrorAction,
} from '@gridsuite/commons-ui';
import { PARAM_LANGUAGE, PARAM_THEME } from '../utils/config-params';
import { ReducerWithInitialState } from '@reduxjs/toolkit/dist/createReducer';
import { LanguageParameters, SupportedLanguages } from '../utils/language';

export type AppState = {
computedLanguage: SupportedLanguages;
[PARAM_THEME]: string;
[PARAM_LANGUAGE]: LanguageParameters;
export type AppState = CommonStoreState & {
computedLanguage: GsLangUser;
[PARAM_THEME]: GsTheme;
[PARAM_LANGUAGE]: GsLang;

user: User | null; //TODO use true definition when commons-ui passed to typescript
signInCallbackError: unknown;
authenticationRouterError: unknown;
signInCallbackError: string | null;
authenticationRouterError: AuthenticationRouterErrorState | null;
showAuthenticationRouterLogin: boolean;
};

Expand All @@ -57,7 +67,11 @@ const initialState: AppState = {
computedLanguage: getLocalStorageComputedLanguage(),
};

export type Actions = AnyAction | ThemeAction | LanguageAction | ComputedLanguageAction;
export type Actions =
| CommonActions
| ThemeAction
| LanguageAction
| ComputedLanguageAction;

export type AppStateKey = keyof AppState;

Expand All @@ -72,51 +86,57 @@ export const reducer: ReducerWithInitialState<AppState> = createReducer(
}
);

builder.addCase(USER, (state: Draft<AppState>, action: AnyAction) => {
builder.addCase(USER, (state: Draft<AppState>, action: UserAction) => {
state.user = action.user;
});

builder.addCase(
SIGNIN_CALLBACK_ERROR,
(state: Draft<AppState>, action: AnyAction) => {
(state: Draft<AppState>, action: SignInCallbackErrorAction) => {
state.signInCallbackError = action.signInCallbackError;
}
);

builder.addCase(
UNAUTHORIZED_USER_INFO,
(state: Draft<AppState>, action: AnyAction) => {
(state: Draft<AppState>, action: UnauthorizedUserAction) => {
state.authenticationRouterError =
action.authenticationRouterError;
}
);

builder.addCase(
LOGOUT_ERROR,
(state: Draft<AppState>, action: AnyAction) => {
(state: Draft<AppState>, action: LogoutErrorAction) => {
state.authenticationRouterError =
action.authenticationRouterError;
}
);

builder.addCase(
USER_VALIDATION_ERROR,
(state: Draft<AppState>, action: AnyAction) => {
(state: Draft<AppState>, action: UserValidationErrorAction) => {
state.authenticationRouterError =
action.authenticationRouterError;
}
);

builder.addCase(
RESET_AUTHENTICATION_ROUTER_ERROR,
(state: Draft<AppState>, action: AnyAction) => {
(
state: Draft<AppState>,
action: AuthenticationRouterErrorAction
) => {
state.authenticationRouterError = null;
}
);

builder.addCase(
SHOW_AUTH_INFO_LOGIN,
(state: Draft<AppState>, action: AnyAction) => {
(
state: Draft<AppState>,
action: ShowAuthenticationRouterLoginAction
) => {
state.showAuthenticationRouterLogin =
action.showAuthenticationRouterLogin;
}
Expand Down
13 changes: 3 additions & 10 deletions src/rest/study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 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 { GridSuiteModule } from '@gridsuite/commons-ui';
import { backendFetchJson } from '../utils/rest-api';
import { getErrorMessage } from '../utils/error';

Expand All @@ -13,15 +14,7 @@ const API_URL =
? `${process.env.REACT_APP_API_GATEWAY}/study/v1`
: `${process.env.REACT_APP_SRV_STUDY_URI}/v1`);

//TODO delete when commons-ui will be in typescript
export type ServerAbout = {
type: 'app' | 'server' | 'other';
name: string;
version?: string;
gitTag?: string;
};

export function getServersInfos(): Promise<ServerAbout[]> {
export function getServersInfos() {
return backendFetchJson(`${API_URL}/servers/about?view=yyy`, {
headers: {
Accept: 'application/json',
Expand All @@ -33,5 +26,5 @@ export function getServersInfos(): Promise<ServerAbout[]> {
`Error while fetching the servers infos : ${getErrorMessage(error)}`
);
throw error;
}) as Promise<ServerAbout[]>;
}) as Promise<GridSuiteModule[]>;
}
25 changes: 11 additions & 14 deletions src/utils/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { LANG_ENGLISH, LANG_FRENCH, LANG_SYSTEM } from '@gridsuite/commons-ui';
import {
GsLang,
GsLangUser,
LANG_ENGLISH,
LANG_FRENCH,
LANG_SYSTEM,
} from '@gridsuite/commons-ui';

const supportedLanguages = [LANG_FRENCH, LANG_ENGLISH];
//export type SupportedLanguagesType = typeof supportedLanguages[number]; //TODO when commons-ui in typescript
export type SupportedLanguages = 'en' | 'fr';
//export type LanguageParameters = SupportedLanguages | typeof LANG_SYSTEM; //TODO when commons-ui in typescript
export type LanguageParameters = SupportedLanguages | 'sys';

export function getSystemLanguage(): SupportedLanguages {
export function getSystemLanguage(): GsLangUser {
const systemLanguage = navigator.language.split(/[-_]/)[0];
return supportedLanguages.includes(systemLanguage)
? systemLanguage
? (systemLanguage as GsLangUser)
: LANG_ENGLISH;
}

export function getComputedLanguage(
language: LanguageParameters
): SupportedLanguages {
return language === LANG_SYSTEM
? getSystemLanguage()
: (language as SupportedLanguages);
//TODO remove cast when commons-ui in typescript
export function getComputedLanguage(language: GsLang) {
return language === LANG_SYSTEM ? getSystemLanguage() : language;
}
Loading

0 comments on commit 037d952

Please sign in to comment.