mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 02:34:21 +00:00
fix(tours): offer waypoints as places only where a trip takes them, and say when every point was there already
The waypoints switch showed in every Tours import dialog; it now shows in the trip's import only. Confirming it when every point was already in the trip closed the dialog without a word.
This commit is contained in:
@@ -83,7 +83,7 @@ const PlacesSidebar = React.memo(function PlacesSidebar(props: PlacesSidebarProp
|
||||
initialFiles={fileImportFiles}
|
||||
/>
|
||||
{/* With Tours on, a route or track file from this column lands in the Tours preview. */}
|
||||
<TourGpxPreviewDialog gpxImport={tourFileImport} />
|
||||
<TourGpxPreviewDialog gpxImport={tourFileImport} offerPoints />
|
||||
<ContextMenu menu={ctxMenu.menu} onClose={ctxMenu.close} />
|
||||
{categoryPickerOpen && (
|
||||
<PlacesBulkCategoryModal
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// FE-GPX-PREVIEW-001 to FE-GPX-PREVIEW-007
|
||||
// FE-GPX-PREVIEW-001 to FE-GPX-PREVIEW-009
|
||||
// The GPX preview as ACT-01 uses it: preset to the tour's type, a city walk
|
||||
// imported as one, and when a track was recorded shown where it carried times.
|
||||
import { fireEvent, render, screen } from '@testing-library/react';
|
||||
@@ -86,7 +86,7 @@ describe('TourGpxPreviewDialog (ACT-01)', () => {
|
||||
describe('TourGpxPreviewDialog with points beside the lines (#2969)', () => {
|
||||
it('FE-GPX-PREVIEW-005: offers the points as places, on by default, and imports them with the tour', () => {
|
||||
const current = gpxImport([track()], { points: 4, areas: 0, other: 0 });
|
||||
render(<TourGpxPreviewDialog gpxImport={current} />);
|
||||
render(<TourGpxPreviewDialog gpxImport={current} offerPoints />);
|
||||
expect(screen.getByTestId('gpx-preview-points')).toBeInTheDocument();
|
||||
// Added as places, they are not listed as left out.
|
||||
expect(screen.queryByTestId('gpx-preview-left-out')).toBeNull();
|
||||
@@ -96,7 +96,7 @@ describe('TourGpxPreviewDialog with points beside the lines (#2969)', () => {
|
||||
|
||||
it('FE-GPX-PREVIEW-006: switched off, the points are named as left out and not imported', () => {
|
||||
const current = gpxImport([track()], { points: 4, areas: 1, other: 0 });
|
||||
render(<TourGpxPreviewDialog gpxImport={current} />);
|
||||
render(<TourGpxPreviewDialog gpxImport={current} offerPoints />);
|
||||
fireEvent.click(screen.getByRole('button', { name: /tours\.import\.pointsAsPlaces|Add the points as places/ }));
|
||||
expect(screen.getByTestId('gpx-preview-left-out')).toHaveTextContent(/tours\.import\.leftOutPoints|4 pins/);
|
||||
fireEvent.click(screen.getByTestId('gpx-preview-import'));
|
||||
@@ -105,7 +105,7 @@ describe('TourGpxPreviewDialog with points beside the lines (#2969)', () => {
|
||||
|
||||
it('FE-GPX-PREVIEW-007: a file whose every track the trip holds can still bring its points', () => {
|
||||
const current = gpxImport([track({ duplicate: true })], { points: 2, areas: 0, other: 0 });
|
||||
render(<TourGpxPreviewDialog gpxImport={current} />);
|
||||
render(<TourGpxPreviewDialog gpxImport={current} offerPoints />);
|
||||
const button = screen.getByTestId('gpx-preview-import');
|
||||
expect(button).toBeEnabled();
|
||||
expect(button).toHaveTextContent(/tours\.import\.pointsConfirm|Add places/);
|
||||
@@ -113,4 +113,20 @@ describe('TourGpxPreviewDialog with points beside the lines (#2969)', () => {
|
||||
fireEvent.click(button);
|
||||
expect(current.confirm).toHaveBeenCalledWith({ tour_type: 'hike' }, { points: true });
|
||||
});
|
||||
|
||||
it('FE-GPX-PREVIEW-008: not offered, as for a recording picked for a tour, the points stay out', () => {
|
||||
const current = gpxImport([track()], { points: 4, areas: 0, other: 0 });
|
||||
render(<TourGpxPreviewDialog gpxImport={current} initial={{ tour_type: 'hike' }} />);
|
||||
expect(screen.queryByTestId('gpx-preview-points')).toBeNull();
|
||||
expect(screen.getByTestId('gpx-preview-left-out')).toHaveTextContent(/tours\.import\.leftOutPoints|4 pins/);
|
||||
fireEvent.click(screen.getByTestId('gpx-preview-import'));
|
||||
expect(current.confirm).toHaveBeenCalledWith({ tour_type: 'hike' }, { points: false });
|
||||
});
|
||||
|
||||
it('FE-GPX-PREVIEW-009: not offered, a file of known tracks has nothing to import, points or not', () => {
|
||||
const current = gpxImport([track({ duplicate: true })], { points: 2, areas: 0, other: 0 });
|
||||
render(<TourGpxPreviewDialog gpxImport={current} />);
|
||||
expect(screen.getByTestId('gpx-preview-import')).toBeDisabled();
|
||||
expect(screen.getByText(/tours\.import\.previewAllDuplicates|nothing to import/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -113,8 +113,8 @@ function leftOutOf(files: readonly GpxPreviewFile[]): TourFileLeftOut {
|
||||
|
||||
/**
|
||||
* #2969: the single points of the files (GPX waypoints, KML pins), which no
|
||||
* tour is made of, offered as places of the trip. On by default: a file with
|
||||
* a route and its stops is usually meant as both.
|
||||
* tour is made of, offered as places of the trip where the dialog's caller
|
||||
* asks for it.
|
||||
*/
|
||||
function PreviewPoints({ count, on, onToggle }: { count: number; on: boolean; onToggle: () => void }) {
|
||||
const { t } = useTranslation();
|
||||
@@ -183,18 +183,22 @@ function PreviewLeftOut({ leftOut }: { leftOut: TourFileLeftOut }) {
|
||||
* as recorded, gets no riding time and is not checked for access. Closing it
|
||||
* stores nothing. `initial` presets the type, as a recording imported for a
|
||||
* tour takes that tour's (ACT-01); left out, it is a hike. A KML or KMZ file
|
||||
* is shown the same way, with what it holds besides (IO-02).
|
||||
* is shown the same way, with what it holds besides (IO-02). `offerPoints`
|
||||
* offers the file's single points as places (#2969); only the places column
|
||||
* asks for it, a recording picked for a tour is imported for its line alone.
|
||||
*/
|
||||
export default function TourGpxPreviewDialog({
|
||||
gpxImport,
|
||||
initial,
|
||||
offerPoints = false,
|
||||
}: {
|
||||
gpxImport: TourGpxImport;
|
||||
initial?: TourGpxImportFields;
|
||||
offerPoints?: boolean;
|
||||
}) {
|
||||
const { preview } = gpxImport;
|
||||
if (!preview) return null;
|
||||
return <PreviewDialog gpxImport={gpxImport} files={preview} initial={initial ?? HIKE} />;
|
||||
return <PreviewDialog gpxImport={gpxImport} files={preview} initial={initial ?? HIKE} offerPoints={offerPoints} />;
|
||||
}
|
||||
|
||||
const HIKE: TourGpxImportFields = { tour_type: 'hike' };
|
||||
@@ -207,16 +211,20 @@ function canConfirm(fresh: number, addsPoints: boolean, typeComplete: boolean):
|
||||
/** The points offered as places, and below them whatever else stays out. */
|
||||
function PreviewExtras({
|
||||
leftOut,
|
||||
offerPoints,
|
||||
withPoints,
|
||||
onTogglePoints,
|
||||
}: {
|
||||
leftOut: TourFileLeftOut;
|
||||
offerPoints: boolean;
|
||||
withPoints: boolean;
|
||||
onTogglePoints: () => void;
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
{leftOut.points > 0 && <PreviewPoints count={leftOut.points} on={withPoints} onToggle={onTogglePoints} />}
|
||||
{offerPoints && leftOut.points > 0 && (
|
||||
<PreviewPoints count={leftOut.points} on={withPoints} onToggle={onTogglePoints} />
|
||||
)}
|
||||
<PreviewLeftOut leftOut={withPoints ? { ...leftOut, points: 0 } : leftOut} />
|
||||
</>
|
||||
);
|
||||
@@ -226,17 +234,20 @@ function PreviewDialog({
|
||||
gpxImport,
|
||||
files,
|
||||
initial,
|
||||
offerPoints,
|
||||
}: {
|
||||
gpxImport: TourGpxImport;
|
||||
files: GpxPreviewFile[];
|
||||
initial: TourGpxImportFields;
|
||||
offerPoints: boolean;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const labelId = useId();
|
||||
const tileUrl = useTileUrl(OFM_POSITRON);
|
||||
const choices = useGpxImportChoices(initial);
|
||||
const leftOut = useMemo(() => leftOutOf(files), [files]);
|
||||
const [withPoints, setWithPoints] = useState(true);
|
||||
// On by default where offered: a file with a route and its stops is usually meant as both.
|
||||
const [withPoints, setWithPoints] = useState(offerPoints);
|
||||
const addsPoints = withPoints && leftOut.points > 0;
|
||||
|
||||
const tracks = files.flatMap((file) => file.tracks.map((track) => ({ file: file.file, track })));
|
||||
@@ -314,7 +325,12 @@ function PreviewDialog({
|
||||
<PreviewTrackRow key={`${file.name}-${index}`} name={file.name} number={index + 1} track={track} />
|
||||
))}
|
||||
</ul>
|
||||
<PreviewExtras leftOut={leftOut} withPoints={withPoints} onTogglePoints={() => setWithPoints((on) => !on)} />
|
||||
<PreviewExtras
|
||||
leftOut={leftOut}
|
||||
offerPoints={offerPoints}
|
||||
withPoints={withPoints}
|
||||
onTogglePoints={() => setWithPoints((on) => !on)}
|
||||
/>
|
||||
{fresh === 0 && !addsPoints && (
|
||||
<div className="mt-2">
|
||||
<TourNotice tone="warning" icon={<AlertTriangle size={13} strokeWidth={2} />}>
|
||||
|
||||
@@ -458,7 +458,7 @@ describe('ToursSidebar', () => {
|
||||
left_out: { points: 3, areas: 1, other: 0 },
|
||||
})
|
||||
const importCall = vi.spyOn(tourGpxApi, 'import').mockResolvedValue({ tours: [tour], caution: false, skipped: 0 })
|
||||
const pinsCall = vi.spyOn(placesApi, 'importMapFile').mockResolvedValue({ count: 3 })
|
||||
const pinsCall = vi.spyOn(placesApi, 'importMapFile')
|
||||
seedStore(useTripStore, { refreshPlaces: vi.fn().mockResolvedValue(undefined) })
|
||||
render(
|
||||
<ToursSidebar tripId={1} days={[]} tours={[]} canEdit={true} onAssignToDay={vi.fn()} onSelectTour={vi.fn()} />
|
||||
@@ -471,11 +471,11 @@ describe('ToursSidebar', () => {
|
||||
const rows = screen.getAllByTestId('gpx-preview-track')
|
||||
expect(rows.map((row) => row.textContent?.slice(0, 1))).toEqual(['1', '2'])
|
||||
expect(rows[1]).toHaveTextContent('No heights in the file')
|
||||
// The pins are offered as places (#2969); only the area stays out.
|
||||
expect(screen.getByTestId('gpx-preview-points')).toHaveTextContent('3 pins')
|
||||
// Only the places column offers the pins as places (#2969); here they stay out.
|
||||
expect(screen.queryByTestId('gpx-preview-points')).toBeNull()
|
||||
const leftOut = screen.getByTestId('gpx-preview-left-out')
|
||||
expect(leftOut).toHaveTextContent('Not imported')
|
||||
expect(leftOut).not.toHaveTextContent('pins')
|
||||
expect(leftOut).toHaveTextContent('3 pins')
|
||||
expect(leftOut).toHaveTextContent('1 area')
|
||||
expect(leftOut).not.toHaveTextContent('other item')
|
||||
expect(within(leftOut).getByRole('button', { name: 'Not imported' })).toBeInTheDocument()
|
||||
@@ -483,8 +483,7 @@ describe('ToursSidebar', () => {
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Import 2 tours' }))
|
||||
await waitFor(() => expect(importCall).toHaveBeenCalledWith(1, file, { tour_type: 'hike' }))
|
||||
await waitFor(() => expect(toast.success).toHaveBeenCalledWith('1 tour imported'))
|
||||
expect(pinsCall).toHaveBeenCalledWith(1, file, { points: true, paths: false })
|
||||
expect(toast.success).toHaveBeenCalledWith('3 places added')
|
||||
expect(pinsCall).not.toHaveBeenCalled()
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
|
||||
@@ -131,4 +131,31 @@ describe('useTourGpxImport with points beside the lines (#2969)', () => {
|
||||
expect(addToast).toHaveBeenCalledWith('tours.import.pointsFailedIn', 'error', undefined);
|
||||
expect(tourRepo.importGpx).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('says so when every line and every point is already in the trip, instead of closing without a word', async () => {
|
||||
vi.spyOn(placesApi, 'importGpx').mockResolvedValue({ count: 0, skipped: 3 });
|
||||
vi.spyOn(placesApi, 'importMapFile').mockResolvedValue({ count: 0 });
|
||||
const { result } = await previewed(() => ({
|
||||
tracks: [{ ...line, duplicate: true }],
|
||||
left_out: { points: 3, areas: 0, other: 0 },
|
||||
}));
|
||||
await act(() => result.current.confirm({ tour_type: 'hike' }, { points: true }));
|
||||
expect(tourRepo.importGpx).not.toHaveBeenCalled();
|
||||
expect(addToast).toHaveBeenCalledTimes(1);
|
||||
expect(addToast).toHaveBeenCalledWith('tours.import.allSkipped', 'warning', undefined);
|
||||
expect(result.current.preview).toBeNull();
|
||||
});
|
||||
|
||||
it('names the points left out as known beside the places that did come in', async () => {
|
||||
vi.spyOn(placesApi, 'importGpx').mockResolvedValue({ count: 1, skipped: 2 });
|
||||
vi.spyOn(placesApi, 'importMapFile').mockResolvedValue({ count: 2 });
|
||||
const { result } = await previewed((picked) => ({
|
||||
tracks: [{ ...line, duplicate: true }],
|
||||
left_out: { points: picked === route ? 3 : 2, areas: 0, other: 0 },
|
||||
}));
|
||||
await act(() => result.current.confirm({ tour_type: 'hike' }, { points: true }));
|
||||
expect(addToast).toHaveBeenCalledWith('tours.import.pointsImported', 'success', undefined);
|
||||
expect(addToast).toHaveBeenCalledWith('tours.import.pointsSkipped', 'warning', undefined);
|
||||
expect(addToast).not.toHaveBeenCalledWith('tours.import.allSkipped', 'warning', undefined);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -78,22 +78,33 @@ async function importTours(
|
||||
return sum;
|
||||
}
|
||||
|
||||
interface PointsImported {
|
||||
places: number;
|
||||
/** Points the preview counted that did not come in, almost always because the trip already holds them. */
|
||||
skipped: number;
|
||||
}
|
||||
|
||||
/** The points of each file that holds any, added as places (#2969); a file that fails is handed to `onError`. */
|
||||
async function importPoints(
|
||||
tripId: number,
|
||||
files: readonly GpxPreviewFile[],
|
||||
onError: (file: File) => void
|
||||
): Promise<number> {
|
||||
let places = 0;
|
||||
): Promise<PointsImported> {
|
||||
const sum: PointsImported = { places: 0, skipped: 0 };
|
||||
for (const { file, leftOut } of files) {
|
||||
if (!leftOut?.points) continue;
|
||||
try {
|
||||
places += await tourRepo.importFilePoints(tripId, file);
|
||||
const added = await tourRepo.importFilePoints(tripId, file);
|
||||
sum.places += added;
|
||||
// Measured against the preview's count rather than the import's own
|
||||
// skipped figure: the map import counts paths it was told to leave out
|
||||
// as skipped too, the GPX import does not.
|
||||
sum.skipped += Math.max(0, leftOut.points - added);
|
||||
} catch {
|
||||
onError(file);
|
||||
}
|
||||
}
|
||||
return places;
|
||||
return sum;
|
||||
}
|
||||
|
||||
interface TourGpxImportOptions {
|
||||
@@ -180,11 +191,12 @@ export function useTourGpxImport({ tripId, canEdit, onToursChanged, onImported,
|
||||
setImporting(true);
|
||||
try {
|
||||
const { tours: imported, skipped, caution } = await importTours(tripId, preview, fields, fileError);
|
||||
const places = extras.points
|
||||
const points = extras.points
|
||||
? await importPoints(tripId, preview, (file) =>
|
||||
toast.error(t('tours.import.pointsFailedIn', { name: file.name }))
|
||||
)
|
||||
: 0;
|
||||
: { places: 0, skipped: 0 };
|
||||
const places = points.places;
|
||||
setPreview(null);
|
||||
if (places > 0) {
|
||||
if (imported.length === 0) await refreshPlaces(tripId);
|
||||
@@ -200,10 +212,14 @@ export function useTourGpxImport({ tripId, canEdit, onToursChanged, onImported,
|
||||
if (caution) toast.info(t('tours.import.caution'));
|
||||
onImported?.(imported);
|
||||
}
|
||||
if (skipped > 0) {
|
||||
toast.warning(
|
||||
imported.length > 0 ? t('tours.import.skipped', { count: skipped }) : t('tours.import.allSkipped')
|
||||
);
|
||||
// Nothing came in although something was sent: one warning, so the
|
||||
// dialog never closes on a file the trip already holds without a word.
|
||||
const addedAny = imported.length > 0 || places > 0;
|
||||
if (!addedAny && (skipped > 0 || points.skipped > 0)) {
|
||||
toast.warning(t('tours.import.allSkipped'));
|
||||
} else {
|
||||
if (skipped > 0) toast.warning(t('tours.import.skipped', { count: skipped }));
|
||||
if (points.skipped > 0) toast.warning(t('tours.import.pointsSkipped'));
|
||||
}
|
||||
} finally {
|
||||
setImporting(false);
|
||||
|
||||
Reference in New Issue
Block a user