mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
fix(pdf): give up a routing export once its dialog or sheet is gone
A PDF of a long trip routes for minutes, and only the skip button could stop it. Closing the export dialog, hiding the phone sheet or leaving the trip left the legs going out and opened the document later on whatever page the user was on. Closing now stops the routing and the document never opens; skipping still prints without the remaining legs. The plan filter moves from TripPDF to pdfScope beside the other plan predicate, which keeps TripPDF under its size entry. The routing test name gets its quotes back, and the export dialog and sheet tests cover the leg count, the skip button and closing while routing.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// FE-PLANNER-EXPORTMODAL-001 to FE-PLANNER-EXPORTMODAL-018
|
||||
import { render, screen, waitFor } from '../../../tests/helpers/render'
|
||||
// FE-PLANNER-EXPORTMODAL-001 to FE-PLANNER-EXPORTMODAL-020
|
||||
import { act, render, screen, waitFor } from '../../../tests/helpers/render'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { downloadTripPDF } from '../PDF/TripPDF'
|
||||
import { buildDay, buildDayNote, buildTrip, buildUser } from '../../../tests/helpers/factories'
|
||||
@@ -291,4 +291,41 @@ describe('TripExportModal', () => {
|
||||
await waitFor(() => expect(downloadTripPDF).toHaveBeenCalledTimes(1))
|
||||
expect(vi.mocked(downloadTripPDF).mock.calls[0][0]).toMatchObject({ onlyUserId: 7 })
|
||||
})
|
||||
|
||||
it('FE-PLANNER-EXPORTMODAL-019: while the PDF routes the row counts the legs, and the rest can be left out (#2876)', async () => {
|
||||
const user = userEvent.setup()
|
||||
let finish: (() => void) | null = null
|
||||
vi.mocked(downloadTripPDF).mockImplementationOnce(() => new Promise<void>(res => { finish = res }))
|
||||
const roadtripViasByDay = { 10: [] }
|
||||
const tourStageStops = new Map()
|
||||
render(<TripExportModal {...makeProps({ roadtripViasByDay, tourStageStops })} />)
|
||||
await user.click(screen.getByText('dayplan.pdf'))
|
||||
await waitFor(() => expect(downloadTripPDF).toHaveBeenCalledTimes(1))
|
||||
const { route } = vi.mocked(downloadTripPDF).mock.calls[0][0]
|
||||
expect(route).toMatchObject({ viasByDay: roadtripViasByDay, tourStageStops })
|
||||
|
||||
act(() => route?.onProgress?.(3, 10))
|
||||
expect(screen.getByText('dayplan.pdfRouting|3|10')).toBeInTheDocument()
|
||||
await user.click(screen.getByRole('button', { name: 'dayplan.pdfSkipRouting' }))
|
||||
expect(route?.signal?.aborted).toBe(true)
|
||||
expect(screen.queryByRole('button', { name: 'dayplan.pdfSkipRouting' })).toBeNull()
|
||||
await act(async () => finish!())
|
||||
})
|
||||
|
||||
it('FE-PLANNER-EXPORTMODAL-020: closing the dialog while the PDF routes gives the export up', async () => {
|
||||
const user = userEvent.setup()
|
||||
let finish: (() => void) | null = null
|
||||
vi.mocked(downloadTripPDF).mockImplementationOnce(() => new Promise<void>(res => { finish = res }))
|
||||
const props = makeProps()
|
||||
const { rerender } = render(<TripExportModal {...props} />)
|
||||
await user.click(screen.getByText('dayplan.pdf'))
|
||||
await waitFor(() => expect(downloadTripPDF).toHaveBeenCalledTimes(1))
|
||||
const { route, signal } = vi.mocked(downloadTripPDF).mock.calls[0][0]
|
||||
|
||||
rerender(<TripExportModal {...props} isOpen={false} />)
|
||||
expect(route?.signal?.aborted).toBe(true)
|
||||
expect(signal?.aborted).toBe(true)
|
||||
await act(async () => finish!())
|
||||
expect(props.onClose).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -66,6 +66,7 @@ export function TripExportModal({
|
||||
fileBase, offerMine, isRunning, anyRunning, exportPdf, pdfRouting, pdfRoutingLabel, skipPdfRouting, downloadIcs, downloadGpx,
|
||||
} = useTripExport({
|
||||
tripId,
|
||||
open: isOpen,
|
||||
data: { trip, days, places, assignments, categories, reservations, dayNotes, viasByDay: roadtripViasByDay, tourStageStops },
|
||||
t,
|
||||
locale,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-PLANNER-TRIPEXPORT-001 to -011: the trip exports behind the desktop export
|
||||
// FE-PLANNER-TRIPEXPORT-001 to -013: the trip exports behind the desktop export
|
||||
// dialog and the phone's export sheet.
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi, type MockInstance } from 'vitest';
|
||||
@@ -19,6 +19,7 @@ const NOTES = { '7': [{ id: 1, text: 'Pack' }] as unknown as DayNote[] };
|
||||
function options(overrides: Partial<TripExportOptions> = {}): TripExportOptions {
|
||||
return {
|
||||
tripId: 3,
|
||||
open: true,
|
||||
data: { trip, days: [], places: [], assignments: {}, categories: [], reservations: [], dayNotes: NOTES },
|
||||
t,
|
||||
locale: 'en-US',
|
||||
@@ -264,6 +265,7 @@ describe('useTripExport', () => {
|
||||
// Skipping stops the routing, not the export: the document still comes out.
|
||||
act(() => result.current.skipPdfRouting());
|
||||
expect(route?.signal?.aborted).toBe(true);
|
||||
expect(vi.mocked(downloadTripPDF).mock.calls[0][0].signal?.aborted).toBe(false);
|
||||
expect(result.current.pdfRouting).toBeNull();
|
||||
// The leg that was already on its way still reports; the row does not come back for it.
|
||||
act(() => route?.onProgress?.(24, 60));
|
||||
@@ -276,4 +278,44 @@ describe('useTripExport', () => {
|
||||
expect(result.current.anyRunning).toBe(false);
|
||||
expect(opts.onExported).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-TRIPEXPORT-012: closing the dialog while the PDF routes gives the export up, and nothing opens', async () => {
|
||||
const pdf = deferred();
|
||||
vi.mocked(downloadTripPDF).mockReturnValue(pdf.promise);
|
||||
const opts = options();
|
||||
const { result, rerender } = renderHook((open: boolean) => useTripExport({ ...opts, open }), {
|
||||
initialProps: true,
|
||||
});
|
||||
let running!: Promise<void>;
|
||||
act(() => {
|
||||
running = result.current.exportPdf();
|
||||
});
|
||||
await vi.waitFor(() => expect(downloadTripPDF).toHaveBeenCalled());
|
||||
const { route, signal } = vi.mocked(downloadTripPDF).mock.calls[0][0];
|
||||
|
||||
rerender(false);
|
||||
expect(route?.signal?.aborted).toBe(true);
|
||||
expect(signal?.aborted).toBe(true);
|
||||
await act(async () => {
|
||||
pdf.resolve();
|
||||
await running;
|
||||
});
|
||||
expect(opts.onExported).not.toHaveBeenCalled();
|
||||
expect(opts.toast.error).not.toHaveBeenCalled();
|
||||
expect(result.current.anyRunning).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-TRIPEXPORT-013: leaving the trip while the PDF routes gives the export up too', async () => {
|
||||
vi.mocked(downloadTripPDF).mockReturnValue(deferred().promise);
|
||||
const { result, unmount } = renderHook(() => useTripExport(options(PHONE)));
|
||||
act(() => {
|
||||
void result.current.exportPdf();
|
||||
});
|
||||
await vi.waitFor(() => expect(downloadTripPDF).toHaveBeenCalled());
|
||||
const { route, signal } = vi.mocked(downloadTripPDF).mock.calls[0][0];
|
||||
|
||||
unmount();
|
||||
expect(route?.signal?.aborted).toBe(true);
|
||||
expect(signal?.aborted).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { useRoadtripSettings } from '../../hooks/useRoadtripSettings';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
@@ -36,6 +36,11 @@ interface PdfRouting {
|
||||
|
||||
export interface TripExportOptions {
|
||||
tripId: number;
|
||||
/**
|
||||
* Whether the dialog or sheet is shown. Closing it, or leaving the trip, gives up a
|
||||
* PDF still routing: its legs stop going out and the document never opens.
|
||||
*/
|
||||
open: boolean;
|
||||
data: TripExportData;
|
||||
t: Translate;
|
||||
locale: string;
|
||||
@@ -83,26 +88,37 @@ export function flatDayNotes(dayNotes: Record<string, DayNote[]>) {
|
||||
* minutes, one leg a second against the shared router, so the row counts the legs and
|
||||
* offers to print without the ones still to come; those are marked in the document (#2876).
|
||||
*/
|
||||
function usePdfRouting() {
|
||||
function usePdfRouting(open: boolean) {
|
||||
const [progress, setProgress] = useState<PdfRouting | null>(null);
|
||||
const controller = useRef<AbortController | null>(null);
|
||||
const controller = useRef<{ routing: AbortController; job: AbortController } | null>(null);
|
||||
|
||||
const start = () => {
|
||||
const routing = new AbortController();
|
||||
controller.current = routing;
|
||||
const running = { routing, job: new AbortController() };
|
||||
controller.current = running;
|
||||
// A leg already on its way still reports after a skip; the row stays quiet about it.
|
||||
const onProgress = (done: number, total: number) =>
|
||||
setProgress(!routing.signal.aborted && done < total ? { done, total } : null);
|
||||
return { signal: routing.signal, onProgress };
|
||||
return { signal: routing.signal, onProgress, givenUp: running.job.signal };
|
||||
};
|
||||
const finish = () => {
|
||||
controller.current = null;
|
||||
setProgress(null);
|
||||
};
|
||||
const skip = () => {
|
||||
controller.current?.abort();
|
||||
controller.current?.routing.abort();
|
||||
setProgress(null);
|
||||
};
|
||||
// Unlike a skip, nobody is left to print for: the export ends without a document.
|
||||
const giveUp = useCallback(() => {
|
||||
controller.current?.routing.abort();
|
||||
controller.current?.job.abort();
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
if (!open) giveUp();
|
||||
}, [open, giveUp]);
|
||||
useEffect(() => giveUp, [giveUp]);
|
||||
|
||||
return { progress, start, finish, skip };
|
||||
}
|
||||
|
||||
@@ -114,6 +130,7 @@ function usePdfRouting() {
|
||||
*/
|
||||
export function useTripExport({
|
||||
tripId,
|
||||
open,
|
||||
data,
|
||||
t,
|
||||
locale,
|
||||
@@ -127,7 +144,7 @@ export function useTripExport({
|
||||
// Which export is working, per lane, so a row can say so instead of looking inert
|
||||
// while a 226 kB PDF builder is fetched and a document is rendered.
|
||||
const [running, setRunning] = useState<Record<string, ExportJob>>({});
|
||||
const pdfRouting = usePdfRouting();
|
||||
const pdfRouting = usePdfRouting(open);
|
||||
// The PDF is built outside React, so it cannot read these itself (#2066).
|
||||
const timeFormat = useSettingsStore((s) => s.settings.time_format) || '24h';
|
||||
const distanceUnit = useSettingsStore((s) => s.settings.distance_unit);
|
||||
@@ -157,7 +174,7 @@ export function useTripExport({
|
||||
if ((requireTrip && !data.trip) || running[laneOf(job)]) return;
|
||||
begin(job);
|
||||
const flatNotes = flatDayNotes(data.dayNotes);
|
||||
const routing = pdfRouting.start();
|
||||
const { givenUp, ...routing } = pdfRouting.start();
|
||||
try {
|
||||
// Loaded on click: the PDF builder is ~226 kB, so every trip used to pay for it
|
||||
// whether or not anyone exported. A missing chunk lands in the catch and shows
|
||||
@@ -178,8 +195,9 @@ export function useTripExport({
|
||||
showServiceStops,
|
||||
onlyUserId: mine ? myId : undefined,
|
||||
route: { viasByDay: data.viasByDay, tourStageStops: data.tourStageStops, ...routing },
|
||||
signal: givenUp,
|
||||
});
|
||||
if (closeAfterPdf) onExported();
|
||||
if (closeAfterPdf && !givenUp.aborted) onExported();
|
||||
} catch (e) {
|
||||
if (logPdfErrors) console.error('PDF error:', e);
|
||||
toast.error(`${t('dayplan.pdfError')}: ${e instanceof Error ? e.message : String(e)}`);
|
||||
|
||||
Reference in New Issue
Block a user