mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 14:17:31 +00:00
fix(mail-ingest): set an inbox entry aside once its booking is saved through Fill in yourself
The entry stayed pending after the prefilled booking was saved, kept its Add action and counted in the badge, so it could be added a second time.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
// FE-MAIL-PREFILL-001 to FE-MAIL-PREFILL-002: "Fill in yourself" opens the booking form in the trip.
|
||||
// FE-MAIL-PREFILL-001 to FE-MAIL-PREFILL-003: "Fill in yourself" opens the booking form in the trip.
|
||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||
import type { BookingImportPreviewItem } from '@trek/shared';
|
||||
import { createElement, type ReactNode } from 'react';
|
||||
import { MemoryRouter, useSearchParams } from 'react-router';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { TranslationProvider } from '../../i18n/TranslationContext';
|
||||
import { useMailIngestStore } from '../../store/mailIngestStore';
|
||||
|
||||
const repo = vi.hoisted(() => ({ prefill: vi.fn() }));
|
||||
const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() }));
|
||||
@@ -56,4 +57,19 @@ describe('useMailPrefillLink', () => {
|
||||
await waitFor(() => expect(startImportReview).toHaveBeenCalledTimes(2));
|
||||
expect(result.current.searchParams.get('mailPrefill')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-MAIL-PREFILL-003: a saved booking sets the entry aside, and a refused one reads the inbox again', async () => {
|
||||
const act$ = vi.fn().mockResolvedValue({});
|
||||
const loadInbox = vi.fn().mockResolvedValue(undefined);
|
||||
useMailIngestStore.setState({ act: act$, loadInbox });
|
||||
const { startImportReview } = setup();
|
||||
await waitFor(() => expect(startImportReview).toHaveBeenCalledTimes(1));
|
||||
const onSaved = startImportReview.mock.calls[0][3] as () => void;
|
||||
onSaved();
|
||||
expect(act$).toHaveBeenCalledWith(12, 'discard');
|
||||
expect(loadInbox).not.toHaveBeenCalled();
|
||||
act$.mockRejectedValueOnce(new Error('decided already'));
|
||||
onSaved();
|
||||
await waitFor(() => expect(loadInbox).toHaveBeenCalledTimes(1));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,14 +3,16 @@ import { useEffect, useRef } from 'react';
|
||||
import type { SetURLSearchParams } from 'react-router';
|
||||
import { useTranslation } from '../../i18n';
|
||||
import { mailIngestRepo } from '../../repo/mailIngestRepo';
|
||||
import { useMailIngestStore } from '../../store/mailIngestStore';
|
||||
import { useToast } from '../shared/Toast';
|
||||
|
||||
/**
|
||||
* "Fill in yourself" from the mail inbox lands in the trip as ?mailPrefill=<id>:
|
||||
* once the trip has loaded, the booking form opens with what the mail held, the
|
||||
* same per-item review a file import uses, and the parameter leaves the address
|
||||
* so a reload does not open it again. Desktop and phone share the planner hook
|
||||
* this runs in.
|
||||
* so a reload does not open it again. Once the booking is saved the entry is set
|
||||
* aside, so it leaves the inbox instead of offering to add it a second time.
|
||||
* Desktop and phone share the planner hook this runs in.
|
||||
*/
|
||||
export function useMailPrefillLink({
|
||||
searchParams,
|
||||
@@ -21,7 +23,12 @@ export function useMailPrefillLink({
|
||||
searchParams: URLSearchParams;
|
||||
setSearchParams: SetURLSearchParams;
|
||||
loaded: boolean;
|
||||
startImportReview: (items: BookingImportPreviewItem[], files?: File[], kind?: 'transports' | 'bookings') => void;
|
||||
startImportReview: (
|
||||
items: BookingImportPreviewItem[],
|
||||
files?: File[],
|
||||
kind?: 'transports' | 'bookings',
|
||||
onSaved?: () => void
|
||||
) => void;
|
||||
}): void {
|
||||
const { t } = useTranslation();
|
||||
const toast = useToast();
|
||||
@@ -48,7 +55,11 @@ export function useMailPrefillLink({
|
||||
mailIngestRepo
|
||||
.prefill(id)
|
||||
.then(({ item }) => {
|
||||
startImportReview([item], [], isTransportReservationType(item.type) ? 'transports' : 'bookings');
|
||||
startImportReview([item], [], isTransportReservationType(item.type) ? 'transports' : 'bookings', () => {
|
||||
const inbox = useMailIngestStore.getState();
|
||||
// Decided elsewhere meanwhile, or offline: a fresh read shows what is left.
|
||||
void inbox.act(id, 'discard').catch(() => inbox.loadInbox());
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
// An entry decided meanwhile has nothing to prefill: the trip stays open, and the person hears why.
|
||||
|
||||
Reference in New Issue
Block a user