|
| 1 | + |
| 2 | +'use client'; |
| 3 | + |
| 4 | +import { useState } from 'react'; |
| 5 | + |
| 6 | +export default function ContactForm({ onClose }: { onClose: () => void }) { |
| 7 | + const [name, setName] = useState(''); |
| 8 | + const [email, setEmail] = useState(''); |
| 9 | + const [message, setMessage] = useState(''); |
| 10 | + const [submitting, setSubmitting] = useState(false); |
| 11 | + const [error, setError] = useState(''); |
| 12 | + const [success, setSuccess] = useState(false); |
| 13 | + |
| 14 | + const handleSubmit = async (e: React.FormEvent) => { |
| 15 | + e.preventDefault(); |
| 16 | + setSubmitting(true); |
| 17 | + setError(''); |
| 18 | + setSuccess(false); |
| 19 | + |
| 20 | + try { |
| 21 | + const response = await fetch('/api/contact', { |
| 22 | + method: 'POST', |
| 23 | + headers: { |
| 24 | + 'Content-Type': 'application/json', |
| 25 | + }, |
| 26 | + body: JSON.stringify({ name, email, message }), |
| 27 | + }); |
| 28 | + |
| 29 | + if (response.ok) { |
| 30 | + setSuccess(true); |
| 31 | + setName(''); |
| 32 | + setEmail(''); |
| 33 | + setMessage(''); |
| 34 | + } else { |
| 35 | + const data = await response.json(); |
| 36 | + setError(data.message || 'Something went wrong.'); |
| 37 | + } |
| 38 | + } catch (error) { |
| 39 | + setError('Something went wrong.'); |
| 40 | + } finally { |
| 41 | + setSubmitting(false); |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50"> |
| 47 | + <div className="bg-white dark:bg-gray-800 p-8 rounded-lg shadow-lg w-full max-w-md"> |
| 48 | + <h2 className="text-2xl font-bold mb-4">Contact Me</h2> |
| 49 | + {success ? ( |
| 50 | + <div className="text-green-500">Your message has been sent successfully!</div> |
| 51 | + ) : ( |
| 52 | + <form onSubmit={handleSubmit}> |
| 53 | + <div className="mb-4"> |
| 54 | + <label htmlFor="name" className="block text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 55 | + Name |
| 56 | + </label> |
| 57 | + <input |
| 58 | + type="text" |
| 59 | + id="name" |
| 60 | + value={name} |
| 61 | + onChange={(e) => setName(e.target.value)} |
| 62 | + className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" |
| 63 | + required |
| 64 | + /> |
| 65 | + </div> |
| 66 | + <div className="mb-4"> |
| 67 | + <label htmlFor="email" className="block text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 68 | + Email |
| 69 | + </label> |
| 70 | + <input |
| 71 | + type="email" |
| 72 | + id="email" |
| 73 | + value={email} |
| 74 | + onChange={(e) => setEmail(e.target.value)} |
| 75 | + className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" |
| 76 | + required |
| 77 | + /> |
| 78 | + </div> |
| 79 | + <div className="mb-4"> |
| 80 | + <label htmlFor="message" className="block text-sm font-medium text-gray-700 dark:text-gray-300"> |
| 81 | + Message |
| 82 | + </label> |
| 83 | + <textarea |
| 84 | + id="message" |
| 85 | + value={message} |
| 86 | + onChange={(e) => setMessage(e.target.value)} |
| 87 | + rows={4} |
| 88 | + className="mt-1 block w-full px-3 py-2 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm" |
| 89 | + required |
| 90 | + ></textarea> |
| 91 | + </div> |
| 92 | + {error && <div className="text-red-500 mb-4">{error}</div>} |
| 93 | + <div className="flex justify-end"> |
| 94 | + <button |
| 95 | + type="button" |
| 96 | + onClick={onClose} |
| 97 | + className="mr-2 bg-gray-200 dark:bg-gray-600 text-gray-800 dark:text-gray-200 font-bold py-2 px-4 rounded" |
| 98 | + disabled={submitting} |
| 99 | + > |
| 100 | + Cancel |
| 101 | + </button> |
| 102 | + <button |
| 103 | + type="submit" |
| 104 | + className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" |
| 105 | + disabled={submitting} |
| 106 | + > |
| 107 | + {submitting ? 'Submitting...' : 'Submit'} |
| 108 | + </button> |
| 109 | + </div> |
| 110 | + </form> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + ); |
| 115 | +} |
0 commit comments