Files
TREK/client/src/pages/tripPlanner/useBookingExpenseEditor.ts
T
Maurice b598b97865 refactor(planner): share the trip sheet wiring between desktop and phone
The phone trip sheet host wrote out by hand three pieces of planner
wiring the desktop planner already had.

useBookingExpenseEditor now holds the expense editor both planners open
over the trip: the booking request state, the receipt fallback, the base
currency, the current user and the save that reloads the budget.
TripPlannerPage and MTripSheets both read it.

applyTripCoverUpdate patches a new trip cover into the store for the
trip form on both sides.

The transport editor and the transit route change moved into
openTransportEditorWith and changeTransitRouteWith, which take the
planner's setters. usePlannerDialogs builds openTransportEditor and
changeTransitRoute on them, and the phone sheet calls the same helpers
on the planner it receives. There are no differences between the two
views to keep as options.
2026-10-09 15:19:51 +02:00

52 lines
2.3 KiB
TypeScript

import { useState } from 'react';
import type { ExpensePrefill } from '../../components/Budget/CostsPanel';
import { expenseEditorFor } from '../../components/Budget/CostsPanel.helpers';
import type { BookingExpenseRequest } from '../../components/Planner/BookingCostsSection.types';
import { useAuthStore } from '../../store/authStore';
import { useSettingsStore } from '../../store/settingsStore';
import { useTripStore } from '../../store/tripStore';
import type { BudgetItem } from '../../types';
interface BookingExpenseEditorOptions {
tripId: number;
/** The trip's own currency, the fallback when the user has no display currency set. */
tripCurrency: string | null | undefined;
/** A scanned receipt sent to the trip from the background tasks widget. */
receiptExpense: ExpensePrefill | null;
clearReceiptExpense: () => void;
}
/**
* The expense editor the trip planner opens over itself: from a booking's Costs block
* (save then open) and for a scanned receipt. The desktop shows it as ExpenseModal, the
* phone as MCostSheet; both read the same editor, base currency and current user here.
*/
export function useBookingExpenseEditor(options: BookingExpenseEditorOptions) {
const { tripId, tripCurrency, receiptExpense, clearReceiptExpense } = options;
const meId = useAuthStore((s) => s.user?.id ?? -1);
const displayCurrency = useSettingsStore((s) => s.settings.default_currency);
const loadBudgetItems = useTripStore((s) => s.loadBudgetItems);
const [bookingExpense, setBookingExpense] = useState<{ editing: BudgetItem | null; prefill?: ExpensePrefill } | null>(
null
);
const openBookingExpense = (req: BookingExpenseRequest) => {
if (req.editItem) setBookingExpense({ editing: req.editItem });
else if (req.prefill) setBookingExpense({ editing: null, prefill: req.prefill });
};
const costsBase = (displayCurrency || tripCurrency || 'EUR').toUpperCase();
// One expense editor for both openers: a booking's Costs block, and a scanned
// receipt sent here from the background tasks widget.
const expenseEditor = expenseEditorFor(
bookingExpense,
() => setBookingExpense(null),
receiptExpense,
clearReceiptExpense
);
const onExpenseSaved = () => {
expenseEditor?.close();
loadBudgetItems(tripId);
};
return { meId, costsBase, openBookingExpense, expenseEditor, onExpenseSaved };
}