Skip to content

Commit

Permalink
fixup! front: fix row deletion in timestop input table
Browse files Browse the repository at this point in the history
  • Loading branch information
Synar committed Nov 20, 2024
1 parent 07c4387 commit 77481df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
8 changes: 5 additions & 3 deletions front/src/modules/timesStops/TimesStopsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
durationSinceStartTime,
formatSuggestedViasToRowVias,
onStopSignalToReceptionSignal,
preventUpdateOnNullableToNullable,
normalizeNullableInRow,
updateDaySinceDeparture,
updateRowTimesAndMargin,
} from './helpers/utils';
Expand Down Expand Up @@ -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;
Expand All @@ -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),
Expand Down
30 changes: 12 additions & 18 deletions front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 77481df

Please sign in to comment.