fix(days): a day without a date keeps no date when it is moved, so it can go to the front of a dated trip

Reordering handed out the dates by position, so moving a day added without a
date to the front gave it the first date and took the date of the last dated
day. The undated row stayed at the end and the move looked like it did
nothing, by drag and by the arrows alike. The dates now go to the dated days
only, in their new order; the rule lives in shared so the server, the
planner's optimistic list and the MCP tool apply the same one.
This commit is contained in:
Maurice
2026-10-10 17:08:28 +02:00
parent d496dd5d30
commit d2e044869a
7 changed files with 106 additions and 11 deletions
+36
View File
@@ -5,6 +5,7 @@ import {
nextTripDate,
planDatedAppend,
planDayGrid,
reorderedDayDates,
resolveDayGridRange,
} from './day-grid';
import { MAX_TRIP_DAYS } from './trip.schema';
@@ -238,3 +239,38 @@ describe('planDayGrid', () => {
});
});
});
describe('reorderedDayDates', () => {
const days = [
{ id: 1, date: '2026-10-01' },
{ id: 2, date: '2026-10-02' },
{ id: 3, date: '2026-10-03' },
{ id: 4, date: null },
];
it('keeps the dates on their slots while dated days change places', () => {
expect([...reorderedDayDates(days, [3, 1, 2, 4])]).toEqual([
[3, '2026-10-01'],
[1, '2026-10-02'],
[2, '2026-10-03'],
[4, null],
]);
});
it('moves a day without a date to the front without handing it a date', () => {
expect([...reorderedDayDates(days, [4, 1, 2, 3])]).toEqual([
[4, null],
[1, '2026-10-01'],
[2, '2026-10-02'],
[3, '2026-10-03'],
]);
});
it('leaves every day without a date on a trip without dates', () => {
const undated = [{ id: 1, date: null }, { id: 2 }];
expect([...reorderedDayDates(undated, [2, 1])]).toEqual([
[2, null],
[1, null],
]);
});
});
+26
View File
@@ -66,6 +66,32 @@ export function planDatedAppend(
return { date, fits: tripSpanDays(trip.start_date, date) <= MAX_TRIP_DAYS };
}
/**
* The date every day carries after the days are put in a new order. The dates stay
* with the positions, so content moves across the calendar, but only the dated days
* take part: a day without a date keeps none wherever it is moved, and the dated days
* take the trip's dates in ascending order as they now come. Handing out the dates by
* plain position instead gave a day added without a date the first date when it was
* moved to the front and took the date of the last dated day, so the undated row
* stayed at the end and the move looked as if nothing had happened.
*/
export function reorderedDayDates(
days: readonly { id: number; date?: string | null }[],
orderedIds: readonly number[],
): Map<number, string | null> {
const dated = new Set(days.filter((d) => !!d.date).map((d) => d.id));
const sortedDates = days
.map((d) => d.date)
.filter((d): d is string => !!d)
.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
const result = new Map<number, string | null>();
let next = 0;
for (const id of orderedIds) {
result.set(id, dated.has(id) ? (sortedDates[next++] ?? null) : null);
}
return result;
}
/** The dates and day count an update leaves a trip with, and whether its day rows are rebuilt. */
export interface DayGridRange {
newStart: string | null | undefined;