82 lines
5.2 KiB
TypeScript
82 lines
5.2 KiB
TypeScript
'use client'
|
|
|
|
import Link from 'next/link'
|
|
import { useActionState, useRef, useState } from 'react'
|
|
import { submitInquiry, type InquiryState } from '@/app/actions/inquiry'
|
|
import type { Dict, Locale } from '@/i18n'
|
|
|
|
/** Форма обращения. dark — на тёмном фоне блока «Сотрудничество». Тексты — из словаря, ошибки по полям. */
|
|
export function ContactForm({ t, locale, email, dark = false, privacyLink = false }: { t: Dict; locale: Locale; email: string; dark?: boolean; privacyLink?: boolean }) {
|
|
const [state, action, pending] = useActionState<InquiryState, FormData>(submitInquiry, { status: 'idle' })
|
|
const openedAt = useRef<HTMLInputElement>(null)
|
|
const [values, setValues] = useState({ name: '', email: '', organization: '', message: '', consent: false })
|
|
const errors = state.status === 'error' ? state.errors : []
|
|
const f = t.form
|
|
const field = `mt-2 block w-full border bg-transparent px-4 py-3 outline-none focus:border-burgundy ${dark ? 'border-paper/40 focus:border-paper' : 'border-line'}`
|
|
const hint = dark ? 'text-sm text-paper/60' : 'text-sm text-muted'
|
|
const err = (k: 'name' | 'email' | 'message' | 'consent') =>
|
|
errors.includes(k) ? <p id={`err-${k}`} role="alert" className={`mt-1 text-sm ${dark ? 'text-[#ffb4bc]' : 'text-burgundy'}`}>{f.errors[k]}</p> : null
|
|
const bad = (k: 'name' | 'email' | 'message' | 'consent') => (errors.includes(k) ? { 'aria-invalid': true, 'aria-describedby': `err-${k}` } : {})
|
|
|
|
if (state.status === 'sent') {
|
|
return (
|
|
<div role="status" className={`max-w-2xl border p-7 ${dark ? 'border-paper/40' : 'border-line'}`}>
|
|
<h3 className="font-serif text-2xl">{f.successTitle}</h3>
|
|
<p className={`mt-3 ${dark ? 'text-paper/75' : 'text-muted'}`}>{f.successText}</p>
|
|
<button type="button" onClick={() => window.location.reload()} className="mt-5 underline underline-offset-4 hover:no-underline">{f.again}</button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<form action={action} noValidate onFocus={() => { if (openedAt.current && !openedAt.current.value) openedAt.current.value = String(Date.now()) }} className="max-w-2xl space-y-6">
|
|
<input type="hidden" name="locale" value={locale} />
|
|
<input type="hidden" name="openedAt" ref={openedAt} defaultValue="" />
|
|
{/* Ловушка для роботов: людям не видна. */}
|
|
<div aria-hidden className="absolute -left-[9999px] h-0 w-0 overflow-hidden">
|
|
<label>{f.email}<input type="text" name="website" tabIndex={-1} autoComplete="off" /></label>
|
|
</div>
|
|
<label className="block">{f.name}
|
|
<input name="name" value={values.name} onChange={(event) => setValues({ ...values, name: event.target.value })} type="text" autoComplete="name" required maxLength={120} className={field} {...bad('name')} />
|
|
{err('name')}
|
|
</label>
|
|
<label className="block">{f.email}
|
|
<input name="email" value={values.email} onChange={(event) => setValues({ ...values, email: event.target.value })} type="email" autoComplete="email" required maxLength={200} className={field} {...bad('email')} />
|
|
{err('email')}
|
|
</label>
|
|
<label className="block">{f.organization} <span className={hint}>({f.optional})</span>
|
|
<input name="organization" value={values.organization} onChange={(event) => setValues({ ...values, organization: event.target.value })} type="text" autoComplete="organization" maxLength={200} className={field} />
|
|
</label>
|
|
<label className="block">{f.message}
|
|
<textarea name="message" value={values.message} onChange={(event) => setValues({ ...values, message: event.target.value })} rows={6} required maxLength={5000} className={field} {...bad('message')} />
|
|
{err('message')}
|
|
</label>
|
|
<div>
|
|
<label className="flex items-start gap-3 text-sm">
|
|
<input name="consent" checked={values.consent} onChange={(event) => setValues({ ...values, consent: event.target.checked })} type="checkbox" required className="mt-1 h-4 w-4 shrink-0 accent-[#7a1f2b]" {...bad('consent')} />
|
|
<span>
|
|
{privacyLink ? (
|
|
<>
|
|
{f.consentPolicyBefore}
|
|
<Link href={`/${locale}/privacy`} className="underline underline-offset-4 hover:no-underline">{f.consentPolicyLink}</Link>
|
|
{f.consentPolicyAfter}
|
|
</>
|
|
) : f.consent}
|
|
</span>
|
|
</label>
|
|
{err('consent')}
|
|
</div>
|
|
{(errors.includes('tooLong') || errors.includes('tooMany') || errors.includes('failed')) && (
|
|
<p role="alert" className={dark ? 'text-[#ffb4bc]' : 'text-burgundy'}>
|
|
{errors.includes('tooMany') ? f.errors.tooMany : errors.includes('tooLong') ? f.errors.tooLong : f.errors.failed}
|
|
</p>
|
|
)}
|
|
<button type="submit" disabled={pending}
|
|
className={`px-8 py-3 transition disabled:opacity-60 ${dark ? 'bg-paper text-graphite hover:bg-paper/85' : 'bg-burgundy text-paper hover:bg-graphite'}`}>
|
|
{pending ? f.sending : f.submit}
|
|
</button>
|
|
<p className={hint}>{f.fallback}<a href={`mailto:${email}`} className="underline underline-offset-4 hover:no-underline">{email}</a></p>
|
|
</form>
|
|
)
|
|
}
|