Files
TREK/client/src/components/Planner/useLegModeActions.ts
T
Maurice bdd8c08dd4 fix(planner): places put on a day, day names, day notes and trip details changed offline wait in the queue and sync once the connection is back
The day plan wrote straight to the API, so offline a place dropped on a day
snapped back with "Error assigning place", a renamed day or a new day note
failed, and a moved stop, a leg's travel mode or who joins a visit were lost.
Visits (put on a day, taken off, reordered, moved, travellers, leg mode), the
day's name, notes text and travel mode, and the day notes (add, edit, delete,
move) now go through assignmentRepo, dayRepo and dayNoteRepo and the mutation
queue: the cached day changes at once, a visit or note made offline gets a
temporary id that the edits after it wait for, and an online write held behind
an older queued one joins the queue, since none of these carry a version
token. A new order of a day that names a visit made offline is sent with its
real id, or without it when its create failed. Desktop, the phone timeline and
note sheet all write through the store, so both are covered.

The replay reads each answer for what it names: the day answer keeps the
cached notes and visits, the participants answer lands on its visit, and a
visit the server refuses with 409 (a Tour already on that day) fails instead
of waiting as a conflict no retry can solve.

The trip's name, description, reminder and the archive flag wait in the queue
too, from the planner, the phone and the dashboard. What only the server can
do says so instead of failing: new dates, a day count, a currency or a cover,
a new trip, a copy or a delete, members, guests, share and invite links, and
a new order of the days, which moves the dates of the bookings.

Refs #2601
2026-10-12 20:17:57 +02:00

68 lines
2.5 KiB
TypeScript

import { type LucideIcon, RotateCcw, TramFront } from 'lucide-react';
import { useCallback } from 'react';
import { assignmentRepo } from '../../repo/assignmentRepo';
import type { TripStoreState } from '../../store/tripStore';
import { type RouteModeOption, routeModeIcon, useRouteModeOptions } from './routeModes';
type Translate = (key: string, params?: Record<string, string | number>) => string;
/** One entry of the connector menu, in the shape the context menu draws. */
export interface LegModeMenuItem {
label?: string;
icon?: LucideIcon;
onClick?: () => void;
divider?: boolean;
}
/**
* The menu a leg's connector opens (#1281): every route profile, public transit
* for the leg when it can be searched (#2398), and the day default, which clears
* the leg's own mode again.
*/
export function legModeMenuItems(
options: RouteModeOption[],
pick: (mode: string | null) => void,
t: Translate,
planTransit?: () => void
): LegModeMenuItem[] {
return [
...options.map((o) => ({ label: o.label, icon: routeModeIcon(o.key), onClick: () => pick(o.key) })),
...(planTransit ? [{ label: t('transit.title'), icon: TramFront, onClick: planTransit }] : []),
{ divider: true },
{ label: t('dayplan.transportMode.useDefault'), icon: RotateCcw, onClick: () => pick(null) },
];
}
export interface LegModeDeps {
tripId: number;
toast: { error: (message: string) => void };
t: Translate;
tripActions: Pick<TripStoreState, 'refreshDays'>;
}
/**
* The per-leg travel mode behind both the desktop day plan and the phone timeline.
* Each side shows a new mode at once its own way; `persistLegMode` then saves it for
* the leg leaving a stop, or with 'incoming' for the one entering it. A refused write
* says why and reloads the days, so the shown mode goes back.
*/
export function useLegModeActions({ tripId, toast, t, tripActions }: LegModeDeps) {
const routeModeOptions = useRouteModeOptions();
const persistLegMode = useCallback(
(assignmentId: number, mode: string | null, direction?: 'incoming') => {
assignmentRepo.setLegMode(tripId, assignmentId, mode, direction).catch((err: unknown) => {
toast.error(err instanceof Error ? err.message : t('common.unknownError'));
void tripActions.refreshDays(tripId);
});
},
[tripId, toast, t, tripActions]
);
const legModeMenu = (pick: (mode: string | null) => void, planTransit?: () => void) =>
legModeMenuItems(routeModeOptions, pick, t, planTransit);
return { routeModeOptions, legModeMenu, persistLegMode };
}