import React, { useState } from 'react'; import { toast } from 'sonner'; interface NewsletterFormProps { description?: string; } export function NewsletterForm({ description }: NewsletterFormProps) { const [email, setEmail] = useState(''); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!email || !email.includes('@')) { toast.error('Please enter a valid email address'); return; } setLoading(true); try { const apiRoot = (window as any).woonoowCustomer?.apiRoot || '/wp-json/woonoow/v1'; const response = await fetch(`${apiRoot}/newsletter/subscribe`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, credentials: 'include', body: JSON.stringify({ email }), }); const data = await response.json(); if (response.ok) { toast.success(data.message || 'Successfully subscribed to newsletter!'); setEmail(''); } else { toast.error(data.message || 'Failed to subscribe. Please try again.'); } } catch (error) { console.error('Newsletter subscription error:', error); toast.error('An error occurred. Please try again later.'); } finally { setLoading(false); } }; return (
{description}
}