Contact: ontvangstadres uit de site-instellingen (instelling > env), PUT /contact-settings/recipient en Berichten-veld (CMS-U11)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -51,6 +51,15 @@ export function FormSwitchCard() {
|
||||
)}
|
||||
</div>
|
||||
<p className="text-sm text-muted-foreground">{t('contact.switch.explanation')}</p>
|
||||
{settings.recipientSource === 'none' && (
|
||||
<p
|
||||
role="status"
|
||||
className="rounded-md border border-amber-500/50 bg-amber-500/10 p-2 text-sm"
|
||||
data-testid="contact-no-recipient-banner"
|
||||
>
|
||||
{t('contact.switch.noRecipientBanner')}
|
||||
</p>
|
||||
)}
|
||||
{!open && settings.hostEnabled && (
|
||||
<p className="text-sm text-muted-foreground" data-testid="contact-switch-hint">
|
||||
{t('contact.switch.firstTimeHint')}
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
import { useState, type FormEvent } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { toast } from 'sonner';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { ProblemDetailsError } from '@/lib/api-client';
|
||||
import { useContactSettings, useUpdateContactRecipient } from '../services/useContactSettings';
|
||||
import { contactErrorMessage } from '../errors';
|
||||
|
||||
// Same shape the server accepts: something@domain.tld, no spaces. The server decides; this only saves a round trip.
|
||||
const LOOKS_LIKE_AN_ADDRESS = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
|
||||
|
||||
function RecipientForm({ saved }: { saved: string }) {
|
||||
const { t } = useTranslation();
|
||||
const { data: settings } = useContactSettings();
|
||||
const update = useUpdateContactRecipient();
|
||||
const [draft, setDraft] = useState(saved);
|
||||
const [touched, setTouched] = useState(false);
|
||||
|
||||
const trimmed = draft.trim();
|
||||
const clientInvalid = trimmed.length > 0 && !LOOKS_LIKE_AN_ADDRESS.test(trimmed);
|
||||
const canSave = trimmed.length > 0 && !clientInvalid && trimmed !== saved && !update.isPending;
|
||||
const source = settings?.recipientSource ?? 'none';
|
||||
|
||||
const save = (event: FormEvent) => {
|
||||
event.preventDefault();
|
||||
setTouched(true);
|
||||
if (!canSave) return;
|
||||
update.mutate(trimmed, { onSuccess: () => toast.success(t('contact.recipient.saved')) });
|
||||
};
|
||||
|
||||
const reset = () =>
|
||||
update.mutate(null, {
|
||||
onSuccess: () => {
|
||||
setDraft('');
|
||||
toast.success(t('contact.recipient.cleared'));
|
||||
},
|
||||
});
|
||||
|
||||
const serverMessage = update.isError
|
||||
? update.error instanceof ProblemDetailsError && update.error.status === 400
|
||||
? t('contact.recipient.invalid')
|
||||
: contactErrorMessage(update.error, t)
|
||||
: null;
|
||||
|
||||
return (
|
||||
<form onSubmit={save} noValidate className="space-y-3" data-testid="contact-recipient-form">
|
||||
<div className="space-y-1">
|
||||
<label htmlFor="contact-recipient-input" className="text-sm font-medium">
|
||||
{t('contact.recipient.label')}
|
||||
</label>
|
||||
<Input
|
||||
id="contact-recipient-input"
|
||||
type="email"
|
||||
inputMode="email"
|
||||
autoComplete="off"
|
||||
value={draft}
|
||||
placeholder={t('contact.recipient.placeholder')}
|
||||
aria-invalid={touched && clientInvalid}
|
||||
aria-describedby="contact-recipient-source"
|
||||
onChange={(e) => {
|
||||
setDraft(e.target.value);
|
||||
setTouched(false);
|
||||
update.reset();
|
||||
}}
|
||||
data-testid="contact-recipient-input"
|
||||
/>
|
||||
{touched && clientInvalid && (
|
||||
<p
|
||||
role="alert"
|
||||
className="text-sm text-destructive"
|
||||
data-testid="contact-recipient-invalid"
|
||||
>
|
||||
{t('contact.recipient.invalid')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<p
|
||||
id="contact-recipient-source"
|
||||
className="text-sm text-muted-foreground"
|
||||
data-testid="contact-recipient-source"
|
||||
>
|
||||
{t(`contact.recipient.source.${source}`)}
|
||||
</p>
|
||||
|
||||
{serverMessage && (
|
||||
<p
|
||||
role="alert"
|
||||
className="text-sm text-destructive"
|
||||
data-testid="contact-recipient-error"
|
||||
>
|
||||
{serverMessage}
|
||||
</p>
|
||||
)}
|
||||
|
||||
<div className="flex flex-wrap gap-2">
|
||||
<Button
|
||||
type="submit"
|
||||
className="min-h-11"
|
||||
disabled={update.isPending || trimmed.length === 0 || trimmed === saved}
|
||||
data-testid="contact-recipient-save"
|
||||
>
|
||||
{t('contact.recipient.save')}
|
||||
</Button>
|
||||
{saved && (
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
className="min-h-11"
|
||||
disabled={update.isPending}
|
||||
onClick={reset}
|
||||
data-testid="contact-recipient-reset"
|
||||
>
|
||||
{t('contact.recipient.reset')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
/** The address contact messages are mailed to: the site setting, with the server configuration as the default. */
|
||||
export function RecipientCard() {
|
||||
const { t } = useTranslation();
|
||||
const { data: settings } = useContactSettings();
|
||||
|
||||
if (!settings) return null;
|
||||
|
||||
return (
|
||||
<Card data-testid="contact-recipient-card">
|
||||
<CardContent className="space-y-3 p-4">
|
||||
<div>
|
||||
<h2 className="font-semibold">{t('contact.recipient.title')}</h2>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
{t('contact.recipient.description')}
|
||||
</p>
|
||||
</div>
|
||||
{/* Keyed by the saved value, so the field follows what the server answered. */}
|
||||
<RecipientForm
|
||||
key={settings.recipientEmail ?? ''}
|
||||
saved={settings.recipientEmail ?? ''}
|
||||
/>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -62,11 +62,21 @@ function seed(): ContactMessageDetail[] {
|
||||
}
|
||||
|
||||
let messages = seed();
|
||||
let settings: ContactSettings = { enabled: false, hostEnabled: true };
|
||||
let settings: ContactSettings = {
|
||||
enabled: false,
|
||||
hostEnabled: true,
|
||||
recipientEmail: null,
|
||||
recipientSource: 'config',
|
||||
};
|
||||
|
||||
export function resetMockContact(): void {
|
||||
messages = seed();
|
||||
settings = { enabled: false, hostEnabled: true };
|
||||
settings = {
|
||||
enabled: false,
|
||||
hostEnabled: true,
|
||||
recipientEmail: null,
|
||||
recipientSource: 'config',
|
||||
};
|
||||
}
|
||||
|
||||
export function getMockContactMessages(): ContactMessageDetail[] {
|
||||
@@ -87,6 +97,26 @@ function toItem(m: ContactMessageDetail): ContactMessageListItem {
|
||||
|
||||
export const contactHandlers = [
|
||||
http.get(SETTINGS, () => HttpResponse.json(settings)),
|
||||
http.put(`${SETTINGS}/recipient`, async ({ request }) => {
|
||||
const body = (await request.json()) as { recipientEmail: string | null };
|
||||
const address = body.recipientEmail?.trim() || null;
|
||||
if (address && !/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(address)) {
|
||||
return HttpResponse.json(
|
||||
{
|
||||
status: 400,
|
||||
title: 'Validation failed',
|
||||
errors: { recipientEmail: ['invalid'] },
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
settings = {
|
||||
...settings,
|
||||
recipientEmail: address,
|
||||
recipientSource: address ? 'setting' : 'config',
|
||||
};
|
||||
return HttpResponse.json(settings);
|
||||
}),
|
||||
http.put(SETTINGS, async ({ request }) => {
|
||||
const body = (await request.json()) as { enabled: boolean };
|
||||
settings = { ...settings, enabled: body.enabled };
|
||||
|
||||
@@ -165,6 +165,112 @@ describe('Form switch', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Recipient address', () => {
|
||||
it('shows the field, says that the server setting applies, and never an address it was not given', async () => {
|
||||
await openPage();
|
||||
|
||||
expect(await screen.findByTestId('contact-recipient-card')).toBeInTheDocument();
|
||||
expect(screen.getByTestId('contact-recipient-input')).toHaveValue('');
|
||||
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
|
||||
'server setting applies',
|
||||
);
|
||||
expect(screen.queryByTestId('contact-recipient-reset')).not.toBeInTheDocument();
|
||||
expect(screen.queryByTestId('contact-no-recipient-banner')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('saves a valid address, shows where it comes from and offers to go back', async () => {
|
||||
await openPage();
|
||||
const input = await screen.findByTestId('contact-recipient-input');
|
||||
|
||||
await userEvent.type(input, ' inbox@example.org ');
|
||||
await userEvent.click(screen.getByTestId('contact-recipient-save'));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
|
||||
'Messages go to this address',
|
||||
),
|
||||
);
|
||||
expect(screen.getByTestId('contact-recipient-input')).toHaveValue('inbox@example.org');
|
||||
expect(screen.getByTestId('contact-recipient-reset')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('does not save an invalid address (client side), and says so', async () => {
|
||||
await openPage();
|
||||
const input = await screen.findByTestId('contact-recipient-input');
|
||||
|
||||
await userEvent.type(input, 'not-an-address');
|
||||
await userEvent.click(screen.getByTestId('contact-recipient-save'));
|
||||
|
||||
expect(await screen.findByTestId('contact-recipient-invalid')).toHaveTextContent(
|
||||
'valid e-mail address',
|
||||
);
|
||||
});
|
||||
|
||||
it('shows a translated message when the server refuses the address', async () => {
|
||||
await openPage();
|
||||
server.use(
|
||||
http.put(`${API_BASE}/api/v1/contact-settings/recipient`, () =>
|
||||
HttpResponse.json(
|
||||
{
|
||||
status: 400,
|
||||
title: 'Validation failed',
|
||||
errors: { recipientEmail: ['invalid'] },
|
||||
},
|
||||
{ status: 400 },
|
||||
),
|
||||
),
|
||||
);
|
||||
await userEvent.type(
|
||||
await screen.findByTestId('contact-recipient-input'),
|
||||
'jan@example.org',
|
||||
);
|
||||
await userEvent.click(screen.getByTestId('contact-recipient-save'));
|
||||
|
||||
const error = await screen.findByTestId('contact-recipient-error');
|
||||
expect(error).toHaveTextContent('valid e-mail address');
|
||||
expect(error).not.toHaveTextContent('Validation failed');
|
||||
});
|
||||
|
||||
it('goes back to the server setting when cleared', async () => {
|
||||
await openPage();
|
||||
await userEvent.type(
|
||||
await screen.findByTestId('contact-recipient-input'),
|
||||
'inbox@example.org',
|
||||
);
|
||||
await userEvent.click(screen.getByTestId('contact-recipient-save'));
|
||||
await userEvent.click(await screen.findByTestId('contact-recipient-reset'));
|
||||
|
||||
await waitFor(() =>
|
||||
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
|
||||
'server setting applies',
|
||||
),
|
||||
);
|
||||
expect(screen.getByTestId('contact-recipient-input')).toHaveValue('');
|
||||
});
|
||||
|
||||
it('warns (but does not block the switch) while there is no address at all', async () => {
|
||||
server.use(
|
||||
http.get(`${API_BASE}/api/v1/contact-settings`, () =>
|
||||
HttpResponse.json({
|
||||
enabled: false,
|
||||
hostEnabled: true,
|
||||
recipientEmail: null,
|
||||
recipientSource: 'none',
|
||||
}),
|
||||
),
|
||||
);
|
||||
await openPage();
|
||||
|
||||
expect(await screen.findByTestId('contact-no-recipient-banner')).toHaveTextContent(
|
||||
'No recipient address is set',
|
||||
);
|
||||
expect(screen.getByTestId('contact-recipient-source')).toHaveTextContent(
|
||||
'no recipient address yet',
|
||||
);
|
||||
expect(screen.getByTestId('contact-switch-toggle')).not.toBeDisabled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Message list', () => {
|
||||
it('shows the inbox without spam and the unread counter', async () => {
|
||||
await openPage();
|
||||
|
||||
@@ -3,6 +3,7 @@ import { useTranslation } from 'react-i18next';
|
||||
import { Mail } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { FormSwitchCard } from '../components/FormSwitchCard';
|
||||
import { RecipientCard } from '../components/RecipientCard';
|
||||
import { MessagesFilterChips } from '../components/MessagesFilterChips';
|
||||
import { MessagesTable } from '../components/MessagesTable';
|
||||
import { MessageDialog } from '../components/MessageDialog';
|
||||
@@ -38,6 +39,8 @@ export function MessagesPage() {
|
||||
|
||||
<FormSwitchCard />
|
||||
|
||||
<RecipientCard />
|
||||
|
||||
<MessagesFilterChips
|
||||
value={filter}
|
||||
unreadCount={data?.unreadCount}
|
||||
|
||||
@@ -42,6 +42,10 @@ export interface ContactSettings {
|
||||
/** The CMS switch: whether the form on the website is open. */
|
||||
enabled: boolean;
|
||||
hostEnabled: boolean;
|
||||
/** The address chosen in the CMS, or null. The address from the server configuration is never sent. */
|
||||
recipientEmail?: string | null;
|
||||
/** Where mail goes: the CMS setting, the server configuration (default) or nowhere. */
|
||||
recipientSource?: 'setting' | 'config' | 'none';
|
||||
}
|
||||
|
||||
export const PAGE_SIZE = 25;
|
||||
|
||||
@@ -25,6 +25,16 @@ export function useContactSettings(options?: { enabled?: boolean }) {
|
||||
});
|
||||
}
|
||||
|
||||
/** Sets the address contact mails go to; null clears it (back to the server configuration). */
|
||||
export function useUpdateContactRecipient() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<ContactSettings, Error, string | null>({
|
||||
mutationFn: (recipientEmail) =>
|
||||
api.put<ContactSettings>('/api/v1/contact-settings/recipient', { recipientEmail }),
|
||||
onSuccess: (settings) => queryClient.setQueryData(CONTACT_SETTINGS_KEY, settings),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateContactSettings() {
|
||||
const queryClient = useQueryClient();
|
||||
return useMutation<ContactSettings, Error, boolean>({
|
||||
|
||||
@@ -431,7 +431,8 @@
|
||||
"nowOff": "The contact form is now off.",
|
||||
"explanation": "While the form is off, visitors only see your e-mail address. Turning it off helps against spam or during maintenance.",
|
||||
"firstTimeHint": "The form stays closed until you turn it on.",
|
||||
"hostOff": "The form is switched off at server level."
|
||||
"hostOff": "The form is switched off at server level.",
|
||||
"noRecipientBanner": "No recipient address is set: messages are kept but not mailed. Enter an address below."
|
||||
},
|
||||
"filter": {
|
||||
"label": "Filter messages",
|
||||
@@ -515,6 +516,22 @@
|
||||
"tls_failed": "The secure connection to the mail server failed.",
|
||||
"connection_failed": "Cannot reach the mail server.",
|
||||
"unknown": "Unknown error while sending."
|
||||
},
|
||||
"recipient": {
|
||||
"title": "Recipient address for contact messages",
|
||||
"description": "We mail every new message to this address. You can reply to that mail directly: the reply goes to the visitor.",
|
||||
"label": "E-mail address",
|
||||
"placeholder": "name@example.com",
|
||||
"save": "Save",
|
||||
"reset": "Back to the server setting",
|
||||
"saved": "Recipient address saved.",
|
||||
"cleared": "The server setting applies again.",
|
||||
"invalid": "Enter a valid e-mail address.",
|
||||
"source": {
|
||||
"setting": "Messages go to this address.",
|
||||
"config": "No address of your own is set: the server setting applies.",
|
||||
"none": "There is no recipient address yet: messages are kept but not mailed."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,7 +431,8 @@
|
||||
"nowOff": "Het contactformulier staat nu uit.",
|
||||
"explanation": "Als het formulier uit staat, ziet de bezoeker alleen je e-mailadres. Uitzetten helpt bij spam of onderhoud.",
|
||||
"firstTimeHint": "Het formulier is nog gesloten tot je het aanzet.",
|
||||
"hostOff": "Het formulier is op serverniveau uitgeschakeld."
|
||||
"hostOff": "Het formulier is op serverniveau uitgeschakeld.",
|
||||
"noRecipientBanner": "Er is geen ontvangstadres ingesteld: berichten worden bewaard maar niet gemaild. Vul hieronder een adres in."
|
||||
},
|
||||
"filter": {
|
||||
"label": "Filter berichten",
|
||||
@@ -515,6 +516,22 @@
|
||||
"tls_failed": "De beveiligde verbinding met de mailserver is mislukt.",
|
||||
"connection_failed": "Geen verbinding met de mailserver.",
|
||||
"unknown": "Onbekende fout bij het versturen."
|
||||
},
|
||||
"recipient": {
|
||||
"title": "Ontvangstadres contactberichten",
|
||||
"description": "Hierheen sturen we een mail bij elk nieuw bericht. Je kunt direct op die mail antwoorden: het antwoord gaat naar de bezoeker.",
|
||||
"label": "E-mailadres",
|
||||
"placeholder": "naam@voorbeeld.nl",
|
||||
"save": "Opslaan",
|
||||
"reset": "Terug naar serverinstelling",
|
||||
"saved": "Ontvangstadres opgeslagen.",
|
||||
"cleared": "De serverinstelling geldt weer.",
|
||||
"invalid": "Voer een geldig e-mailadres in.",
|
||||
"source": {
|
||||
"setting": "Berichten gaan naar dit adres.",
|
||||
"config": "Er is geen eigen adres ingesteld: de serverinstelling geldt.",
|
||||
"none": "Er is nog geen ontvangstadres: berichten worden bewaard maar niet gemaild."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user