From 6115d6a44bde2c1dc470bc0805117b2314832093 Mon Sep 17 00:00:00 2001
From: Clara Ni <clara.ni@outlook.fr>
Date: Mon, 2 Dec 2024 11:55:25 +0100
Subject: [PATCH] fixup! front: reorganize actions related to pathSteps

---
 .../components/Itinerary/ModalSuggestedVias.tsx      | 12 +++---------
 front/src/reducers/osrdconf/osrdConfCommon/index.ts  |  7 ++++---
 2 files changed, 7 insertions(+), 12 deletions(-)

diff --git a/front/src/modules/pathfinding/components/Itinerary/ModalSuggestedVias.tsx b/front/src/modules/pathfinding/components/Itinerary/ModalSuggestedVias.tsx
index c4a864a8735..bf2b89955f9 100644
--- a/front/src/modules/pathfinding/components/Itinerary/ModalSuggestedVias.tsx
+++ b/front/src/modules/pathfinding/components/Itinerary/ModalSuggestedVias.tsx
@@ -9,7 +9,7 @@ import ModalBodySNCF from 'common/BootstrapSNCF/ModalSNCF/ModalBodySNCF';
 import ModalFooterSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalFooterSNCF';
 import ModalHeaderSNCF from 'common/BootstrapSNCF/ModalSNCF/ModalHeaderSNCF';
 import { useOsrdConfActions, useOsrdConfSelectors } from 'common/osrdContext';
-import { isVia, matchPathStepAndOp } from 'modules/pathfinding/utils';
+import { isVia } from 'modules/pathfinding/utils';
 import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
 import { useAppDispatch } from 'store';
 import { formatUicToCi } from 'utils/strings';
@@ -20,11 +20,10 @@ type ModalSuggestedViasProps = {
 
 const ModalSuggestedVias = ({ suggestedVias }: ModalSuggestedViasProps) => {
   const { upsertViaFromSuggestedOP, clearVias, removeVia } = useOsrdConfActions();
-  const { getVias, getDestination, getPathSteps } = useOsrdConfSelectors();
+  const { getVias, getDestination } = useOsrdConfSelectors();
   const dispatch = useAppDispatch();
   const vias = useSelector(getVias());
   const destination = useSelector(getDestination);
-  const pathSteps = useSelector(getPathSteps);
   const { t } = useTranslation('operationalStudies/manageTrainSchedule');
 
   const isOriginOrDestination = useCallback(
@@ -33,12 +32,7 @@ const ModalSuggestedVias = ({ suggestedVias }: ModalSuggestedViasProps) => {
     [destination]
   );
 
-  const removeViaFromPath = (op: SuggestedOP) => {
-    const pathStepToRemove = pathSteps.find((step) => step && matchPathStepAndOp(step, op));
-    if (pathStepToRemove) {
-      dispatch(removeVia(pathStepToRemove.id));
-    }
-  };
+  const removeViaFromPath = (op: SuggestedOP) => dispatch(removeVia(op));
 
   const formatOP = (op: SuggestedOP, idx: number, idxTrueVia: number) => {
     const isInVias = isVia(vias, op);
diff --git a/front/src/reducers/osrdconf/osrdConfCommon/index.ts b/front/src/reducers/osrdconf/osrdConfCommon/index.ts
index 43052873bb0..fbd803a6eba 100644
--- a/front/src/reducers/osrdconf/osrdConfCommon/index.ts
+++ b/front/src/reducers/osrdconf/osrdConfCommon/index.ts
@@ -22,6 +22,7 @@ import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
 import type { OsrdConfState, PathStep } from 'reducers/osrdconf/types';
 import { removeElementAtIndex } from 'utils/array';
 import type { ArrayElement } from 'utils/types';
+import { matchPathStepAndOp } from 'modules/pathfinding/utils';
 
 export const defaultCommonConf: OsrdConfState = {
   constraintDistribution: 'MARECO',
@@ -87,7 +88,7 @@ interface CommonConfReducers<S extends OsrdConfState> extends InfraStateReducers
     reducer: CaseReducer<S, PayloadAction<S['pathSteps']>>;
     prepare: PrepareAction<S['pathSteps']>;
   };
-  ['removeVia']: CaseReducer<S, PayloadAction<string>>;
+  ['removeVia']: CaseReducer<S, PayloadAction<SuggestedOP>>;
   ['upsertViaFromSuggestedOP']: CaseReducer<S, PayloadAction<SuggestedOP>>;
   ['upsertSeveralViasFromSuggestedOP']: CaseReducer<S, PayloadAction<SuggestedOP[]>>;
   ['updateRollingStockComfort']: CaseReducer<S, PayloadAction<S['rollingStockComfort']>>;
@@ -198,9 +199,9 @@ export function buildCommonConfReducers<S extends OsrdConfState>(): CommonConfRe
       state.powerRestriction = [];
     },
     // Use this action in the suggested ops list, not the via list
-    removeVia(state: Draft<S>, action: PayloadAction<string>) {
+    removeVia(state: Draft<S>, action: PayloadAction<SuggestedOP>) {
       // Index takes count of the origin in the array
-      state.pathSteps = state.pathSteps.filter((step) => !step || step.id !== action.payload);
+      state.pathSteps = state.pathSteps.filter((step) => !step || matchPathStepAndOp(step, action.payload));
       state.powerRestriction = [];
     },
     // Use this action in the via list, not the suggested op list