Files
ipal-kit/src/globals/SiteIntegrations/components/TestEmailButton.tsx
T
Radosław SmolińskiandClaude Opus 5.5 6b46bc0c14 Admin panel in English and Polish, reworded per docs/dev/admin-ux.md
- admin-i18n module: AdminText { en, pl }, component texts under the ipal
  namespace (project translations win), en + pl admin languages when the
  project sets none (project's supportedLanguages wins)
- every plugin label, description, option, tab and group as { en, pl };
  explicit labels instead of auto-labels; sentence case; glossary terms
- visible renames (slugs, names, data unchanged): Cookie banner, Form
  messages, Privacy group, Search engines, Maintenance, Spam protection,
  E-mail sending, stats nav link Overview (hidden without access)
- Polish visitor defaults by page locale: cookie banner, form messages,
  validators, lightbox, accessibility widget; {field} uses the label
- fixes: e-mail status banner used Turnstile wording; developer words and
  env var names out of editor messages; stats numbers/dates in the admin
  language; test e-mail no longer says ipal-kit
- test/adminUx.test.ts: length limits, en + pl present, no developer words

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 15:46:25 +01:00

87 lines
3.0 KiB
TypeScript

'use client'
import { useTranslation } from '@payloadcms/ui'
import { useState } from 'react'
import { ipalT } from '../../../modules/admin-i18n/t.js'
/**
* Admin UI: a small "send test e-mail" tool for the SiteIntegrations e-mail tab.
* Enter an address, click Send, and it POSTs to /api/ipal/test-email, which
* sends through the currently-selected transport (SMTP or Graph). Shows the
* result inline so you can confirm delivery without leaving the panel.
*
* Assigned via a `ui` field's admin.components.Field. Texts: `testEmail*` keys.
*/
export const TestEmailButton = () => {
const { t } = useTranslation()
const text = (key: Parameters<typeof ipalT>[1], vars?: Record<string, unknown>) => ipalT(t as never, key, vars)
const [to, setTo] = useState('')
const [status, setStatus] = useState<
{ kind: 'error'; msg: string } | { kind: 'idle' } | { kind: 'ok'; msg: string } | { kind: 'sending' }
>({ kind: 'idle' })
const send = async () => {
if (!to.trim()) {
setStatus({ kind: 'error', msg: text('testEmailInvalidRecipient') })
return
}
setStatus({ kind: 'sending' })
try {
const res = await fetch('/api/ipal/test-email', {
body: JSON.stringify({ to: to.trim() }),
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
method: 'POST',
})
const data = await res.json()
if (data.ok) {
setStatus({ kind: 'ok', msg: data.message ?? text('testEmailSent', { to: to.trim() }) })
} else {
setStatus({ kind: 'error', msg: data.error ?? text('testEmailFailed') })
}
} catch {
setStatus({ kind: 'error', msg: text('testEmailNoConnection') })
}
}
return (
<div className="field-type" style={{ marginTop: '1rem' }}>
<label className="field-label" htmlFor="ipal-test-email-to">{text('testEmailLabel')}</label>
<p style={{ fontSize: '.85rem', marginTop: 0, opacity: 0.7 }}>{text('testEmailHint')}</p>
<div style={{ alignItems: 'center', display: 'flex', flexWrap: 'wrap', gap: '.5rem' }}>
<input
aria-label={text('testEmailRecipient')}
id="ipal-test-email-to"
onChange={(e) => setTo(e.target.value)}
placeholder="[email protected]"
style={{ flex: 1, minWidth: '220px' }}
type="email"
value={to}
/>
<button
className="btn btn--style-secondary"
disabled={status.kind === 'sending'}
onClick={send}
style={{ whiteSpace: 'nowrap' }}
type="button"
>
{status.kind === 'sending' ? text('testEmailSending') : text('testEmailSend')}
</button>
</div>
{status.kind === 'ok' && (
<p style={{ color: 'var(--theme-success-500, green)', marginTop: '.5rem' }}>
✓ {status.msg}
</p>
)}
{status.kind === 'error' && (
<p role="alert" style={{ color: 'var(--theme-error-500, crimson)', marginTop: '.5rem' }}>
✗ {status.msg}
</p>
)}
</div>
)
}
export default TestEmailButton