From e459ec6e604855f4c993a200cd8065c026c048f3 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:59:25 +0100 Subject: [PATCH] fix(mail-ingest): ask before closing the booking mail setup with changes not saved The setup only keeps its fields through its own Save, yet Close, the close mark, the backdrop and the phone sheet dropped them without a word. Now each way out asks, and discarding puts the saved values back. --- .../src/components/Admin/AddonSetupDialog.tsx | 5 +- .../Admin/mailIngest/MailIngestAddonTile.tsx | 15 ++++- .../mailIngest/useMailIngestAdmin.test.ts | 55 +++++++++++++++++++ .../Admin/mailIngest/useMailIngestAdmin.ts | 26 ++++++++- .../screens/admin/MMailIngestAdminCard.tsx | 30 ++++++++-- 5 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 client/src/components/Admin/mailIngest/useMailIngestAdmin.test.ts diff --git a/client/src/components/Admin/AddonSetupDialog.tsx b/client/src/components/Admin/AddonSetupDialog.tsx index abe25bb94..133ed1cfb 100644 --- a/client/src/components/Admin/AddonSetupDialog.tsx +++ b/client/src/components/Admin/AddonSetupDialog.tsx @@ -12,8 +12,9 @@ import { /** * The dialog an addon's setup row opens: the icon tile, the addon it belongs - * to above the title, what it sets up below, and one Close, since everything - * inside saves on its own (booking mails, paddle water data). + * to above the title, what it sets up below, and one Close. The paddle water + * data saves on its own; the booking mail setup has its own Save and asks + * before a way out drops what it holds unsaved. */ export default function AddonSetupDialog({ icon, diff --git a/client/src/components/Admin/mailIngest/MailIngestAddonTile.tsx b/client/src/components/Admin/mailIngest/MailIngestAddonTile.tsx index 01bd41b3f..d317a8652 100644 --- a/client/src/components/Admin/mailIngest/MailIngestAddonTile.tsx +++ b/client/src/components/Admin/mailIngest/MailIngestAddonTile.tsx @@ -6,6 +6,7 @@ import { copyText } from '../../../utils/clipboard'; import { relativeTime } from '../../../utils/relativeTime'; import { HintMark, SETTINGS_BUTTON, SETTINGS_BUTTON_PRIMARY, StatusPill } from '../../Settings/settingsKit'; import ToggleSwitch from '../../Settings/ToggleSwitch'; +import ConfirmDialog from '../../shared/ConfirmDialog'; import CustomSelect from '../../shared/CustomSelect'; import { EditorField, GRID_2, INPUT, READONLY_BOX } from '../../shared/dialogParts'; import { useToast } from '../../shared/Toast'; @@ -312,6 +313,7 @@ export default function MailIngestAddonTile() { const admin = useMailIngestAdmin(true); const [open, setOpen] = useState(false); const name = t('mailAdmin.title'); + const close = () => admin.leaving.requestClose(() => setOpen(false)); return ( <> setOpen(false)} + onClose={close} + blocked={admin.leaving.asking} > )} + ); } diff --git a/client/src/components/Admin/mailIngest/useMailIngestAdmin.test.ts b/client/src/components/Admin/mailIngest/useMailIngestAdmin.test.ts new file mode 100644 index 000000000..25638c3cc --- /dev/null +++ b/client/src/components/Admin/mailIngest/useMailIngestAdmin.test.ts @@ -0,0 +1,55 @@ +// FE-ADMIN-MAILINGEST-001: leaving the booking mail setup with changes not saved. +import { act, renderHook, waitFor } from '@testing-library/react'; +import { createElement, type ReactNode } from 'react'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { TranslationProvider } from '../../../i18n/TranslationContext'; + +const repo = vi.hoisted(() => ({ + adminSettings: vi.fn(), + adminStatus: vi.fn(), + saveAdminSettings: vi.fn(), + adminTest: vi.fn(), +})); +const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() })); +vi.mock('../../../repo/mailIngestRepo', () => ({ mailIngestRepo: repo })); +vi.mock('../../shared/Toast', () => ({ useToast: () => toast })); + +const { useMailIngestAdmin } = await import('./useMailIngestAdmin'); + +const wrapper = ({ children }: { children: ReactNode }) => createElement(TranslationProvider, null, children); + +beforeEach(() => { + vi.clearAllMocks(); + repo.adminSettings.mockResolvedValue({ mail_ingest_enabled: 'true', mail_ingest_imap_host: 'mail.example.com' }); + repo.adminStatus.mockResolvedValue(null); + repo.saveAdminSettings.mockResolvedValue({}); +}); + +describe('useMailIngestAdmin', () => { + it('FE-ADMIN-MAILINGEST-001: a way out with changes not saved asks, and discarding puts the saved values back', async () => { + const { result } = renderHook(() => useMailIngestAdmin(true), { wrapper }); + await waitFor(() => expect(result.current.loaded).toBe(true)); + + const close = vi.fn(); + act(() => result.current.leaving.requestClose(close)); + expect(close).toHaveBeenCalledTimes(1); + expect(result.current.leaving.asking).toBe(false); + + act(() => result.current.set('imap_host', 'imap.example.com')); + act(() => result.current.leaving.requestClose(close)); + expect(close).toHaveBeenCalledTimes(1); + expect(result.current.leaving.asking).toBe(true); + + act(() => result.current.leaving.keepEditing()); + expect(result.current.leaving.asking).toBe(false); + expect(result.current.form?.imap_host).toBe('imap.example.com'); + + act(() => result.current.leaving.requestClose(close)); + act(() => result.current.leaving.discard()); + expect(close).toHaveBeenCalledTimes(2); + expect(result.current.leaving.asking).toBe(false); + expect(result.current.form?.imap_host).toBe('mail.example.com'); + expect(result.current.dirty).toBe(false); + expect(repo.saveAdminSettings).not.toHaveBeenCalled(); + }); +}); diff --git a/client/src/components/Admin/mailIngest/useMailIngestAdmin.ts b/client/src/components/Admin/mailIngest/useMailIngestAdmin.ts index 5f1390cd9..37e8a675c 100644 --- a/client/src/components/Admin/mailIngest/useMailIngestAdmin.ts +++ b/client/src/components/Admin/mailIngest/useMailIngestAdmin.ts @@ -1,5 +1,5 @@ import type { MailIngestAdminStatusResponse, MailIngestAdminTestResponse } from '@trek/shared'; -import { useCallback, useEffect, useState } from 'react'; +import { useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from '../../../i18n'; import { mailIngestRepo } from '../../../repo/mailIngestRepo'; import { getApiErrorMessage } from '../../../types'; @@ -14,6 +14,28 @@ import { type MailAdminForm, } from './mailIngestAdminModel'; +/** + * The setup's own Save keeps the transport fields, so a way out of the setup + * with changes not saved asks first, and throwing them away puts the saved + * values back. + */ +function useLeaveSetup(dirty: boolean, revert: () => void) { + const [asking, setAsking] = useState(false); + const leave = useRef<(() => void) | null>(null); + const requestClose = (close: () => void) => { + if (!dirty) return close(); + leave.current = close; + setAsking(true); + }; + const discard = () => { + setAsking(false); + revert(); + leave.current?.(); + leave.current = null; + }; + return { asking, requestClose, keepEditing: () => setAsking(false), discard, revert }; +} + /** * The admin card of the booking mail import, desktop and phone alike: the * instance switch (saved at once, as every switch in the admin settings), the @@ -76,6 +98,7 @@ export function useMailIngestAdmin(active: boolean) { const patch = form && saved ? changesOf(form, saved) : {}; const problems = form ? problemsOf(form) : []; + const leaving = useLeaveSetup(Object.keys(patch).length > 0, () => setForm(saved)); const save = async () => { if (!form || Object.keys(patch).length === 0 || problems.length > 0) return; @@ -118,6 +141,7 @@ export function useMailIngestAdmin(active: boolean) { test, testing, runTest: () => void runTest(), + leaving, }; } diff --git a/client/src/mobile/screens/admin/MMailIngestAdminCard.tsx b/client/src/mobile/screens/admin/MMailIngestAdminCard.tsx index bd1633158..1111064e6 100644 --- a/client/src/mobile/screens/admin/MMailIngestAdminCard.tsx +++ b/client/src/mobile/screens/admin/MMailIngestAdminCard.tsx @@ -18,6 +18,7 @@ import { relativeTime } from '../../../utils/relativeTime'; import MSegmented from '../../components/MSegmented'; import MSheet from '../../components/MSheet'; import MToggle from '../../components/MToggle'; +import MConfirmSheet from '../settings/MConfirmSheet'; import { MAdminButton, MAdminField, @@ -262,6 +263,7 @@ export default function MMailIngestAdminCard({ first = false }: { first?: boolea const admin = useMailIngestAdmin(true); const [open, setOpen] = useState(false); const setupLabel = t('mailAdmin.setup'); + const close = () => admin.leaving.requestClose(() => setOpen(false)); return ( <> {admin.enabled && admin.form && setOpen(true)} />} {open && admin.enabled && admin.form && ( - setOpen(false)} variant="bottom" material="glass" ariaLabel={setupLabel}> - } - title={setupLabel} - onClose={() => setOpen(false)} - > + { + admin.leaving.revert(); + setOpen(false); + }} + variant="bottom" + material="glass" + ariaLabel={setupLabel} + discardGuard={admin.dirty} + > + } title={setupLabel} onClose={close}> )} + ); }