From 77481df6f6b5ee03bc51622e3daeac70bde41923 Mon Sep 17 00:00:00 2001 From: Alice Khoudli Date: Wed, 20 Nov 2024 17:24:07 +0100 Subject: [PATCH] fixup! front: fix row deletion in timestop input table --- .../modules/timesStops/TimesStopsInput.tsx | 8 +++-- front/src/modules/timesStops/helpers/utils.ts | 30 ++++++++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/front/src/modules/timesStops/TimesStopsInput.tsx b/front/src/modules/timesStops/TimesStopsInput.tsx index 49715c8c531..a617bf2c027 100644 --- a/front/src/modules/timesStops/TimesStopsInput.tsx +++ b/front/src/modules/timesStops/TimesStopsInput.tsx @@ -15,7 +15,7 @@ import { durationSinceStartTime, formatSuggestedViasToRowVias, onStopSignalToReceptionSignal, - preventUpdateOnNullableToNullable, + normalizeNullableInRow, updateDaySinceDeparture, updateRowTimesAndMargin, } from './helpers/utils'; @@ -114,7 +114,6 @@ const TimesStopsInput = ({ allWaypoints, startTime, pathSteps }: TimesStopsInput rows.length ); updatedRows = updateDaySinceDeparture(updatedRows, startTime); - updatedRows = preventUpdateOnNullableToNullable(updatedRows, rows); if (!updatedRows[operation.fromRowIndex].isMarginValid) { newRows[operation.fromRowIndex].isMarginValid = false; @@ -127,7 +126,10 @@ const TimesStopsInput = ({ allWaypoints, startTime, pathSteps }: TimesStopsInput setRows(newRows); } else { const newVias = updatedRows - .filter((row, index) => !isEqual(row, rows[index])) + .filter( + (row, index) => + !isEqual(normalizeNullablesInRow(row), normalizeNullablesInRow(rows[index])) + ) .map(({ shortSlipDistance, onStopSignal, arrival, departure, ...row }) => ({ ...row, arrival: durationSinceStartTime(startTime, arrival), diff --git a/front/src/modules/timesStops/helpers/utils.ts b/front/src/modules/timesStops/helpers/utils.ts index b3662001810..fe9c132e1a7 100644 --- a/front/src/modules/timesStops/helpers/utils.ts +++ b/front/src/modules/timesStops/helpers/utils.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-use-before-define */ import dayjs from 'dayjs'; import type { TFunction } from 'i18next'; -import { round, isEqual, isNil } from 'lodash'; +import _, { round, isEqual, isNil } from 'lodash'; import { keyColumn, createTextColumn } from 'react-datasheet-grid'; import type { OperationalPointWithTimeAndSpeed } from 'applications/operationalStudies/types'; @@ -218,24 +218,18 @@ export function updateRowTimesAndMargin( } /** - * This function prevent a change from undefined to null (or the reverse) - * from being treated as an actual update of a row (otherwise would occur on deletion of an undefined field) + * This function is called before comparing rows to prevent a change from undefined to null (or the reverse) + * from being treated as an actual update of a row (otherwise changes would occur on deletion of an undefined field) */ -export function preventUpdateOnNullableToNullable( - newRowData: TimesStopsInputRow[], - previousRowData: TimesStopsInputRow[] -): TimesStopsInputRow[] { - return newRowData.map((newRow, index) => { - const previousRow = previousRowData[index]; - // == null is equivalent to === null || === undefined - if (newRow.stopFor == null && previousRow.stopFor == null) { - newRow.stopFor = previousRow.stopFor; - } - if (newRow.theoreticalMargin == null && previousRow.theoreticalMargin == null) { - newRow.theoreticalMargin = previousRow.theoreticalMargin; - } - return newRow; - }); +export function normalizeNullableInRow(row: TimesStopsInputRow): TimesStopsInputRow { + const normalizedRow = _.clone(row); + if (normalizedRow.stopFor === null) { + normalizedRow.stopFor = undefined; + } + if (normalizedRow.theoreticalMargin === null) { + normalizedRow.theoreticalMargin = undefined; + } + return normalizedRow; } /**