Tours: save a tour as a new, independent tour (P2C-07)

This commit is contained in:
Maurice
2026-10-15 22:18:41 +02:00
parent 356df90095
commit 485e360c64
38 changed files with 432 additions and 62 deletions
+8
View File
@@ -0,0 +1,8 @@
import type { TourCopyRequest, TourCreateResponse } from '@trek/shared';
import apiClient from './client';
/** P2C-07: a tour of the trip saved again as a new, independent tour. */
export const tourCopiesApi = {
copy: (tripId: number | string, placeId: number | string, body: TourCopyRequest): Promise<TourCreateResponse> =>
apiClient.post(`/trips/${tripId}/tours/${placeId}/copy`, body).then((r) => r.data),
};
@@ -7,11 +7,13 @@ import {
ChevronDown,
ChevronUp,
Clock,
CopyPlus,
ExternalLink,
File,
FileDown,
FileImage,
FileText,
Loader2,
Minus,
Mountain,
Navigation,
@@ -72,6 +74,8 @@ interface TourDetailDialogProps extends TourPermissionProps {
onFileUpload?: (formData: FormData) => Promise<unknown>;
/** A wanderer snapshot's source check brought the changed trail in as a new tour (MF-03). */
onSourceReimported?: (tour: TourListItem) => Promise<void> | void;
/** P2C-07: save this tour again as a new, independent one. */
onSaveAsNew?: (tour: TourListItem) => Promise<void> | void;
onAssignToDay?: (placeId: number, dayId?: number) => void;
onRemoveAssignment?: (dayId: number, assignmentId: number) => void;
onDelete?: () => void;
@@ -303,6 +307,7 @@ export default function TourDetailDialog({
onUpdatePlace,
onFileUpload,
onSourceReimported,
onSaveAsNew,
onAssignToDay,
onRemoveAssignment,
onDelete,
@@ -506,6 +511,7 @@ export default function TourDetailDialog({
</span>
</Tooltip>
)}
{canEdit && onSaveAsNew && <SaveAsNewButton onSave={() => onSaveAsNew(tour)} />}
<FooterSpacer />
{canEdit && onDelete && (
<DeleteButton
@@ -593,6 +599,35 @@ export default function TourDetailDialog({
);
}
/** P2C-07 in the footer: one press, the copy opens; the button waits while the server makes it. */
function SaveAsNewButton({ onSave }: { onSave: () => Promise<void> | void }) {
const { t } = useTranslation();
const [saving, setSaving] = useState(false);
const save = async () => {
if (saving) return;
setSaving(true);
try {
await onSave();
} finally {
setSaving(false);
}
};
return (
<Tooltip label={t('tours.copy.hint')}>
<span className="inline-flex">
<DialogButton
onClick={() => void save()}
disabled={saving}
icon={saving ? <Loader2 size={14} className="animate-spin" /> : <CopyPlus size={14} strokeWidth={2} />}
aria-label={t('tours.copy.action')}
>
<span className="max-sm:hidden">{t('tours.copy.action')}</span>
</DialogButton>
</span>
</Tooltip>
);
}
/** A tour's files: the count to unfold, each file to open, and an upload for who may change the tour. */
function TourFilesSection({
placeId,
@@ -5,6 +5,7 @@ import { useTourPlanner } from '../../components/Tours/planner/useTourPlanner';
import { useRecordedTrack } from '../../components/Tours/useRecordedTrack';
import { useTourGpxImport } from '../../components/Tours/useTourGpxImport';
import { useWandererBrowser } from '../../components/Tours/wanderer/useWandererBrowser';
import { tourRepo } from '../../repo/tourRepo';
import { useAddonStore } from '../../store/addonStore';
import { useTripStore } from '../../store/tripStore';
import type { Place } from '../../types';
@@ -130,6 +131,23 @@ export function useTourWorkspace(
void invalidateTourPlaceIds({ placeIds: [tour.place_id] });
planner.handlePlaceClick(tour.place_id);
},
// P2C-07: the copy opens at once, under a name that says it is one; renaming it is the next step.
onSaveAsNew: async (tour: TourListItem) => {
try {
const { tour: copy } = await tourRepo.copy(
tripId,
tour.place_id,
t('tours.copy.name', { name: tour.name }).slice(0, 255)
);
upsertTour(copy);
await useTripStore.getState().refreshPlaces(tripId);
void invalidateTourPlaceIds({ placeIds: [copy.place_id] });
planner.handlePlaceClick(copy.place_id);
toast.success(t('tours.copy.saved'));
} catch {
toast.error(t('tours.copy.failed'));
}
},
};
return {
+11
View File
@@ -1,6 +1,7 @@
import { computeTourMetrics, type GeometryPoint, type TourCreateRequest, type TourCreateResponse, type TourDetailResponse, type TourImportGpxResponse, type TourListItem, type TourWaypoint } from '@trek/shared'
import { toursApi } from '../api/client'
import { recordedTrackApi } from '../api/recordedTrack'
import { tourCopiesApi } from '../api/tourCopies'
import { offlineDb, replaceTripTours, type CachedTour } from '../db/offlineDb'
import { mutationQueue, generateUUID, nextTempId } from '../sync/mutationQueue'
import { isEffectivelyOffline } from '../sync/networkMode'
@@ -189,6 +190,16 @@ export const tourRepo = {
return result
},
/**
* P2C-07, online only like the restore: the copy is made from the server's
* stored line and control points, so it needs the server to answer.
*/
async copy(tripId: number | string, placeId: number, name: string): Promise<TourCreateResponse> {
const result = await tourCopiesApi.copy(tripId, placeId, { name })
await offlineDb.tours.put({ ...result.tour, trip_id: Number(tripId), waypoints: result.waypoints })
return result
},
async importGpx(tripId: number | string, file: File): Promise<TourImportGpxResponse> {
const result = await toursApi.importGpx(tripId, file)
await offlineDb.tours.bulkPut(result.tours.map(tour => ({ ...tour, trip_id: Number(tripId) })))