fix(planner): a create expense click that saves nothing no longer opens the editor on the next save

In the desktop booking and transport dialogs, clicking "Create expense"
(or editing a linked expense) before the booking had a title saved
nothing, which is right, but the wish was kept. The next ordinary save
then opened the expense editor out of nowhere. The same happened after a
save that threw.

useBookingExpenseIntent stored the wish in a ref and only cleared it when
the form read it, which the desktop forms do after a successful save. The
hook now clears the wish once the save its click started has settled,
unless a newer click replaced it in the meantime. The phone sheets read
it at the start of the save and are unaffected.

Red tests:
- FE-PLANNER-RESMODAL-107 in ReservationModal.test.tsx: "expected
  "vi.fn()" to not be called at all, but actually been called 1 times".
- FE-PLANNER-BOOKINGEXPENSE-005 in useBookingExpenseIntent.test.ts:
  "expected { create: true } to be null".
- FE-PLANNER-BOOKINGEXPENSE-006: "expected { editItem: { id: 9, ... } }
  to be null".

No existing assertions changed.
This commit is contained in:
Maurice
2026-10-09 17:43:24 +02:00
parent 70f4f7cef3
commit 1e674f040a
3 changed files with 59 additions and 10 deletions
@@ -20,6 +20,16 @@ export interface BookingExpenseRemoval {
export function useBookingExpenseIntent(submit: () => Promise<unknown>, removal?: BookingExpenseRemoval) {
const intentRef = useRef<BookingExpenseIntent | null>(null);
// The wish belongs to the save its click starts. A save that returns early
// (no title) or throws never reads it, and it must not open the editor on
// the next, unrelated save. A newer click's wish is left alone.
const saveWith = (intent: BookingExpenseIntent) => {
intentRef.current = intent;
void submit().finally(() => {
if (intentRef.current === intent) intentRef.current = null;
});
};
return {
/** Forgets a wish left from an earlier opening. */
reset: () => {
@@ -31,15 +41,9 @@ export function useBookingExpenseIntent(submit: () => Promise<unknown>, removal?
intentRef.current = null;
return intent;
},
create: () => {
intentRef.current = { create: true };
void submit();
},
create: () => saveWith({ create: true }),
/** Saves the booking first, then opens the linked expense. */
edit: (item: BudgetItem) => {
intentRef.current = { editItem: item };
void submit();
},
edit: (item: BudgetItem) => saveWith({ editItem: item }),
remove: async (item: BudgetItem) => {
if (!removal) return;
try {