Skip to content

Commit e7a66a9

Browse files
committed
fixup! front: create type StdcmPathStep
1 parent ede9a59 commit e7a66a9

File tree

8 files changed

+57
-32
lines changed

8 files changed

+57
-32
lines changed

front/src/applications/stdcm/components/StdcmForm/StdcmConfig.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,14 @@ const StdcmConfig = ({
9494
};
9595

9696
const removeOriginArrivalTime = () => {
97-
dispatch(updateStdcmPathStep({ ...origin, arrivalType: ArrivalTimeTypes.ASAP }));
97+
dispatch(
98+
updateStdcmPathStep({ id: origin.id, updates: { arrivalType: ArrivalTimeTypes.ASAP } })
99+
);
98100
};
99101
const removeDestinationArrivalTime = () => {
100-
dispatch(updateStdcmPathStep({ ...destination, arrivalType: ArrivalTimeTypes.ASAP }));
102+
dispatch(
103+
updateStdcmPathStep({ id: destination.id, updates: { arrivalType: ArrivalTimeTypes.ASAP } })
104+
);
101105
};
102106

103107
useEffect(() => {

front/src/applications/stdcm/components/StdcmForm/StdcmDestination.tsx

+9-5
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => {
4040
);
4141

4242
const onArrivalChange = (schedule: ScheduleConstraint) => {
43-
const newPathStep = { ...destination, arrival: generateISODateFromDateTime(schedule) };
44-
dispatch(updateStdcmPathStep(newPathStep));
43+
dispatch(
44+
updateStdcmPathStep({
45+
id: destination.id,
46+
updates: { arrival: generateISODateFromDateTime(schedule) },
47+
})
48+
);
4549
};
4650

4751
const onArrivalTypeChange = (arrivalType: ArrivalTimeTypes) => {
48-
dispatch(updateStdcmPathStep({ ...destination, arrivalType }));
52+
dispatch(updateStdcmPathStep({ id: destination.id, updates: { arrivalType } }));
4953
};
5054

5155
const onToleranceChange = ({
@@ -57,8 +61,8 @@ const StdcmDestination = ({ disabled = false }: StdcmConfigCardProps) => {
5761
}) => {
5862
dispatch(
5963
updateStdcmPathStep({
60-
...destination,
61-
tolerances: { before: toleranceBefore, after: toleranceAfter },
64+
id: destination.id,
65+
updates: { tolerances: { before: toleranceBefore, after: toleranceAfter } },
6266
})
6367
);
6468
};

front/src/applications/stdcm/components/StdcmForm/StdcmOperationalPoint.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const StdcmOperationalPoint = ({ point, opPointId, disabled }: StdcmOperationalP
8484
coordinates: p.geographic.coordinates,
8585
}
8686
: { name: undefined, ch: undefined, uic: -1, coordinates: undefined };
87-
dispatch(updateStdcmPathStep({ ...point, ...newPoint }));
87+
dispatch(updateStdcmPathStep({ id: point.id, updates: newPoint }));
8888
};
8989

9090
const updateSelectedPoint = (

front/src/applications/stdcm/components/StdcmForm/StdcmOrigin.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,21 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => {
3737
);
3838

3939
const onOriginArrivalChange = (schedule: ScheduleConstraint) => {
40-
const newPathStep = { ...origin, arrival: generateISODateFromDateTime(schedule) };
41-
dispatch(updateStdcmPathStep(newPathStep));
40+
dispatch(
41+
updateStdcmPathStep({
42+
id: origin.id,
43+
updates: { arrival: generateISODateFromDateTime(schedule) },
44+
})
45+
);
4246
};
4347

4448
const onOriginArrivalTypeChange = (arrivalType: ArrivalTimeTypes) => {
45-
dispatch(updateStdcmPathStep({ ...origin, arrivalType }));
49+
dispatch(
50+
updateStdcmPathStep({
51+
id: origin.id,
52+
updates: { arrivalType },
53+
})
54+
);
4655
};
4756

4857
const onOriginToleranceChange = ({
@@ -54,8 +63,8 @@ const StdcmOrigin = ({ disabled = false }: StdcmConfigCardProps) => {
5463
}) => {
5564
dispatch(
5665
updateStdcmPathStep({
57-
...origin,
58-
tolerances: { before: toleranceBefore, after: toleranceAfter },
66+
id: origin.id,
67+
updates: { tolerances: { before: toleranceBefore, after: toleranceAfter } },
5968
})
6069
);
6170
};

front/src/applications/stdcm/components/StdcmForm/StdcmVias.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@ const StdcmVias = ({ disabled = false }: StdcmConfigCardProps) => {
3333
const updateStopType = (newStopType: StdcmStopTypes, pathStep: StdcmPathStep) => {
3434
const defaultStopTime =
3535
newStopType === StdcmStopTypes.DRIVER_SWITCH ? formatDurationAsISO8601(3 * 60) : '';
36-
const newPathStep = { ...pathStep, stopType: newStopType, stopFor: defaultStopTime };
37-
dispatch(updateStdcmPathStep(newPathStep));
36+
dispatch(
37+
updateStdcmPathStep({
38+
id: pathStep.id,
39+
updates: { stopType: newStopType, stopFor: defaultStopTime },
40+
})
41+
);
3842
};
3943

4044
const updateStopDuration = (stopTime: string, pathStep: StdcmPathStep) => {
4145
dispatch(
42-
updateStdcmPathStep({ ...pathStep, stopFor: formatDurationAsISO8601(Number(stopTime) * 60) })
46+
updateStdcmPathStep({
47+
id: pathStep.id,
48+
updates: { stopFor: formatDurationAsISO8601(Number(stopTime) * 60) },
49+
})
4350
);
4451
};
4552

front/src/reducers/osrdconf/stdcmConf/index.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import nextId from 'react-id-generator';
44

55
import { ArrivalTimeTypes, StdcmStopTypes } from 'applications/stdcm/types';
66
import { defaultCommonConf, buildCommonConfReducers } from 'reducers/osrdconf/osrdConfCommon';
7-
import type { OsrdStdcmConfState } from 'reducers/osrdconf/types';
7+
import type { OsrdStdcmConfState, StdcmPathStep } from 'reducers/osrdconf/types';
88
import { addElementAtIndex } from 'utils/array';
99
import type { ArrayElement } from 'utils/types';
1010

@@ -94,10 +94,15 @@ export const stdcmConfSlice = createSlice({
9494
},
9595
updateStdcmPathStep(
9696
state: Draft<OsrdStdcmConfState>,
97-
action: PayloadAction<ArrayElement<OsrdStdcmConfState['stdcmPathSteps']>>
97+
action: PayloadAction<{
98+
id: string;
99+
updates: Partial<ArrayElement<OsrdStdcmConfState['stdcmPathSteps']>>;
100+
}>
98101
) {
99102
const newPathSteps = state.stdcmPathSteps.map((pathStep) =>
100-
pathStep.id === action.payload.id ? action.payload : pathStep
103+
pathStep.id === action.payload.id
104+
? ({ ...pathStep, ...action.payload.updates } as StdcmPathStep)
105+
: pathStep
101106
);
102107
state.stdcmPathSteps = newPathSteps;
103108
},

front/src/reducers/osrdconf/stdcmConf/stdcmConfReducers.spec.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ describe('stdcmConfReducers', () => {
138138
it('should handle origin update', () => {
139139
const origin = store.getState()[stdcmConfSlice.name].stdcmPathSteps.at(0)!;
140140
expect(origin.isVia).toBe(false);
141-
const newOrigin = {
142-
...origin,
141+
const updates = {
143142
arrivalType: ArrivalTimeTypes.ASAP,
144143
arrival: '2024-08-12T15:45:00.000+02:00',
145144
tolerances: {
@@ -148,30 +147,28 @@ describe('stdcmConfReducers', () => {
148147
},
149148
};
150149

151-
store.dispatch(stdcmConfSliceActions.updateStdcmPathStep(newOrigin));
150+
store.dispatch(stdcmConfSliceActions.updateStdcmPathStep({ id: origin.id, updates }));
152151
const state = store.getState()[stdcmConfSlice.name];
153-
expect(state.stdcmPathSteps.at(0)).toEqual(newOrigin);
152+
expect(state.stdcmPathSteps.at(0)).toEqual({ ...origin, ...updates });
154153
});
155154

156155
it('should handle via update', () => {
157156
const via = store.getState()[stdcmConfSlice.name].stdcmPathSteps.at(1)!;
158157
expect(via.isVia).toBe(true);
159-
const newVia = {
160-
...via,
158+
const updates = {
161159
stopType: StdcmStopTypes.DRIVER_SWITCH,
162160
stopFor: 'PT60',
163161
};
164162

165-
store.dispatch(stdcmConfSliceActions.updateStdcmPathStep(newVia));
163+
store.dispatch(stdcmConfSliceActions.updateStdcmPathStep({ id: via.id, updates }));
166164
const state = store.getState()[stdcmConfSlice.name];
167-
expect(state.stdcmPathSteps.at(1)).toEqual(newVia);
165+
expect(state.stdcmPathSteps.at(1)).toEqual({ ...via, ...updates });
168166
});
169167

170168
it('should handle destination update', () => {
171169
const destination = store.getState()[stdcmConfSlice.name].stdcmPathSteps.at(-1)!;
172170
expect(destination.isVia).toBe(false);
173-
const newDestination = {
174-
...destination,
171+
const updates = {
175172
arrivalType: ArrivalTimeTypes.ASAP,
176173
arrival: '2024-08-12T15:45:00.000+02:00',
177174
tolerances: {
@@ -180,9 +177,9 @@ describe('stdcmConfReducers', () => {
180177
},
181178
};
182179

183-
store.dispatch(stdcmConfSliceActions.updateStdcmPathStep(newDestination));
180+
store.dispatch(stdcmConfSliceActions.updateStdcmPathStep({ id: destination.id, updates }));
184181
const state = store.getState()[stdcmConfSlice.name];
185-
expect(state.stdcmPathSteps.at(-1)).toEqual(newDestination);
182+
expect(state.stdcmPathSteps.at(-1)).toEqual({ ...destination, ...updates });
186183
});
187184
});
188185

front/src/reducers/osrdconf/types.ts

-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,5 @@ export type PathStep = PathItemLocation & {
8484
};
8585

8686
export type StdcmPathStep = PathStep & {
87-
isVia: boolean;
8887
tolerances?: { before: number; after: number };
8988
} & ({ isVia: true; stopType: StdcmStopTypes } | { isVia: false; arrivalType: ArrivalTimeTypes });

0 commit comments

Comments
 (0)