mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 13:23:04 +00:00
fix(planner): a timed booking dropped against its time goes back alone instead of taking the stops behind the drop along
The day list draws a row with a time by that time, and an untimed stop takes the time of the timed row before it. A transit ride dropped behind a later stop was stored there, drawn back at its time on the next render, and every untimed stop behind the drop went with it; the next drag then stored that shuffle. A dropped booking or timed note now lands in the slot nearest the drop the list draws it in, so nothing else moves and what is stored is what is shown. Rides dropped where their time allows stay on the drop, and the rule for untimed stops behind a booking is unchanged. The chronology check for a timed stop reads a booking's time for the day (the arrival on the last day of a multi-day ride) instead of its departure, and the three copies of the remove and insert step share one helper. Refs #2631
This commit is contained in:
@@ -1,10 +1,19 @@
|
||||
// FE-PLANNER-DAYPLAN-236: the drag data a booking drop leaves behind. The mocks
|
||||
// are the ones DayPlanSidebar.test.tsx renders the sidebar with.
|
||||
// FE-PLANNER-DAYPLAN-236: the drag data a booking drop leaves behind, and
|
||||
// FE-PLANNER-DAYPLAN-237/238: where a timed booking dropped in the day lands. The
|
||||
// mocks are the ones DayPlanSidebar.test.tsx renders the sidebar with.
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { buildDay, buildReservation, buildTrip, buildUser } from '../../../tests/helpers/factories';
|
||||
import {
|
||||
buildAssignment,
|
||||
buildDay,
|
||||
buildPlace,
|
||||
buildReservation,
|
||||
buildTrip,
|
||||
buildUser,
|
||||
} from '../../../tests/helpers/factories';
|
||||
import { server } from '../../../tests/helpers/msw/server';
|
||||
import { fireEvent, render, screen } from '../../../tests/helpers/render';
|
||||
import { fireEvent, render, screen, waitFor } from '../../../tests/helpers/render';
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { reservationsApi } from '../../api/client';
|
||||
import { clearExchangeRateCache } from '../../hooks/useExchangeRates';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
@@ -183,3 +192,61 @@ describe('DayPlanSidebar drag data', () => {
|
||||
expect(window.__dragData).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
// #2631: a booking with a time is drawn by it, and the untimed stops behind it take
|
||||
// that time. Dropped against its time it went back on the next render and took the
|
||||
// stops behind the drop with it, so the day reshuffled on every drag.
|
||||
describe('DayPlanSidebar timed booking drop', () => {
|
||||
const renderDay = (busTime: string, busSlot: number) => {
|
||||
const day = buildDay({ id: 10, date: '2025-06-01', title: 'Day 1' });
|
||||
const stops = [
|
||||
buildPlace({ id: 1, name: 'Breakfast', place_time: '08:00' }),
|
||||
buildPlace({ id: 2, name: 'Museum', place_time: '14:00' }),
|
||||
buildPlace({ id: 3, name: 'Old town' }),
|
||||
buildPlace({ id: 4, name: 'Harbour' }),
|
||||
];
|
||||
const assignments = {
|
||||
'10': stops.map((place, i) => buildAssignment({ id: 11 + i, day_id: 10, order_index: i, place })),
|
||||
};
|
||||
const bus = buildReservation({
|
||||
id: 500,
|
||||
type: 'bus',
|
||||
title: 'City bus',
|
||||
day_id: 10,
|
||||
reservation_time: `2025-06-01T${busTime}`,
|
||||
day_plan_position: busSlot,
|
||||
day_positions: { 10: busSlot },
|
||||
});
|
||||
seedStore(useTripStore, { reservations: [bus] });
|
||||
const onReorder = vi.fn(async () => undefined);
|
||||
render(
|
||||
<DayPlanSidebar
|
||||
{...makeDefaultProps({ days: [day], places: stops, assignments, reservations: [bus], onReorder })}
|
||||
/>
|
||||
);
|
||||
const row = (text: string) => screen.getByText(text).closest('[draggable="true"]') as HTMLElement;
|
||||
fireEvent.dragStart(row('City bus'), {
|
||||
dataTransfer: { setData: vi.fn(), effectAllowed: '', getData: vi.fn(() => '') },
|
||||
});
|
||||
fireEvent.drop(row('Harbour'), { dataTransfer: { getData: vi.fn(() => '') } });
|
||||
return { onReorder };
|
||||
};
|
||||
|
||||
it('FE-PLANNER-DAYPLAN-237: a booking dropped against its time stays at its time and moves no stop', async () => {
|
||||
// The 12:00 bus dropped ahead of Harbour, behind the 14:00 museum.
|
||||
const { onReorder } = renderDay('12:00', 0.5);
|
||||
await waitFor(() =>
|
||||
expect(reservationsApi.updatePositions).toHaveBeenCalledWith(1, [{ id: 500, day_plan_position: 0.5 }], 10)
|
||||
);
|
||||
expect(onReorder).toHaveBeenCalledWith(10, [11, 12, 13, 14]);
|
||||
expect(useTripStore.getState().reservations[0].day_positions).toEqual({ 10: 0.5 });
|
||||
});
|
||||
|
||||
it('FE-PLANNER-DAYPLAN-238: a booking dropped where its time allows lands on the drop', async () => {
|
||||
const { onReorder } = renderDay('16:00', 1.5);
|
||||
await waitFor(() =>
|
||||
expect(reservationsApi.updatePositions).toHaveBeenCalledWith(1, [{ id: 500, day_plan_position: 2.5 }], 10)
|
||||
);
|
||||
expect(onReorder).toHaveBeenCalledWith(10, [11, 12, 13, 14]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -46,7 +46,7 @@ import { getAccommodationAnchors, getDayBookendHotels, shouldDrawMorningLeg, sho
|
||||
import {
|
||||
TRANSPORT_TYPES, parseTimeToMinutes, getSpanPhase, hidesOnMiddleDay, getDisplayTimeForDay, getTransportRouteEndpoints,
|
||||
getTransportForDay as _getTransportForDay, getMergedItems as _getMergedItems, isCarrierTransport, hasCarrierEndpointOnDay,
|
||||
getAssignmentReservations, timedSlot, storedRideSlot,
|
||||
getAssignmentReservations, timedSlot, storedRideSlot, seatTimedDrop,
|
||||
type MergedItem,
|
||||
} from '../../utils/dayMerge'
|
||||
import { withinDriveRange } from '@trek/shared/roadtrip'
|
||||
@@ -54,7 +54,7 @@ import { formatDate, formatTime, formatMoneySum, splitReservationDateTime } from
|
||||
import { dayHeadingParts } from '../../utils/dayLabel'
|
||||
import { pendingStayIds } from '../../utils/pendingStays'
|
||||
import { dayWeatherAnchor } from '../../utils/dayWeather'
|
||||
import { metadataWithLegPositions, planMergedOrder, withTransportPositions } from '../../utils/mergedOrder'
|
||||
import { metadataWithLegPositions, moveRow, planMergedOrder, withTransportPositions } from '../../utils/mergedOrder'
|
||||
import { planCosts } from './planCosts'
|
||||
import { useDayNotes } from '../../hooks/useDayNotes'
|
||||
import { useExchangeRates } from '../../hooks/useExchangeRates'
|
||||
@@ -939,16 +939,9 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) {
|
||||
const fromIdx = m.findIndex(i => i.type === fromType && i.data.id === fromId)
|
||||
const toIdx = m.findIndex(matchTo)
|
||||
if (fromIdx !== -1 && toIdx !== -1) {
|
||||
const simulated = [...m]
|
||||
const [moved] = simulated.splice(fromIdx, 1)
|
||||
let insertIdx = simulated.findIndex(matchTo)
|
||||
if (insertIdx === -1) insertIdx = simulated.length
|
||||
if (insertAfter) insertIdx += 1
|
||||
simulated.splice(insertIdx, 0, moved)
|
||||
|
||||
const timedInOrder = simulated
|
||||
const timedInOrder = moveRow(m, fromIdx, matchTo, insertAfter).order
|
||||
.map(i => {
|
||||
if (i.type === 'transport') return parseTimeToMinutes(i.data?.reservation_time)
|
||||
if (i.type === 'transport') return parseTimeToMinutes(getDisplayTimeForDay(i.data, dayId))
|
||||
if (i.type === 'place') return parseTimeToMinutes(i.data?.place?.place_time)
|
||||
return null
|
||||
})
|
||||
@@ -974,17 +967,9 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) {
|
||||
return
|
||||
}
|
||||
|
||||
const newOrder = [...m]
|
||||
const [moved] = newOrder.splice(fromIdx, 1)
|
||||
let adjustedTo = newOrder.findIndex(matchTo)
|
||||
if (adjustedTo === -1) adjustedTo = newOrder.length
|
||||
if (insertAfter) adjustedTo += 1
|
||||
newOrder.splice(adjustedTo, 0, moved)
|
||||
|
||||
await applyMergedOrder(dayId, newOrder)
|
||||
setDraggingId(null)
|
||||
setDropTargetKey(null)
|
||||
dragDataRef.current = null
|
||||
const dropped = moveRow(m, fromIdx, matchTo, insertAfter)
|
||||
await applyMergedOrder(dayId, seatTimedDrop(dropped.order, dropped.index, dayId, getDisplayTimeForDay)) // #2631
|
||||
setDraggingId(null); setDropTargetKey(null); dragDataRef.current = null
|
||||
}
|
||||
|
||||
const confirmTimeRemoval = async () => {
|
||||
@@ -1011,14 +996,7 @@ function useDayPlanSidebar(props: DayPlanSidebarProps) {
|
||||
const toIdx = m.findIndex(matchTo)
|
||||
if (fromIdx === -1 || toIdx === -1 || fromIdx === toIdx) return
|
||||
|
||||
const newOrder = [...m]
|
||||
const [moved] = newOrder.splice(fromIdx, 1)
|
||||
let adjustedTo = newOrder.findIndex(matchTo)
|
||||
if (adjustedTo === -1) adjustedTo = newOrder.length
|
||||
if (insertAfter) adjustedTo += 1
|
||||
newOrder.splice(adjustedTo, 0, moved)
|
||||
|
||||
await applyMergedOrder(dayId, newOrder)
|
||||
await applyMergedOrder(dayId, moveRow(m, fromIdx, matchTo, insertAfter).order)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1866,7 +1844,7 @@ const DayPlanSidebar = React.memo(function DayPlanSidebar(props: DayPlanSidebarP
|
||||
if (parseTimeToMinutes(placeTime) !== null) {
|
||||
const timedInNewOrder = newOrder
|
||||
.map(i => {
|
||||
if (i.type === 'transport') return parseTimeToMinutes(i.data?.reservation_time)
|
||||
if (i.type === 'transport') return parseTimeToMinutes(getDisplayTimeForDay(i.data, day.id))
|
||||
if (i.type === 'place') return parseTimeToMinutes(i.data?.place?.place_time)
|
||||
return null
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user