Skip to content

Commit

Permalink
front: fix row deletion in timestop input table
Browse files Browse the repository at this point in the history
Signed-off-by: Alice Khoudli <alice.khoudli@polytechnique.org>
  • Loading branch information
Synar committed Nov 22, 2024
1 parent 8a2b4c9 commit f584878
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
6 changes: 5 additions & 1 deletion front/src/modules/timesStops/TimesStopsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
durationSinceStartTime,
formatSuggestedViasToRowVias,
onStopSignalToReceptionSignal,
normalizeNullablesInRow,
updateDaySinceDeparture,
updateRowTimesAndMargin,
} from './helpers/utils';
Expand Down Expand Up @@ -125,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
4 changes: 2 additions & 2 deletions front/src/modules/timesStops/helpers/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('updateRowTimesAndMargin', () => {
name: 'Gr',
arrival: undefined,
isMarginValid: true,
onStopSignal: false,
onStopSignal: undefined,
theoreticalMargin: '0%',
});
});
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('updateRowTimesAndMargin', () => {
departure: undefined,
stopFor: undefined,
isMarginValid: true,
onStopSignal: false,
onStopSignal: undefined,
});
});
});
Expand Down
21 changes: 20 additions & 1 deletion front/src/modules/timesStops/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ export function updateRowTimesAndMargin(
newRowData.stopFor = undefined;
}
}
if (!newRowData.stopFor && op.fromRowIndex !== allWaypointsLength - 1) {
if (
!newRowData.stopFor &&
newRowData.onStopSignal &&
op.fromRowIndex !== allWaypointsLength - 1
) {
newRowData.onStopSignal = false;
}
newRowData.isMarginValid = !(
Expand All @@ -213,6 +217,21 @@ export function updateRowTimesAndMargin(
return newRowData;
}

/**
* 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 normalizeNullablesInRow(row: TimesStopsInputRow): TimesStopsInputRow {
const normalizedRow = { ...row };
if (normalizedRow.stopFor === null) {
normalizedRow.stopFor = undefined;
}
if (normalizedRow.theoreticalMargin === null) {
normalizedRow.theoreticalMargin = undefined;
}
return normalizedRow;
}

/**
* This function goes through the whole array of path waypoints
* and updates the number of days since departure.
Expand Down

0 comments on commit f584878

Please sign in to comment.