188 lines
5.4 KiB
TypeScript
188 lines
5.4 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import { CheckCircle, AlertCircle } from 'lucide-react';
|
|
|
|
interface ContactFormData {
|
|
name: string;
|
|
email: string;
|
|
phone?: string;
|
|
message: string;
|
|
}
|
|
|
|
export function ContactForm() {
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
const [submitStatus, setSubmitStatus] = useState<'idle' | 'success' | 'error'>('idle');
|
|
const [formData, setFormData] = useState<ContactFormData>({
|
|
name: '',
|
|
email: '',
|
|
phone: '',
|
|
message: '',
|
|
});
|
|
const [errors, setErrors] = useState<Partial<ContactFormData>>({});
|
|
|
|
const validateForm = (): boolean => {
|
|
const newErrors: Partial<ContactFormData> = {};
|
|
|
|
if (!formData.name.trim()) {
|
|
newErrors.name = 'Name is required';
|
|
}
|
|
|
|
if (!formData.email.trim()) {
|
|
newErrors.email = 'Email is required';
|
|
} else if (!/\S+@\S+\.\S+/.test(formData.email)) {
|
|
newErrors.email = 'Email is invalid';
|
|
}
|
|
|
|
if (!formData.message.trim()) {
|
|
newErrors.message = 'Message is required';
|
|
}
|
|
|
|
setErrors(newErrors);
|
|
return Object.keys(newErrors).length === 0;
|
|
};
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
|
|
if (!validateForm()) {
|
|
return;
|
|
}
|
|
|
|
setIsSubmitting(true);
|
|
setSubmitStatus('idle');
|
|
|
|
try {
|
|
const response = await fetch('/api/contact', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(formData),
|
|
});
|
|
|
|
if (response.ok) {
|
|
setSubmitStatus('success');
|
|
setFormData({ name: '', email: '', phone: '', message: '' });
|
|
} else {
|
|
setSubmitStatus('error');
|
|
}
|
|
} catch (error) {
|
|
setSubmitStatus('error');
|
|
} finally {
|
|
setIsSubmitting(false);
|
|
}
|
|
};
|
|
|
|
const handleInputChange = (field: keyof ContactFormData, value: string) => {
|
|
setFormData(prev => ({ ...prev, [field]: value }));
|
|
if (errors[field]) {
|
|
setErrors(prev => ({ ...prev, [field]: undefined }));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="max-w-2xl mx-auto">
|
|
{submitStatus === 'success' && (
|
|
<div className="mb-6 p-4 bg-green-50 border border-green-200 rounded-md">
|
|
<div className="flex items-center">
|
|
<CheckCircle className="h-5 w-5 text-green-600 mr-2" />
|
|
<p className="text-green-800">
|
|
Thank you for your message! We'll get back to you within 24 hours.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{submitStatus === 'error' && (
|
|
<div className="mb-6 p-4 bg-red-50 border border-red-200 rounded-md">
|
|
<div className="flex items-center">
|
|
<AlertCircle className="h-5 w-5 text-red-600 mr-2" />
|
|
<p className="text-red-800">
|
|
There was an error sending your message. Please try again or call us directly.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-6">
|
|
<div>
|
|
<label htmlFor="name" className="block text-sm font-medium text-ink mb-2">
|
|
Full Name *
|
|
</label>
|
|
<Input
|
|
id="name"
|
|
type="text"
|
|
value={formData.name}
|
|
onChange={(e) => handleInputChange('name', e.target.value)}
|
|
className={errors.name ? 'border-red-500' : ''}
|
|
placeholder="Your full name"
|
|
/>
|
|
{errors.name && (
|
|
<p className="mt-1 text-sm text-red-600">{errors.name}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="email" className="block text-sm font-medium text-ink mb-2">
|
|
Email Address *
|
|
</label>
|
|
<Input
|
|
id="email"
|
|
type="email"
|
|
value={formData.email}
|
|
onChange={(e) => handleInputChange('email', e.target.value)}
|
|
className={errors.email ? 'border-red-500' : ''}
|
|
placeholder="your.email@example.com"
|
|
/>
|
|
{errors.email && (
|
|
<p className="mt-1 text-sm text-red-600">{errors.email}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="phone" className="block text-sm font-medium text-ink mb-2">
|
|
Phone Number (Optional)
|
|
</label>
|
|
<Input
|
|
id="phone"
|
|
type="tel"
|
|
value={formData.phone}
|
|
onChange={(e) => handleInputChange('phone', e.target.value)}
|
|
placeholder="(555) 123-4567"
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="message" className="block text-sm font-medium text-ink mb-2">
|
|
Message *
|
|
</label>
|
|
<Textarea
|
|
id="message"
|
|
value={formData.message}
|
|
onChange={(e) => handleInputChange('message', e.target.value)}
|
|
className={errors.message ? 'border-red-500' : ''}
|
|
placeholder="Tell us about your tax needs..."
|
|
rows={5}
|
|
/>
|
|
{errors.message && (
|
|
<p className="mt-1 text-sm text-red-600">{errors.message}</p>
|
|
)}
|
|
</div>
|
|
|
|
<Button
|
|
type="submit"
|
|
disabled={isSubmitting}
|
|
className="w-full"
|
|
size="lg"
|
|
>
|
|
{isSubmitting ? 'Sending...' : 'Send Message'}
|
|
</Button>
|
|
</form>
|
|
</div>
|
|
);
|
|
}
|