Phase 6.6: Mail-Settings-Forms on RHF + Zod
- MailSettings account form: RHF+Zod (email valid, password required, imap/smtp host required, ports numeric) - SignatureManager: RHF+Zod (name required, body_html, is_default) - RuleEditor: RHF+Zod (name required, priority numeric) - LabelManager: RHF+Zod (name required, color optional) - VacationResponder: RHF+Zod (enabled, dates with end>start validation, subject, body) - Error display under each field - Preserved all existing functionality: CRUD, templates, variables - Added 2 validation tests for MailSettings account form
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
|
||||
import React, { useState, useEffect, useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { z } from 'zod';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Input } from '@/components/ui/Input';
|
||||
import { Card } from '@/components/ui/Card';
|
||||
@@ -41,12 +44,24 @@ export function SignatureManager() {
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [editing, setEditing] = useState<MailSignature | null>(null);
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [name, setName] = useState('');
|
||||
const [bodyHtml, setBodyHtml] = useState('');
|
||||
const [isDefault, setIsDefault] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [deleteTarget, setDeleteTarget] = useState<MailSignature | null>(null);
|
||||
|
||||
// ── Signature form (RHF + Zod) ──
|
||||
const sigSchema = z.object({
|
||||
name: z.string().min(1, 'required'),
|
||||
body_html: z.string().default(''),
|
||||
is_default: z.boolean().default(false),
|
||||
});
|
||||
type SigFormData = z.infer<typeof sigSchema>;
|
||||
|
||||
const { register: registerSig, handleSubmit: handleSubmitSig, reset: resetSig, watch: watchSig, setValue: setSigValue, formState: { errors: sigErrors } } = useForm<SigFormData>({
|
||||
resolver: zodResolver(sigSchema),
|
||||
defaultValues: { name: '', body_html: '', is_default: false },
|
||||
});
|
||||
|
||||
const bodyHtmlValue = watchSig('body_html');
|
||||
|
||||
const load = useCallback(() => {
|
||||
setLoading(true);
|
||||
fetchSignatures()
|
||||
@@ -64,30 +79,25 @@ export function SignatureManager() {
|
||||
|
||||
const handleNew = () => {
|
||||
setEditing(null);
|
||||
setName('');
|
||||
setBodyHtml('');
|
||||
setIsDefault(false);
|
||||
resetSig({ name: '', body_html: '', is_default: false });
|
||||
setShowForm(true);
|
||||
};
|
||||
|
||||
const handleEdit = (sig: MailSignature) => {
|
||||
setEditing(sig);
|
||||
setName(sig.name);
|
||||
setBodyHtml(sig.body_html);
|
||||
setIsDefault(sig.is_default);
|
||||
resetSig({ name: sig.name, body_html: sig.body_html, is_default: sig.is_default });
|
||||
setShowForm(true);
|
||||
};
|
||||
|
||||
const handleSave = useCallback(async () => {
|
||||
if (!name.trim()) return;
|
||||
const handleSave = useCallback(async (data: SigFormData) => {
|
||||
setSaving(true);
|
||||
try {
|
||||
if (editing) {
|
||||
const updated = await updateSignature(editing.id, { name, body_html: bodyHtml, is_default: isDefault });
|
||||
const updated = await updateSignature(editing.id, { name: data.name, body_html: data.body_html, is_default: data.is_default });
|
||||
setSignatures((prev) => prev.map((s) => (s.id === editing.id ? updated : s)));
|
||||
toast.success(t('mail.signatureUpdated'));
|
||||
} else {
|
||||
const created = await createSignature({ name, body_html: bodyHtml, is_default: isDefault });
|
||||
const created = await createSignature({ name: data.name, body_html: data.body_html, is_default: data.is_default });
|
||||
setSignatures((prev) => [...prev, created]);
|
||||
toast.success(t('mail.signatureCreated'));
|
||||
}
|
||||
@@ -97,7 +107,7 @@ export function SignatureManager() {
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}, [editing, name, bodyHtml, isDefault, toast, t]);
|
||||
}, [editing, toast, t]);
|
||||
|
||||
const handleDelete = useCallback(async () => {
|
||||
if (!deleteTarget) return;
|
||||
@@ -136,11 +146,11 @@ export function SignatureManager() {
|
||||
|
||||
{showForm && (
|
||||
<Card className="mb-4" data-testid="signature-form">
|
||||
<div className="space-y-3">
|
||||
<form onSubmit={handleSubmitSig(handleSave)} className="space-y-3">
|
||||
<Input
|
||||
label={t('mail.signatureName')}
|
||||
value={name}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
{...registerSig('name')}
|
||||
error={sigErrors.name?.message === 'required' ? t('validation.required') : undefined}
|
||||
placeholder={t('mail.signatureName')}
|
||||
required
|
||||
/>
|
||||
@@ -152,7 +162,7 @@ export function SignatureManager() {
|
||||
<button
|
||||
key={v.token}
|
||||
type="button"
|
||||
onClick={() => setBodyHtml((prev) => `${prev}${v.token}`)}
|
||||
onClick={() => setSigValue('body_html', `${bodyHtmlValue}${v.token}`)}
|
||||
className="inline-flex items-center px-2 py-0.5 text-xs rounded border border-secondary-300 bg-secondary-50 hover:bg-secondary-100 text-secondary-700"
|
||||
title={v.description}
|
||||
>
|
||||
@@ -161,20 +171,20 @@ export function SignatureManager() {
|
||||
))}
|
||||
</div>
|
||||
<RichTextEditor
|
||||
content={bodyHtml}
|
||||
onChange={setBodyHtml}
|
||||
content={bodyHtmlValue}
|
||||
onChange={(html: string) => setSigValue('body_html', html)}
|
||||
placeholder="<p>Mit freundlichen Grüßen,<br>{{user.name}}</p>"
|
||||
/>
|
||||
</div>
|
||||
<label className="flex items-center gap-2 text-sm text-secondary-700">
|
||||
<input type="checkbox" checked={isDefault} onChange={(e) => setIsDefault(e.target.checked)} className="rounded" />
|
||||
<input type="checkbox" {...registerSig('is_default')} className="rounded" />
|
||||
{t('mail.defaultSignature')}
|
||||
</label>
|
||||
<div className="flex gap-2">
|
||||
<Button onClick={handleSave} isLoading={saving} size="sm">{t('common.save')}</Button>
|
||||
<Button variant="secondary" size="sm" onClick={() => setShowForm(false)}>{t('common.cancel')}</Button>
|
||||
<Button type="submit" isLoading={saving} size="sm">{t('common.save')}</Button>
|
||||
<Button variant="secondary" size="sm" type="button" onClick={() => setShowForm(false)}>{t('common.cancel')}</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user