mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
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:
@@ -1,74 +1,34 @@
|
||||
import type React from 'react';
|
||||
import { type Dispatch, type SetStateAction, useState } from 'react';
|
||||
|
||||
import { isEffectivelyOffline } from '../../sync/networkMode';
|
||||
import type { TripFile } from '../../types';
|
||||
import { attachedBookingFiles } from './bookingFormModel';
|
||||
|
||||
export interface BookingFileAttachOptions {
|
||||
/** The booking being edited; files picked before it is saved wait in `pendingFiles`. */
|
||||
reservation: { id: number; title: string } | null | undefined;
|
||||
/** The booking being edited; unset while it is being created. */
|
||||
reservation: { id: number } | null | undefined;
|
||||
/** The trip's files. */
|
||||
files: TripFile[];
|
||||
onFileUpload?: (fd: FormData) => Promise<unknown>;
|
||||
setPendingFiles: Dispatch<SetStateAction<File[]>>;
|
||||
/** The toast and translate the dialog already holds. */
|
||||
toast: { success: (message: string) => void; error: (message: string) => void };
|
||||
t: (key: string) => string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Files row of the desktop booking and transport dialogs: a file picked on a
|
||||
* saved booking goes up at once, one picked on a new booking waits for the save,
|
||||
* and the files linked or detached in the dialog show on the booking right away.
|
||||
* The Files row of the desktop booking and transport dialogs. A picked file waits
|
||||
* in `pendingFiles` and goes up with the save, on a new booking and a saved one
|
||||
* alike, as on the phone (#2534): one that fails stays in the list to try again
|
||||
* instead of vanishing with its toast (#2883). The files linked or detached in the
|
||||
* dialog show on the booking right away.
|
||||
*/
|
||||
export function useBookingFileAttach({
|
||||
reservation,
|
||||
files,
|
||||
onFileUpload,
|
||||
setPendingFiles,
|
||||
toast,
|
||||
t,
|
||||
}: BookingFileAttachOptions) {
|
||||
const [uploadingFile, setUploadingFile] = useState(false);
|
||||
export function useBookingFileAttach({ reservation, files, setPendingFiles }: BookingFileAttachOptions) {
|
||||
const [linkedFileIds, setLinkedFileIds] = useState<number[]>([]);
|
||||
|
||||
const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (!file) return;
|
||||
if (reservation?.id && isEffectivelyOffline()) {
|
||||
// A file goes up at once on a saved booking, and an upload cannot wait in
|
||||
// the offline queue.
|
||||
toast.error(t('reservations.needsConnection'));
|
||||
e.target.value = '';
|
||||
} else if (reservation?.id) {
|
||||
setUploadingFile(true);
|
||||
try {
|
||||
if (onFileUpload) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
fd.append('reservation_id', String(reservation.id));
|
||||
fd.append('description', reservation.title);
|
||||
await onFileUpload(fd);
|
||||
toast.success(t('reservations.toast.fileUploaded'));
|
||||
} else {
|
||||
// A dialog without an upload reports a failed upload, as it always did.
|
||||
toast.error(t('reservations.toast.uploadError'));
|
||||
}
|
||||
} catch {
|
||||
toast.error(t('reservations.toast.uploadError'));
|
||||
} finally {
|
||||
setUploadingFile(false);
|
||||
e.target.value = '';
|
||||
}
|
||||
} else {
|
||||
setPendingFiles((prev) => [...prev, file]);
|
||||
e.target.value = '';
|
||||
}
|
||||
if (file) setPendingFiles((prev) => [...prev, file]);
|
||||
e.target.value = '';
|
||||
};
|
||||
|
||||
return {
|
||||
uploadingFile,
|
||||
handleFileChange,
|
||||
attachedFiles: attachedBookingFiles(files, reservation?.id, linkedFileIds),
|
||||
removePending: (index: number) => setPendingFiles((prev) => prev.filter((_, j) => j !== index)),
|
||||
|
||||
Reference in New Issue
Block a user