mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-06-24 07:41:47 +00:00
fix(planner): keep a reservation on its day when edited (#1237)
Editing a booking forced its day_id to the globally selected day, which is null when editing from the Book tab - so the booking lost its day and vanished from the Plan. Preserve the reservation own day_id on edit instead.
This commit is contained in:
@@ -1160,10 +1160,13 @@ describe('TripPlannerPage', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('FE-PAGE-PLANNER-041: handleSaveReservation edit path covers update reservation', () => {
|
describe('FE-PAGE-PLANNER-041: handleSaveReservation edit path covers update reservation', () => {
|
||||||
it('calls onEdit then onSave on ReservationModal to exercise the edit-reservation handler', async () => {
|
it('preserves the reservation day_id on edit so it stays in the Plan (#1237)', async () => {
|
||||||
vi.useFakeTimers();
|
vi.useFakeTimers();
|
||||||
|
|
||||||
seedTripStore({ id: 42 });
|
seedTripStore({ id: 42 });
|
||||||
|
// Capture the update payload — tripActions is a snapshot of the store at mount.
|
||||||
|
const updateReservationSpy = vi.fn().mockResolvedValue({ id: 1, day_id: 7 });
|
||||||
|
seedStore(useTripStore, { updateReservation: updateReservationSpy } as any);
|
||||||
|
|
||||||
renderPlannerPage(42);
|
renderPlannerPage(42);
|
||||||
|
|
||||||
@@ -1179,20 +1182,26 @@ describe('TripPlannerPage', () => {
|
|||||||
expect(screen.getByTestId('reservations-panel')).toBeInTheDocument();
|
expect(screen.getByTestId('reservations-panel')).toBeInTheDocument();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set editingReservation via captured onEdit prop (inline lambda in JSX)
|
// Edit a reservation that lives on day 7 (no day is selected — Book tab).
|
||||||
const fakeReservation = { id: 1, trip_id: 42, name: 'Test', type: 'restaurant', status: 'confirmed' };
|
const fakeReservation = { id: 1, trip_id: 42, name: 'Test', type: 'other', status: 'confirmed', day_id: 7 };
|
||||||
await act(async () => {
|
await act(async () => {
|
||||||
capturedReservationsPanelProps.current.onEdit?.(fakeReservation);
|
capturedReservationsPanelProps.current.onEdit?.(fakeReservation);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Call onSave — now takes edit path (editingReservation is set)
|
|
||||||
await act(async () => {
|
await act(async () => {
|
||||||
await capturedReservationModalProps.current.onSave?.({
|
await capturedReservationModalProps.current.onSave?.({
|
||||||
name: 'Updated Booking',
|
name: 'Updated Booking',
|
||||||
type: 'restaurant',
|
type: 'tour',
|
||||||
status: 'confirmed',
|
status: 'confirmed',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The booking must keep its own day_id (not be nulled to the selected day).
|
||||||
|
expect(updateReservationSpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
1,
|
||||||
|
expect.objectContaining({ day_id: 7 }),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -568,7 +568,10 @@ export function useTripPlanner() {
|
|||||||
const handleSaveReservation = async (data: Record<string, string | number | null> & { title: string }) => {
|
const handleSaveReservation = async (data: Record<string, string | number | null> & { title: string }) => {
|
||||||
try {
|
try {
|
||||||
if (editingReservation) {
|
if (editingReservation) {
|
||||||
const r = await tripActions.updateReservation(tripId, editingReservation.id, { ...data, day_id: selectedDayId || null })
|
// Keep the reservation on its own day. Forcing selectedDayId here dropped
|
||||||
|
// the booking out of the Plan when edited from the Book tab (no day
|
||||||
|
// selected -> selectedDayId null -> day_id nulled).
|
||||||
|
const r = await tripActions.updateReservation(tripId, editingReservation.id, { ...data, day_id: editingReservation.day_id ?? null })
|
||||||
toast.success(t('trip.toast.reservationUpdated'))
|
toast.success(t('trip.toast.reservationUpdated'))
|
||||||
setShowReservationModal(false)
|
setShowReservationModal(false)
|
||||||
setEditingReservation(null)
|
setEditingReservation(null)
|
||||||
|
|||||||
Reference in New Issue
Block a user