Skip to content

Commit

Permalink
migrate api service & dedup utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Jul 26, 2024
1 parent e0b41ac commit e979bbf
Show file tree
Hide file tree
Showing 17 changed files with 324 additions and 503 deletions.
8 changes: 4 additions & 4 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ REACT_APP_NAME=GridDynaApp

EXTEND_ESLINT=true

REACT_APP_API_PREFIX=api
REACT_APP_GATEWAY_PREFIX=/gateway
REACT_APP_URI=/dynamic-mapping-server
REACT_APP_WS_GATEWAY=ws/gateway
REACT_APP_STUDY_URI=/study-server
REACT_APP_API_GATEWAY=api/gateway

REACT_APP_API_PREFIX=api/gateway
REACT_APP_DYNAMAP_SVC=dynamic-mapping
8 changes: 4 additions & 4 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ REACT_APP_USE_AUTHENTICATION=false

REACT_APP_NAME=GridDynaApp

REACT_APP_API_PREFIX=api
REACT_APP_GATEWAY_PREFIX=/gateway
REACT_APP_URI=/dynamic-mapping-server
REACT_APP_WS_GATEWAY=ws/gateway
REACT_APP_STUDY_URI=/study-server
REACT_APP_API_GATEWAY=api/gateway

REACT_APP_API_PREFIX=api
REACT_APP_DYNAMAP_SVC=dynamic-mapping-server
50 changes: 32 additions & 18 deletions src/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,14 @@ import {
import { FormattedMessage } from 'react-intl';
import { ReactComponent as PowsyblLogo } from '../images/powsybl_logo.svg';
import AppPackage from '../../package.json';
import {
fetchAppsAndUrls,
fetchIdpSettings,
fetchValidateUser,
fetchVersion,
} from '../utils/rest-api';
import { getServersInfos } from '../rest/studyAPI';
import { UserSlice } from '../redux/slices/User';
import RootContainer from '../containers/RootContainer';
import {
appLocalSrv,
appsMetadataSrv,
studySrv,
userAdminSrv,
} from '../services';

const lightTheme = createTheme({
palette: {
Expand Down Expand Up @@ -87,13 +86,23 @@ const App = () => {

const navigate = useNavigate();

const onLogoClick = useCallback(
() => navigate('/', { replace: true }),
[navigate]
);

const dispatch = useDispatch();

const authenticationDispatch = useCallback(
(action) => dispatch(UserSlice.actions[action.type](action)),
[dispatch]
);

const onLogoutClick = useCallback(
() => logout(authenticationDispatch, userManager.instance),
[authenticationDispatch, userManager.instance]
);

const location = useLocation();

// Can't use lazy initializer because useMatch is a hook
Expand Down Expand Up @@ -121,8 +130,8 @@ const App = () => {
? initializeAuthenticationProd(
authenticationDispatch,
initialMatchSilentRenewCallbackUrl != null,
fetchIdpSettings,
fetchValidateUser,
appLocalSrv.fetchIdpSettings,
userAdminSrv.fetchValidateUser,
initialMatchSigninCallbackUrl != null
)
: initializeAuthenticationDev(
Expand All @@ -148,12 +157,21 @@ const App = () => {

useEffect(() => {
if (user !== null) {
fetchAppsAndUrls().then((res) => {
appsMetadataSrv.fetchAppsMetadata().then((res) => {
setAppsAndUrls(res);
});
}
}, [user]);

const additionalModulesFetcher = useCallback(
() => studySrv.getServersInfos('dyna'),
[]
);
const globalVersionFetcher = useCallback(
() => appsMetadataSrv.fetchVersion().then((res) => res?.deployVersion),
[]
);

return (
<StyledEngineProvider injectFirst>
<ThemeProvider theme={getMuiTheme(theme)}>
Expand All @@ -165,16 +183,12 @@ const App = () => {
appLogo={<PowsyblLogo />}
appVersion={AppPackage.version}
appLicense={AppPackage.license}
onLogoClick={() => navigate('/', { replace: true })}
onLogoutClick={() =>
logout(authenticationDispatch, userManager.instance)
}
onLogoClick={onLogoClick}
onLogoutClick={onLogoutClick}
user={user}
appsAndUrls={appsAndUrls}
globalVersionPromise={() =>
fetchVersion().then((res) => res?.deployVersion)
}
additionalModulesPromise={getServersInfos}
globalVersionPromise={globalVersionFetcher}
additionalModulesPromise={additionalModulesFetcher}
/>
<CardErrorBoundary>
{user !== null ? (
Expand Down
10 changes: 10 additions & 0 deletions src/react-app-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,13 @@
*/

/// <reference types="react-scripts" />

namespace NodeJS {
interface ProcessEnv {
REACT_APP_API_GATEWAY: string;
REACT_APP_WS_GATEWAY: string;

REACT_APP_API_PREFIX: string;
REACT_APP_DYNAMAP_SVC: string;
}
}
23 changes: 15 additions & 8 deletions src/redux/slices/Mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import {
createSelector,
createSlice,
} from '@reduxjs/toolkit';
import * as mappingsAPI from '../../rest/mappingsAPI';
import { cloneDeep, isEmpty, isEqual } from 'lodash';
import RequestStatus from '../../constants/RequestStatus';
import * as networkAPI from '../../rest/networkAPI';
import { AutomatonFamily } from '../../constants/automatonDefinition';
import { RuleEquipmentTypes } from '../../constants/equipmentType';
import {
Expand All @@ -27,6 +25,7 @@ import {
import { v4 as uuid4 } from 'uuid';
import { enrichIdRqbQuery } from '../../utils/rqb-utils';
import { assignArray } from '../../utils/functions';
import { dynamicMappingSrv } from '../../services';

const initialState = {
mappings: [],
Expand Down Expand Up @@ -555,7 +554,7 @@ export const postMapping = createAsyncThunk(
)?.controlledParameters
: state?.mappings.controlledParameters;

return await mappingsAPI.postMapping(
return await dynamicMappingSrv.postMapping(
mappingName,
augmentedRules,
formattedAutomata,
Expand All @@ -569,31 +568,39 @@ export const getMappings = createAsyncThunk(
'mappings/get',
async (_arg, { getState }) => {
const token = getState()?.user.user?.id_token;
return await mappingsAPI.getMappings(token);
return await dynamicMappingSrv.getMappings(token);
}
);

export const deleteMapping = createAsyncThunk(
'mappings/delete',
async (mappingName, { getState }) => {
const token = getState()?.user.user?.id_token;
return await mappingsAPI.deleteMapping(mappingName, token);
return await dynamicMappingSrv.deleteMapping(mappingName, token);
}
);

export const renameMapping = createAsyncThunk(
'mappings/rename',
async ({ nameToReplace, newName }, { getState }) => {
const token = getState()?.user.user?.id_token;
return await mappingsAPI.renameMapping(nameToReplace, newName, token);
return await dynamicMappingSrv.renameMapping(
nameToReplace,
newName,
token
);
}
);

export const copyMapping = createAsyncThunk(
'mappings/copy',
async ({ originalName, copyName }, { getState }) => {
const token = getState()?.user.user?.id_token;
return await mappingsAPI.copyMapping(originalName, copyName, token);
return await dynamicMappingSrv.copyMapping(
originalName,
copyName,
token
);
}
);

Expand All @@ -610,7 +617,7 @@ export const getNetworkMatchesFromRule = createAsyncThunk(
equipmentType: foundRule.type,
filter: augmentFilter(foundRule.filter, foundRule.type),
};
return await networkAPI.getNetworkMatchesFromRule(
return await dynamicMappingSrv.getNetworkMatchesFromRule(
networkId,
ruleToMatch,
token
Expand Down
18 changes: 11 additions & 7 deletions src/redux/slices/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
createSelector,
createSlice,
} from '@reduxjs/toolkit';
import * as modelsAPI from '../../rest/modelsAPI';
import RequestStatus from '../../constants/RequestStatus';
import { cloneDeep, findIndex, forEach, uniqBy } from 'lodash';
import { SetType } from '../../constants/models';
import { dynamicMappingSrv } from '../../services';

const DEFAULT_GROUP = {
name: '',
Expand Down Expand Up @@ -74,15 +74,15 @@ export const getModels = createAsyncThunk(
'models/get',
async (_arg, { getState }) => {
const token = getState()?.user.user?.id_token;
return await modelsAPI.getModels(token);
return await dynamicMappingSrv.getModels(token);
}
);

export const getModelDefinitions = createAsyncThunk(
'models/definitions',
async (modelName, { getState }) => {
const token = getState()?.user.user?.id_token;
return await modelsAPI.getModelDefinitions(modelName, token);
return await dynamicMappingSrv.getModelDefinitions(modelName, token);
}
);

Expand All @@ -91,7 +91,7 @@ export const getModelSets = createAsyncThunk(
async ({ modelName, groupName, groupType }, { getState }) => {
if (groupName) {
const token = getState()?.user.user?.id_token;
return await modelsAPI.getModelSets(
return await dynamicMappingSrv.getModelSets(
modelName,
groupName,
groupType !== '' ? groupType : SetType.FIXED,
Expand All @@ -108,7 +108,7 @@ export const getSearchedModelSets = createAsyncThunk(
async ({ modelName, groupName, groupType }, { getState }) => {
if (groupName) {
const token = getState()?.user.user?.id_token;
return await modelsAPI.getModelSets(
return await dynamicMappingSrv.getModelSets(
modelName,
groupName,
groupType ?? '',
Expand All @@ -125,15 +125,19 @@ export const postModelSetsGroup = createAsyncThunk(
async (strict, { getState }) => {
const token = getState()?.user.user?.id_token;
const setGroup = getState()?.models.currentGroup;
return await modelsAPI.postModelSetsGroup(setGroup, strict, token);
return await dynamicMappingSrv.postModelSetsGroup(
setGroup,
strict,
token
);
}
);

export const getAutomatonDefinitions = createAsyncThunk(
'models/automaton/get/definitions',
async (_arg, { getState }) => {
const token = getState()?.user.user?.id_token;
return await modelsAPI.getAutomatonDefinitions(token);
return await dynamicMappingSrv.getAutomatonDefinitions(token);
}
);

Expand Down
11 changes: 7 additions & 4 deletions src/redux/slices/Network.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
createSlice,
} from '@reduxjs/toolkit';
import RequestStatus from '../../constants/RequestStatus';
import * as networkAPI from '../../rest/networkAPI';
import { createParameterSelector } from '../selectorUtil';
import { PropertyType } from '../../constants/equipmentType';
import { dynamicMappingSrv } from '../../services';

const initialState = {
propertyValues: [],
Expand Down Expand Up @@ -73,23 +73,26 @@ export const getPropertyValuesFromFile = createAsyncThunk(
'network/getValuesFromFile',
async (file, { getState }) => {
const token = getState()?.user.user?.id_token;
return await networkAPI.getPropertyValuesFromFile(file, token);
return await dynamicMappingSrv.getPropertyValuesFromFile(file, token);
}
);

export const getPropertyValuesFromNetworkId = createAsyncThunk(
'network/getValuesFromId',
async (networkId, { getState }) => {
const token = getState()?.user.user?.id_token;
return await networkAPI.getPropertyValuesFromId(networkId, token);
return await dynamicMappingSrv.getPropertyValuesFromId(
networkId,
token
);
}
);

export const getNetworkNames = createAsyncThunk(
'network/getNetworks',
async (_args, { getState }) => {
const token = getState()?.user.user?.id_token;
return await networkAPI.getNetworksName(token);
return await dynamicMappingSrv.getNetworksName(token);
}
);

Expand Down
8 changes: 4 additions & 4 deletions src/redux/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { reducer } from './reducer';

export const store = configureStore({ reducer });
export type AppDispatch = typeof store.dispatch;
initCommonServices(
process.env.REACT_APP_NAME!,
() => store.getState().user?.user
);

export function getUser() {
return store.getState().user?.user;
}
Loading

0 comments on commit e979bbf

Please sign in to comment.