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