Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: refacto before selecting a PR #10157

Merged
merged 3 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ import { calculateDistanceAlongTrack } from 'applications/editor/tools/utils';
import { useManageTrainScheduleContext } from 'applications/operationalStudies/hooks/useManageTrainScheduleContext';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import { osrdEditoastApi } from 'common/api/osrdEditoastApi';
import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { useOsrdConfSelectors } from 'common/osrdContext';
import { setPointIti } from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/setPointIti';
import type { PathStep } from 'reducers/osrdconf/types';

type FeatureInfoClickType = {
displayPopup: boolean;
coordinates?: number[];
feature?: any;
};
import type { FeatureInfoClick } from '../types';

type RenderPopupProps = {
type AddPathStepPopupProps = {
pathProperties?: ManageTrainSchedulePathProperties;
featureInfoClick: FeatureInfoClick;
resetFeatureInfoClick: () => void;
};

function RenderPopup({ pathProperties }: RenderPopupProps) {
const { getFeatureInfoClick, getInfraID, getOrigin, getDestination } = useOsrdConfSelectors();
function AddPathStepPopup({
pathProperties,
featureInfoClick,
resetFeatureInfoClick,
}: AddPathStepPopupProps) {
const { getInfraID, getOrigin, getDestination } = useOsrdConfSelectors();
const { launchPathfinding } = useManageTrainScheduleContext();
const osrdConfActions = useOsrdConfActions();
const { t } = useTranslation(['operationalStudies/manageTrainSchedule']);
const featureInfoClick: FeatureInfoClickType = useSelector(getFeatureInfoClick);
const infraId = useSelector(getInfraID);
const origin = useSelector(getOrigin);
const destination = useSelector(getDestination);
Expand All @@ -46,13 +46,7 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {

useEffect(() => {
const calculateOffset = async () => {
if (
!featureInfoClick.feature ||
!featureInfoClick.feature.properties ||
!featureInfoClick.coordinates
)
return;
const trackId = featureInfoClick.feature.properties.id;
const trackId = featureInfoClick.feature.properties?.id;
const result = await getTrackEntity({
infraId: infraId!,
objectType: 'TrackSection',
Expand All @@ -73,18 +67,10 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
setTrackOffset(offset);
};

if (featureInfoClick.displayPopup) {
calculateOffset();
}
calculateOffset();
}, [featureInfoClick]);

if (
clarani marked this conversation as resolved.
Show resolved Hide resolved
!featureInfoClick.displayPopup ||
!featureInfoClick.feature ||
!featureInfoClick.feature.properties ||
!featureInfoClick.coordinates
)
return null;
if (!featureInfoClick.feature.properties) return null;

const { properties: trackProperties } = featureInfoClick.feature;
const coordinates = featureInfoClick.coordinates.slice(0, 2);
Expand Down Expand Up @@ -127,7 +113,7 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
className="btn btn-sm btn-success"
type="button"
onClick={() =>
setPointIti('origin', pathStepProperties, osrdConfActions, launchPathfinding)
setPointIti('origin', pathStepProperties, launchPathfinding, resetFeatureInfoClick)
}
>
<RiMapPin2Fill />
Expand All @@ -141,8 +127,8 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
setPointIti(
'via',
pathStepProperties,
osrdConfActions,
launchPathfinding,
resetFeatureInfoClick,
pathProperties
)
}
Expand All @@ -156,7 +142,7 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
className="btn btn-sm btn-warning"
type="button"
onClick={() =>
setPointIti('destination', pathStepProperties, osrdConfActions, launchPathfinding)
setPointIti('destination', pathStepProperties, launchPathfinding, resetFeatureInfoClick)
}
>
<IoFlag />
Expand All @@ -167,4 +153,4 @@ function RenderPopup({ pathProperties }: RenderPopupProps) {
);
}

export default React.memo(RenderPopup);
export default React.memo(AddPathStepPopup);
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
/* eslint-disable import/prefer-default-export */
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import { insertViaFromMap } from 'reducers/osrdconf/helpers';
import type { ConfSliceActions } from 'reducers/osrdconf/osrdConfCommon';
import type { PathStep } from 'reducers/osrdconf/types';
import { store } from 'store';
import { addElementAtIndex, replaceElementAtIndex } from 'utils/array';

export function setPointIti(
pointType: 'origin' | 'destination' | 'via',
pathStep: PathStep,
actions: ConfSliceActions,
launchPathfinding: (newPathSteps: (PathStep | null)[]) => void,
resetFeatureInfoClick: () => void,
pathProperties?: ManageTrainSchedulePathProperties
) {
const { updateFeatureInfoClick } = actions;
const { pathSteps } = store.getState().operationalStudiesConf;

let newPathSteps: (PathStep | null)[];
Expand All @@ -32,6 +30,6 @@ export function setPointIti(
newPathSteps = addElementAtIndex(pathSteps, pathSteps.length - 1, pathStep);
}
}
store.dispatch(updateFeatureInfoClick({ displayPopup: false }));
resetFeatureInfoClick();
launchPathfinding(newPathSteps);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ import TracksGeographic from 'common/Map/Layers/TracksGeographic';
import TracksOSM from 'common/Map/Layers/TracksOSM';
import { removeSearchItemMarkersOnMap } from 'common/Map/utils';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { useInfraID, useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
import { useInfraID } from 'common/osrdContext';
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import VirtualLayers from 'modules/simulationResult/components/SimulationResultsMap/VirtualLayers';
import RenderPopup from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/RenderPopup';
import AddPathStepPopup from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/AddPathStepPopup';
import { updateViewport } from 'reducers/map';
import type { Viewport } from 'reducers/map';
import { getMap, getTerrain3DExaggeration } from 'reducers/map/selectors';
Expand All @@ -55,6 +55,7 @@ import ItineraryLayer from './ManageTrainScheduleMap/ItineraryLayer';
import ItineraryMarkers, {
type MarkerInformation,
} from './ManageTrainScheduleMap/ItineraryMarkers';
import type { FeatureInfoClick } from './types';

type MapProps = {
pathProperties?: ManageTrainSchedulePathProperties;
Expand Down Expand Up @@ -118,19 +119,15 @@ const Map = ({
bottom: 20,
};

const { getFeatureInfoClick } = useOsrdConfSelectors();
const featureInfoClick = useSelector(getFeatureInfoClick);
const [featureInfoClick, setFeatureInfoClick] = useState<FeatureInfoClick>();

const { updateFeatureInfoClick } = useOsrdConfActions();
const resetFeatureInfoClick = useCallback(() => {
setFeatureInfoClick(undefined);
}, []);

const closeFeatureInfoClickPopup = useCallback(() => {
if (featureInfoClick.displayPopup) {
dispatch(
updateFeatureInfoClick({
displayPopup: false,
feature: undefined,
})
);
if (featureInfoClick) {
setFeatureInfoClick(undefined);
}
}, [featureInfoClick]);

Expand All @@ -151,20 +148,12 @@ const Map = ({
result.feature.properties.id &&
result.feature.geometry.type === 'LineString'
) {
dispatch(
updateFeatureInfoClick({
displayPopup: true,
feature: result.feature,
coordinates: result.nearest,
})
);
setFeatureInfoClick({
feature: result.feature,
coordinates: result.nearest,
});
} else {
dispatch(
updateFeatureInfoClick({
displayPopup: false,
feature: undefined,
})
);
setFeatureInfoClick(undefined);
}
removeSearchItemMarkersOnMap(dispatch);
};
Expand Down Expand Up @@ -391,7 +380,13 @@ const Map = ({
layerOrder={LAYER_GROUPS_ORDER[LAYERS.LINE_SEARCH.GROUP]}
infraID={infraID}
/>
{!showStdcmAssets && <RenderPopup pathProperties={pathProperties} />}
{!showStdcmAssets && featureInfoClick && (
<AddPathStepPopup
pathProperties={pathProperties}
featureInfoClick={featureInfoClick}
resetFeatureInfoClick={resetFeatureInfoClick}
/>
)}
</>
)}
<ItineraryLayer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Position } from 'geojson';
import type { Feature, Position } from 'geojson';

import type { ReceptionSignal, TrainScheduleBase } from 'common/api/osrdEditoastApi';
import type { IsoDurationString } from 'common/types';
Expand Down Expand Up @@ -68,3 +68,5 @@ export type ValidConfig = {
firstStartTime: string;
speedLimitByTag?: string;
};

export type FeatureInfoClick = { feature: Feature; coordinates: number[] };
1 change: 0 additions & 1 deletion front/src/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ const buildOsrdConfPersistConfig = <T extends OsrdConfState>(
key: slice.name,
storage,
transforms: [stdcmPathStepsDateTransform, operationalStudiesDateTransform],
blacklist: ['featureInfoClick'],
});

export const persistConfig = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@
import type { Feature } from 'geojson';

import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import type { OsrdConfState, PathStep } from 'reducers/osrdconf/types';
import type { PathStep } from 'reducers/osrdconf/types';

export default function commonConfBuilder() {
return {
buildFeatureInfoClick: (
featureInfoClickFields?: Partial<OsrdConfState['featureInfoClick']>
): OsrdConfState['featureInfoClick'] => ({
displayPopup: true,
feature: {
type: 'Feature',
_geometry: {
type: 'LineString',
coordinates: [12, 45],
},
properties: {
title: 'test',
toto: 'toto',
},
id: 'test',
_vectorTileFeature: {
id: 10,
type: 1,
extent: 15,
properties: {
name: 'test',
},
},
} as unknown as Feature,
...featureInfoClickFields,
}),

buildPathSteps: (): PathStep[] => [
{
uic: 474007,
Expand Down
10 changes: 0 additions & 10 deletions front/src/reducers/osrdconf/osrdConfCommon/__tests__/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { omit } from 'lodash';
import { describe, beforeEach, it, expect } from 'vitest';

import { StdcmStopTypes } from 'applications/stdcm/types';
Expand Down Expand Up @@ -207,15 +206,6 @@ const testCommonConfReducers = (slice: OperationalStudiesConfSlice | StdcmConfSl
expect(state.pathSteps[1]?.stopType).toEqual('serviceStop');
});

it('should handle updateFeatureInfoClick', () => {
const newFeatureClick = testDataBuilder.buildFeatureInfoClick();
defaultStore.dispatch(slice.actions.updateFeatureInfoClick(newFeatureClick));
const state = defaultStore.getState()[slice.name];
const feature = omit(newFeatureClick.feature, ['_vectorTileFeature']);
const expected = { ...newFeatureClick, feature };
expect(state.featureInfoClick).toStrictEqual(expected);
});

it('should handle updateGridMarginBefore', () => {
const newGridMarginBefore = 5;
defaultStore.dispatch(slice.actions.updateGridMarginBefore(newGridMarginBefore));
Expand Down
7 changes: 0 additions & 7 deletions front/src/reducers/osrdconf/osrdConfCommon/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { CaseReducer, PayloadAction } from '@reduxjs/toolkit';
import type { Draft } from 'immer';
import { omit } from 'lodash';

import { type StdcmStopTypes } from 'applications/stdcm/types';
import { type InfraStateReducers, buildInfraStateReducers, infraState } from 'reducers/infra';
Expand Down Expand Up @@ -33,7 +32,6 @@ export const defaultCommonConf: OsrdConfState = {
gridMarginBefore: undefined,
gridMarginAfter: undefined,
...infraState,
featureInfoClick: { displayPopup: false },
// Corresponds to origin and destination not defined
pathSteps: [null, null],
rollingStockComfort: 'STANDARD' as const,
Expand Down Expand Up @@ -62,7 +60,6 @@ interface CommonConfReducers<S extends OsrdConfState> extends InfraStateReducers
>;
['updateGridMarginBefore']: CaseReducer<S, PayloadAction<S['gridMarginBefore']>>;
['updateGridMarginAfter']: CaseReducer<S, PayloadAction<S['gridMarginAfter']>>;
['updateFeatureInfoClick']: CaseReducer<S, PayloadAction<S['featureInfoClick']>>;
['updatePathSteps']: CaseReducer<S, PayloadAction<S['pathSteps']>>;
['replaceItinerary']: CaseReducer<S, PayloadAction<S['pathSteps']>>;
['deleteItinerary']: CaseReducer<S>;
Expand Down Expand Up @@ -146,10 +143,6 @@ export function buildCommonConfReducers<S extends OsrdConfState>(): CommonConfRe
updateGridMarginAfter(state: Draft<S>, action: PayloadAction<S['gridMarginAfter']>) {
state.gridMarginAfter = action.payload;
},
updateFeatureInfoClick(state: Draft<S>, action: PayloadAction<S['featureInfoClick']>) {
const feature = omit(action.payload.feature, ['_vectorTileFeature']);
state.featureInfoClick = { ...action.payload, feature };
},
// update path steps without changing the itinerary (only add vias on the existing pathfinding,
// add schedules, margins or power restrictions)
updatePathSteps(state: Draft<S>, action: PayloadAction<S['pathSteps']>) {
Expand Down
1 change: 0 additions & 1 deletion front/src/reducers/osrdconf/osrdConfCommon/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const buildCommonConfSelectors = <ConfState extends OsrdConfState>(
getGridMarginBefore: makeOsrdConfSelector('gridMarginBefore'),
getGridMarginAfter: makeOsrdConfSelector('gridMarginAfter'),
getPowerRestriction: makeOsrdConfSelector('powerRestriction'),
getFeatureInfoClick: makeOsrdConfSelector('featureInfoClick'),
getPathSteps,
getOrigin: (state: RootState) => {
const pathSteps = getPathSteps(state);
Expand Down
3 changes: 1 addition & 2 deletions front/src/reducers/osrdconf/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Feature, Position } from 'geojson';
import type { Position } from 'geojson';

import type { PowerRestriction } from 'applications/operationalStudies/types';
import type {
Expand Down Expand Up @@ -39,7 +39,6 @@ export interface OsrdConfState extends InfraState {
initialSpeed?: number;
gridMarginBefore?: number;
gridMarginAfter?: number;
featureInfoClick: { displayPopup: boolean; feature?: Feature; coordinates?: number[] };
pathSteps: (PathStep | null)[];
rollingStockComfort: Comfort;
startTime: Date;
Expand Down
Loading