fix(bookings): keep the editor open until a booking's files are uploaded

Saving a booking or a transport closed the editor and reported success
before the travellers and picked files were written. A failed upload left
a booking without some of its PDFs, no list to retry from, and a create
form that would add a duplicate when saved again. On desktop a file picked
on an existing booking went up at once and vanished when that failed.

The planner's save now takes the editor's follow-up as a step it waits
for: it stores the booking, runs the traveller write and the uploads, and
only then says so and closes. Each file that lands leaves the list, so a
failure leaves exactly the files still to send, and the editor keeps the
stored booking so the retry updates it instead of creating another one,
without a second linked cost or stay. All four editors (desktop booking
and transport dialogs, both phone sheets) share this through
useBookingFollowUp, an import review only moves on once an item's files
are up, and desktop files picked on a saved booking wait for Update like
on the phone.

Closes #2883
This commit is contained in:
Maurice
2026-10-29 14:20:57 +01:00
parent 7d669cb5c3
commit b06e3e6266
47 changed files with 642 additions and 341 deletions
@@ -1,12 +1,11 @@
// FE-PLANNER-BOOKINGFILES-001 to -006: the Files row of the desktop booking and
// FE-PLANNER-BOOKINGFILES-001 to -005: the Files row of the desktop booking and
// transport dialogs.
import { act, renderHook } from '@testing-library/react';
import type React from 'react';
import { useState } from 'react';
import { describe, expect, it, vi } from 'vitest';
import { describe, expect, it } from 'vitest';
import { buildTripFile } from '../../../tests/helpers/factories';
import { _resetNetworkMode, setForcedOffline } from '../../sync/networkMode';
import type { TripFile } from '../../types';
import { type BookingFileAttachOptions, useBookingFileAttach } from './useBookingFileAttach';
@@ -18,69 +17,36 @@ function changeEvent(file?: File): FileEvent {
}
function setup(overrides: Partial<BookingFileAttachOptions> = {}, files: TripFile[] = []) {
const toast = { success: vi.fn(), error: vi.fn() };
const t = (key: string) => key;
const view = renderHook(() => {
return renderHook(() => {
const [pendingFiles, setPendingFiles] = useState<File[]>([]);
const attach = useBookingFileAttach({
reservation: { id: 5, title: 'Hotel' },
files,
setPendingFiles,
toast,
t,
...overrides,
});
const attach = useBookingFileAttach({ reservation: { id: 5 }, files, setPendingFiles, ...overrides });
return { attach, pendingFiles };
});
return { ...view, toast };
}
describe('useBookingFileAttach', () => {
it('FE-PLANNER-BOOKINGFILES-001: a file on a saved booking goes up at once and the input clears', async () => {
const onFileUpload = vi.fn(async (_fd: FormData) => undefined);
const { result, toast } = setup({ onFileUpload });
const e = changeEvent(new File(['a'], 'a.pdf'));
await act(() => result.current.attach.handleFileChange(e));
const fd = onFileUpload.mock.calls[0][0];
expect(fd.get('reservation_id')).toBe('5');
expect(fd.get('description')).toBe('Hotel');
expect(toast.success).toHaveBeenCalledWith('reservations.toast.fileUploaded');
it('FE-PLANNER-BOOKINGFILES-001: a file picked on a saved booking waits for the save like on a new one (#2883)', () => {
const { result } = setup();
const a = new File(['a'], 'a.pdf');
const e = changeEvent(a);
act(() => result.current.attach.handleFileChange(e));
expect(result.current.pendingFiles).toEqual([a]);
expect(e.target.value).toBe('');
expect(result.current.attach.uploadingFile).toBe(false);
expect(result.current.pendingFiles).toEqual([]);
});
it('FE-PLANNER-BOOKINGFILES-002: a failed upload, or a dialog without one, says so', async () => {
const onFileUpload = vi.fn(async () => {
throw new Error('boom');
});
const failing = setup({ onFileUpload });
await act(() => failing.result.current.attach.handleFileChange(changeEvent(new File(['a'], 'a.pdf'))));
expect(failing.toast.error).toHaveBeenCalledWith('reservations.toast.uploadError');
const without = setup({ onFileUpload: undefined });
await act(() => without.result.current.attach.handleFileChange(changeEvent(new File(['a'], 'a.pdf'))));
expect(without.toast.error).toHaveBeenCalledWith('reservations.toast.uploadError');
expect(without.toast.success).not.toHaveBeenCalled();
});
it('FE-PLANNER-BOOKINGFILES-003: on a new booking the file waits for the save and can be taken back', async () => {
const onFileUpload = vi.fn(async () => undefined);
const { result } = setup({ reservation: null, onFileUpload });
it('FE-PLANNER-BOOKINGFILES-002: on a new booking the files gather and can be taken back', () => {
const { result } = setup({ reservation: null });
const a = new File(['a'], 'a.pdf');
const b = new File(['b'], 'b.pdf');
const e = changeEvent(a);
await act(() => result.current.attach.handleFileChange(e));
await act(() => result.current.attach.handleFileChange(changeEvent(b)));
await act(() => result.current.attach.handleFileChange(changeEvent()));
expect(onFileUpload).not.toHaveBeenCalled();
expect(e.target.value).toBe('');
act(() => result.current.attach.handleFileChange(changeEvent(a)));
act(() => result.current.attach.handleFileChange(changeEvent(b)));
act(() => result.current.attach.handleFileChange(changeEvent()));
expect(result.current.pendingFiles).toEqual([a, b]);
act(() => result.current.attach.removePending(0));
expect(result.current.pendingFiles).toEqual([b]);
});
it('FE-PLANNER-BOOKINGFILES-004: files linked and detached in the dialog show on the booking', () => {
it('FE-PLANNER-BOOKINGFILES-003: files linked and detached in the dialog show on the booking', () => {
const files = [buildTripFile({ id: 1, reservation_id: 5 }), buildTripFile({ id: 2, reservation_id: null })];
const { result } = setup({}, files);
expect(result.current.attach.attachedFiles.map((f) => f.id)).toEqual([1]);
@@ -90,25 +56,8 @@ describe('useBookingFileAttach', () => {
expect(result.current.attach.attachedFiles.map((f) => f.id)).toEqual([1]);
});
it('FE-PLANNER-BOOKINGFILES-005: a new booking lists no attached files', () => {
it('FE-PLANNER-BOOKINGFILES-004: a new booking lists no attached files', () => {
const { result } = setup({ reservation: null }, [buildTripFile({ id: 1, reservation_id: 5 })]);
expect(result.current.attach.attachedFiles).toEqual([]);
});
it('FE-PLANNER-BOOKINGFILES-006: offline a file on a saved booking is not sent and says it needs a connection (#2601)', async () => {
const onFileUpload = vi.fn(async (_fd: FormData) => undefined);
const { result, toast } = setup({ onFileUpload });
const event = changeEvent(new File(['x'], 'ticket.pdf'));
setForcedOffline(true);
try {
await act(async () => {
await result.current.attach.handleFileChange(event);
});
} finally {
_resetNetworkMode();
}
expect(onFileUpload).not.toHaveBeenCalled();
expect(toast.error).toHaveBeenCalledWith('reservations.needsConnection');
expect(event.target.value).toBe('');
expect(result.current.pendingFiles).toEqual([]);
});
});