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
+23
View File
@@ -42,6 +42,29 @@ describe('daysSlice', () => {
expect(optimistic[0].title).toBe('Departure');
});
it('FE-TSLICE-DAYS-001b: a day without a date moves to the front and keeps no date', async () => {
const days = [
buildDay({ id: 1, trip_id: 1, day_number: 1, date: '2025-06-01' }),
buildDay({ id: 2, trip_id: 1, day_number: 2, date: '2025-06-02' }),
buildDay({ id: 3, trip_id: 1, day_number: 3, date: null }),
];
seedStore(useTripStore, { days });
let optimistic: Day[] = [];
server.use(
http.put('/api/trips/1/days/reorder', () => {
optimistic = useTripStore.getState().days;
return HttpResponse.json({ success: true });
}),
http.get('/api/trips/1/days', () => HttpResponse.json({ days })),
);
await useTripStore.getState().reorderDays(1, [3, 1, 2]);
expect(optimistic.map(d => d.id)).toEqual([3, 1, 2]);
expect(optimistic.map(d => d.date)).toEqual([null, '2025-06-01', '2025-06-02']);
});
it('FE-TSLICE-DAYS-002: sends the requested order and refreshes days plus bookings', async () => {
seedStore(useTripStore, { days: datedDays(), reservations: [] });
+3 -2
View File
@@ -4,6 +4,7 @@ import type { StoreApi } from 'zustand'
import type { TripStoreState } from '../tripStore'
import type { Day } from '../../types'
import { getApiErrorMessage } from '../../types'
import { reorderedDayDates } from '@trek/shared'
type SetState = StoreApi<TripStoreState>['setState']
type GetState = StoreApi<TripStoreState>['getState']
@@ -47,12 +48,12 @@ export const createDaysSlice = (set: SetState, get: GetState): DaysSlice => ({
reorderDays: async (tripId, orderedIds) => {
const prevDays = get().days
const byId = new Map(prevDays.map(d => [d.id, d]))
const sortedDates = prevDays.map(d => d.date).filter((d): d is string => !!d).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
const dates = reorderedDayDates(prevDays, orderedIds)
const optimistic = orderedIds
.map((id, i) => {
const d = byId.get(id)
if (!d) return null
return { ...d, day_number: i + 1, date: sortedDates.length ? (sortedDates[i] ?? null) : d.date }
return { ...d, day_number: i + 1, date: dates.get(id) ?? null }
})
.filter((d): d is NonNullable<typeof d> => d !== null)