|
| 1 | +// app/contact/page.js |
1 | 2 | 'use client' |
2 | 3 |
|
3 | 4 | import { useState } from 'react' |
4 | 5 | import Image from 'next/image' |
5 | 6 |
|
6 | | -export default function Contact() { |
| 7 | +export default function ContactPage() { |
7 | 8 | const [form, setForm] = useState({ name: '', email: '', message: '' }) |
8 | 9 | const [status, setStatus] = useState(null) |
9 | 10 | const [submitting, setSubmitting] = useState(false) |
10 | 11 |
|
| 12 | + const handleChange = (e) => |
| 13 | + setForm({ ...form, [e.target.name]: e.target.value }) |
| 14 | + |
11 | 15 | const handleSubmit = async (e) => { |
12 | 16 | e.preventDefault() |
13 | 17 | setSubmitting(true) |
14 | 18 | setStatus(null) |
15 | 19 |
|
16 | | - const res = await fetch('/api/contact', { |
17 | | - method: 'POST', |
18 | | - headers: { 'Content-Type': 'application/json' }, |
19 | | - body: JSON.stringify(form), |
20 | | - }) |
21 | | - |
22 | | - if (res.ok) { |
| 20 | + try { |
| 21 | + const res = await fetch('/api/contact', { |
| 22 | + method: 'POST', |
| 23 | + headers: { 'Content-Type': 'application/json' }, |
| 24 | + body: JSON.stringify(form), |
| 25 | + }) |
| 26 | + if (!res.ok) throw new Error('Network response was not ok') |
23 | 27 | setStatus('Message sent!') |
24 | 28 | setForm({ name: '', email: '', message: '' }) |
25 | | - } else { |
| 29 | + } catch { |
26 | 30 | setStatus('Failed to send message.') |
| 31 | + } finally { |
| 32 | + setSubmitting(false) |
27 | 33 | } |
28 | | - |
29 | | - setSubmitting(false) |
30 | 34 | } |
31 | 35 |
|
32 | 36 | return ( |
33 | | - <div className="max-w-xl mx-auto p-8 text-center"> |
34 | | - <Image |
35 | | - src="/portraits/casual.jpeg" |
36 | | - width={250} |
37 | | - height={350} |
38 | | - className="mx-auto rounded-xl mb-6" |
39 | | - alt="Contact Grant" |
40 | | - /> |
41 | | - <h2 className="text-3xl font-bold mb-4">Contact Me</h2> |
42 | | - <form onSubmit={handleSubmit} className="space-y-4"> |
43 | | - <input |
44 | | - required |
45 | | - className="w-full p-2 border rounded bg-black text-white placeholder-gray-400" |
46 | | - placeholder="Name" |
47 | | - value={form.name} |
48 | | - onChange={(e) => setForm({ ...form, name: e.target.value })} |
49 | | - /> |
50 | | - <input |
51 | | - required |
52 | | - type="email" |
53 | | - className="w-full p-2 border rounded bg-black text-white placeholder-gray-400" |
54 | | - placeholder="Email" |
55 | | - value={form.email} |
56 | | - onChange={(e) => setForm({ ...form, email: e.target.value })} |
57 | | - /> |
58 | | - <textarea |
59 | | - required |
60 | | - className="w-full p-2 border rounded bg-black text-white placeholder-gray-400" |
61 | | - rows={5} |
62 | | - placeholder="Message" |
63 | | - value={form.message} |
64 | | - onChange={(e) => setForm({ ...form, message: e.target.value })} |
65 | | - /> |
66 | | - <button |
67 | | - type="submit" |
68 | | - disabled={submitting} |
69 | | - className={`w-full py-2 px-4 rounded text-white ${submitting ? 'bg-gray-500' : 'bg-blue-600 hover:bg-blue-700'}`} |
70 | | - > |
71 | | - {submitting ? 'Sending...' : 'Send Message'} |
72 | | - </button> |
73 | | - {status && <p className="text-sm mt-2">{status}</p>} |
74 | | - </form> |
| 37 | + <div className="min-h-screen py-12"> |
| 38 | + <div className="max-w-md mx-auto bg-white dark:bg-gray-700 rounded-2xl shadow-xl overflow-hidden"> |
| 39 | + <div className="p-6"> |
| 40 | + <div className="flex justify-center mb-6"> |
| 41 | + <div className="w-32 h-32 rounded-full overflow-hidden ring-4 ring-white dark:ring-gray-800"> |
| 42 | + <Image |
| 43 | + src="/portraits/casual.jpeg" |
| 44 | + alt="Contact Grant" |
| 45 | + width={128} |
| 46 | + height={128} |
| 47 | + className="object-cover" |
| 48 | + /> |
| 49 | + </div> |
| 50 | + </div> |
| 51 | + |
| 52 | + <h2 className="text-2xl font-semibold text-gray-900 dark:text-gray-100 text-center mb-6"> |
| 53 | + Contact Me |
| 54 | + </h2> |
| 55 | + |
| 56 | + <form onSubmit={handleSubmit} className="space-y-4"> |
| 57 | + <input |
| 58 | + id="name" |
| 59 | + name="name" |
| 60 | + value={form.name} |
| 61 | + onChange={handleChange} |
| 62 | + required |
| 63 | + placeholder="Your Name" |
| 64 | + className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-gray-100 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-400" |
| 65 | + /> |
| 66 | + |
| 67 | + <input |
| 68 | + id="email" |
| 69 | + type="email" |
| 70 | + name="email" |
| 71 | + value={form.email} |
| 72 | + onChange={handleChange} |
| 73 | + required |
| 74 | + placeholder="Your Email" |
| 75 | + className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-gray-100 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-400" |
| 76 | + /> |
| 77 | + |
| 78 | + <textarea |
| 79 | + id="message" |
| 80 | + name="message" |
| 81 | + value={form.message} |
| 82 | + onChange={handleChange} |
| 83 | + required |
| 84 | + rows={5} |
| 85 | + placeholder="Your Message" |
| 86 | + className="w-full px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-gray-100 dark:bg-gray-800 text-gray-900 dark:text-gray-100 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-blue-400" |
| 87 | + /> |
| 88 | + |
| 89 | + <button |
| 90 | + type="submit" |
| 91 | + disabled={submitting} |
| 92 | + className={`w-full py-3 text-white font-medium rounded-lg transition ${ |
| 93 | + submitting |
| 94 | + ? 'bg-gray-400 cursor-not-allowed' |
| 95 | + : 'bg-blue-500 hover:bg-blue-600' |
| 96 | + }`} |
| 97 | + > |
| 98 | + {submitting ? 'Sending...' : 'Send Message'} |
| 99 | + </button> |
| 100 | + |
| 101 | + {status && ( |
| 102 | + <p |
| 103 | + className={`text-center mt-2 text-sm ${ |
| 104 | + status.includes('Failed') ? 'text-red-400' : 'text-green-400' |
| 105 | + }`} |
| 106 | + > |
| 107 | + {status} |
| 108 | + </p> |
| 109 | + )} |
| 110 | + </form> |
| 111 | + </div> |
| 112 | + </div> |
75 | 113 | </div> |
76 | 114 | ) |
77 | 115 | } |
0 commit comments