mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
fix(mobile): files attached while editing a booking are uploaded on save
The phone booking and transport sheets collect picked files and upload them after the save, but only when the record was new. Editing an existing booking showed the picked files in the sheet, then dropped them on Update without a word. The upload now runs after every save that comes back with a record, through one helper both sheets share. A save that fails uploads nothing. Closes #2534
This commit is contained in:
@@ -12,6 +12,7 @@ import { CustomDatePicker } from '../../../../components/shared/CustomDateTimePi
|
||||
import { BookingCodeInput } from '../../../../components/shared/BookingCode'
|
||||
import { Eyebrow, FIELD_AREA_CLS, FIELD_CLS, FormSheetFooter, FormSheetHeader } from './PlSheetChrome'
|
||||
import MBookingFilesCosts from './MBookingFilesCosts'
|
||||
import { uploadBookingFiles } from './uploadBookingFiles'
|
||||
import { buildAssignmentOptions } from '../../../../components/Planner/assignmentOptions'
|
||||
import GuestBadge from '../../../../components/shared/GuestBadge'
|
||||
import { SPLIT_COLORS } from '../../../../components/Budget/BudgetPanel.constants'
|
||||
@@ -62,7 +63,7 @@ export default function MReservationSheet({ planner, onOpenExpense }: MReservati
|
||||
bookingForAssignmentId, setBookingForAssignmentId,
|
||||
assignments,
|
||||
importReviewActive, advanceImportReview,
|
||||
handleSaveReservation, canUploadFiles, tripActions,
|
||||
handleSaveReservation, canUploadFiles,
|
||||
} = planner
|
||||
const { locale } = useTranslation()
|
||||
const setReservationTravelers = useTripStore(s => s.setReservationTravelers)
|
||||
@@ -286,15 +287,7 @@ export default function MReservationSheet({ planner, onOpenExpense }: MReservati
|
||||
const changed = original.length !== next.length || next.some(id => !original.includes(id))
|
||||
if (changed) await setReservationTravelers(tripId, savedId, next)
|
||||
}
|
||||
if (!res?.id && saved?.id && pendingFiles.length > 0 && canUploadFiles) {
|
||||
for (const file of pendingFiles) {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file)
|
||||
fd.append('reservation_id', String(saved.id))
|
||||
fd.append('description', form.title)
|
||||
await tripActions.addFile(tripId, fd)
|
||||
}
|
||||
}
|
||||
await uploadBookingFiles(planner, saved?.id, pendingFiles, form.title)
|
||||
if (withExpense && saved?.id) {
|
||||
onOpenExpense({ prefill: { reservationId: saved.id, name: form.title, category: typeToCostCategory(form.type) } })
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import { importedPriceEntry } from '../../../../components/Planner/importedPrice
|
||||
import TransitSearchPanel from '../../../../components/Planner/TransitSearchPanel'
|
||||
import { Eyebrow, FIELD_AREA_CLS, FIELD_CLS, FormSheetFooter, FormSheetHeader } from './PlSheetChrome'
|
||||
import MBookingFilesCosts from './MBookingFilesCosts'
|
||||
import { uploadBookingFiles } from './uploadBookingFiles'
|
||||
import GuestBadge from '../../../../components/shared/GuestBadge'
|
||||
import { SPLIT_COLORS } from '../../../../components/Budget/BudgetPanel.constants'
|
||||
import { useTripStore } from '../../../../store/tripStore'
|
||||
@@ -146,7 +147,7 @@ export default function MTransportFormSheet({ planner, onOpenExpense }: MTranspo
|
||||
transportPrefill, transitPrefill, setTransitPrefill,
|
||||
importReviewActive, advanceImportReview,
|
||||
handleSaveTransport, handleDeleteReservation,
|
||||
canUploadFiles, tripActions,
|
||||
canUploadFiles,
|
||||
} = planner
|
||||
const { locale } = useTranslation()
|
||||
const setReservationTravelers = useTripStore(s => s.setReservationTravelers)
|
||||
@@ -586,15 +587,7 @@ export default function MTransportFormSheet({ planner, onOpenExpense }: MTranspo
|
||||
const changed = original.length !== next.length || next.some(id => !original.includes(id))
|
||||
if (changed) await setReservationTravelers(tripId, savedId, next)
|
||||
}
|
||||
if (!res?.id && saved?.id && pendingFiles.length > 0 && canUploadFiles) {
|
||||
for (const file of pendingFiles) {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file)
|
||||
fd.append('reservation_id', String(saved.id))
|
||||
fd.append('description', form.title)
|
||||
await tripActions.addFile(tripId, fd)
|
||||
}
|
||||
}
|
||||
await uploadBookingFiles(planner, saved?.id, pendingFiles, form.title)
|
||||
if (withExpense && saved?.id) {
|
||||
onOpenExpense({ prefill: { reservationId: saved.id, name: form.title, category: typeToCostCategory(form.type) } })
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { TripPlanner } from '../MTripShell'
|
||||
|
||||
/**
|
||||
* Uploads the files picked in a booking or transport sheet against the saved
|
||||
* record. Runs after both a create and an edit: the sheet only holds files the
|
||||
* user just picked, so nothing is uploaded twice, and skipping the edit dropped
|
||||
* them without a word (#2534). A save that did not come back with a record
|
||||
* uploads nothing.
|
||||
*/
|
||||
export async function uploadBookingFiles(
|
||||
planner: Pick<TripPlanner, 'tripId' | 'tripActions' | 'canUploadFiles'>,
|
||||
savedId: number | undefined,
|
||||
files: File[],
|
||||
description: string,
|
||||
): Promise<void> {
|
||||
if (!savedId || !planner.canUploadFiles) return
|
||||
for (const file of files) {
|
||||
const fd = new FormData()
|
||||
fd.append('file', file)
|
||||
fd.append('reservation_id', String(savedId))
|
||||
fd.append('description', description)
|
||||
await planner.tripActions.addFile(planner.tripId, fd)
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import { buildPlanner } from '../../../helpers/mobileTrip'
|
||||
import { resetAllStores } from '../../../helpers/store'
|
||||
import { act, fireEvent, render, screen, waitFor } from '../../../helpers/render'
|
||||
|
||||
// FE-MOB-RESSH-001 to FE-MOB-RESSH-068
|
||||
// FE-MOB-RESSH-001 to FE-MOB-RESSH-068 (plus 037b)
|
||||
|
||||
// Date/time/select pickers have their own suites — here they only have to be
|
||||
// addressable, so they render as plain controls.
|
||||
@@ -724,8 +724,22 @@ describe('MReservationSheet', () => {
|
||||
expect((fd.get('file') as File).name).toBe('a.pdf')
|
||||
})
|
||||
|
||||
it('FE-MOB-RESSH-037: files are not re-uploaded when an existing booking is edited', async () => {
|
||||
const { planner } = setup(makePlanner({ editingReservation: DINNER }))
|
||||
it('FE-MOB-RESSH-037: files attached while editing a booking are uploaded against it (#2534)', async () => {
|
||||
const { planner } = setup(makePlanner({ editingReservation: DINNER, handleSaveReservation: vi.fn(async () => DINNER) }))
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement
|
||||
fireEvent.change(input, { target: { files: [new File(['a'], 'a.pdf'), new File(['b'], 'b.pdf')] } })
|
||||
fireEvent.click(submitBtn())
|
||||
|
||||
await waitFor(() => expect(planner.tripActions.addFile).toHaveBeenCalledTimes(2))
|
||||
const [tripId, fd] = vi.mocked(planner.tripActions.addFile).mock.calls[1] as [number, FormData]
|
||||
expect(tripId).toBe(5)
|
||||
expect(fd.get('reservation_id')).toBe('55')
|
||||
expect(fd.get('description')).toBe('Dinner')
|
||||
expect((fd.get('file') as File).name).toBe('b.pdf')
|
||||
})
|
||||
|
||||
it('FE-MOB-RESSH-037b: a failed edit keeps the attached files off the server', async () => {
|
||||
const { planner } = setup(makePlanner({ editingReservation: DINNER, handleSaveReservation: vi.fn(async () => undefined) }))
|
||||
const input = document.querySelector('input[type="file"]') as HTMLInputElement
|
||||
fireEvent.change(input, { target: { files: [new File(['a'], 'a.pdf')] } })
|
||||
fireEvent.click(submitBtn())
|
||||
|
||||
@@ -14,7 +14,7 @@ import { buildPlanner } from '../../../helpers/mobileTrip'
|
||||
import { resetAllStores, seedStore } from '../../../helpers/store'
|
||||
import { act, fireEvent, render, screen, waitFor } from '../../../helpers/render'
|
||||
|
||||
// FE-MOB-TRFRM-001 to FE-MOB-TRFRM-057
|
||||
// FE-MOB-TRFRM-001 to FE-MOB-TRFRM-059
|
||||
//
|
||||
// The sheet's own pickers (airport/location search, day select, time picker) and
|
||||
// the embedded transit panel are replaced by minimal controlled stand-ins so the
|
||||
@@ -1117,4 +1117,29 @@ describe('MTransportFormSheet', () => {
|
||||
expect(screen.getByRole('button', { name: 'Create expense' })).toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: 'Link' })).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('FE-MOB-TRFRM-058: files attached while editing a transport are uploaded against it (#2534)', async () => {
|
||||
const handleSaveTransport = makeSave(SHINKANSEN)
|
||||
const addFile = vi.fn(async (_tripId: number, _form: FormData) => undefined)
|
||||
renderSheet(makePlanner({ editingTransport: SHINKANSEN, handleSaveTransport, tripActions: { addFile } }))
|
||||
fireEvent.click(screen.getByRole('button', { name: 'attach-file' }))
|
||||
|
||||
await submit('common.update')
|
||||
expect(addFile).toHaveBeenCalledTimes(1)
|
||||
const fd = addFile.mock.calls[0][1] as FormData
|
||||
expect(fd.get('reservation_id')).toBe('61')
|
||||
expect(fd.get('description')).toBe('Shinkansen')
|
||||
expect((fd.get('file') as File).name).toBe('extra.pdf')
|
||||
})
|
||||
|
||||
it('FE-MOB-TRFRM-059: a failed transport edit keeps the attached files off the server', async () => {
|
||||
const handleSaveTransport = vi.fn(async (_payload: SavePayload) => undefined)
|
||||
const addFile = vi.fn(async (_tripId: number, _form: FormData) => undefined)
|
||||
renderSheet(makePlanner({ editingTransport: SHINKANSEN, handleSaveTransport, tripActions: { addFile } }))
|
||||
fireEvent.click(screen.getByRole('button', { name: 'attach-file' }))
|
||||
|
||||
await submit('common.update')
|
||||
expect(handleSaveTransport).toHaveBeenCalled()
|
||||
expect(addFile).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user