Files
TREK/client/src/pages/tripPlanner/useTourDetailActions.ts
T
Maurice 692373c8b8 feat(tours): take a tour over from another trip, the dialog (LIB-02)
The Tours tab's list of the trip's tours gains a button beside the GPX
import: take a tour over from another trip. Its dialog lists the tours of
the user's other trips, searched by name and filtered by trip and type,
each with its trip and the measures it has (time, distance, ascent; one it
lacks is left out, a paddle tour's climb too). Choosing one shows it on a
map with where it comes from, its type, distance, time, heights and number
of waypoints, and the name it gets in this trip, its own name to start
with. The note under the name says the copy is a tour of this trip of its
own, on no day yet, and that later changes stay apart.

Only the button in the foot takes it over. The new tour then comes into
the trip's lists and map and opens in the editor, unless the editor holds
unsaved work, which it never replaces. Nothing is written before the
confirm, and closing writes nothing. The library is online only: offline,
or when the list fails, the dialog says so and offers to try again; a list
cut at 100 tours says to search or filter.

A tour taken over names its source in its provenance: "Taken over from"
with the trip and the tour as they were called then.
2026-10-19 15:28:53 +02:00

48 lines
1.8 KiB
TypeScript

import type { TourListItem } from '@trek/shared';
import { tourRepo } from '../../repo/tourRepo';
import { useTripStore } from '../../store/tripStore';
import type { Place } from '../../types';
import type { useTripPlanner } from './useTripPlanner';
/**
* What the tour's detail does beyond a place's: a rename reloads the tours,
* a reimport from its source check (MF-03) takes the new snapshot in and opens
* it, and "save as new tour" (P2C-07) makes the copy and opens it under a name
* that says it is one, so renaming it is the next step.
*/
export function useTourDetailActions(planner: ReturnType<typeof useTripPlanner>) {
const { invalidateTourPlaceIds, reloadTourPlaceIds, t, toast, tripActions, tripId, upsertTour } = planner;
/** A tour that just came in: in the lists and on the map. */
const bringIn = async (tour: TourListItem) => {
upsertTour(tour);
await useTripStore.getState().refreshPlaces(tripId);
void invalidateTourPlaceIds({ placeIds: [tour.place_id] });
};
/** ...and its detail open. */
const takeIn = async (tour: TourListItem) => {
await bringIn(tour);
planner.handlePlaceClick(tour.place_id);
};
return {
onUpdatePlace: async (placeId: number, data: Partial<Place>) => {
await tripActions.updatePlace(tripId, placeId, data);
await reloadTourPlaceIds();
},
onSourceReimported: takeIn,
/** LIB-02: a tour taken over from another trip; the tours rail opens it in the planner. */
onTakenOver: bringIn,
onSaveAsNew: async (tour: TourListItem) => {
try {
const name = t('tours.copy.name', { name: tour.name }).slice(0, 255);
const { tour: copy } = await tourRepo.copy(tripId, tour.place_id, name);
await takeIn(copy);
toast.success(t('tours.copy.saved'));
} catch {
toast.error(t('tours.copy.failed'));
}
},
};
}