'use client' import { useState, ReactNode } from 'react' import { useAllauthAPI } from '../contexts/APIContext' import { useStyles } from '../contexts/StylesContext' import useAuthForm, { AuthField } from './AuthForm' import { AuthResponse, AuthDetails } from '../api' interface FieldConfig { name: string title: string type: string placeholder?: string } interface FooterLink { href: string label: string } interface AuthFormPageProps { title: string subtitle?: string fields: FieldConfig[] submitLabel?: string submittingLabel?: string submitFn: (api: ReturnType, data: Record) => Promise onResponse: (response: AuthResponse, authDetails: AuthDetails, data: Record) => void footerLinks?: FooterLink[] preFields?: ReactNode postFields?: ReactNode error?: string | null } export function AuthFormPage({ title, subtitle, fields, submitLabel = 'Submit', submittingLabel = 'Submitting...', submitFn, onResponse, footerLinks, preFields, postFields, error: externalError, }: AuthFormPageProps) { const api = useAllauthAPI() const styles = useStyles() const [data, setData] = useState>(() => Object.fromEntries(fields.map(f => [f.name, ''])) ) const authForm = useAuthForm( () => submitFn(api, data), (response, authDetails) => onResponse(response, authDetails, data) ) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() authForm.submit() } const handleFieldChange = (fieldName: string) => (e: React.ChangeEvent) => { setData(prev => ({ ...prev, [fieldName]: e.target.value })) } const formErrors = authForm.errors.filter(err => !err.param || err.param === '__all__') return (

{title}

{subtitle &&

{subtitle}

} {externalError &&

{externalError}

} {formErrors.length > 0 && (
{formErrors.map((err, i) => (

{err.message}

))}
)}
{preFields}
{fields.map(field => ( ))}
{postFields}
{footerLinks && footerLinks.length > 0 && (
{footerLinks.map((link, i) => ( {link.label} ))}
)}
) }