From 71b1064aa98af79737fb288f9cbb06533b9cb51e Mon Sep 17 00:00:00 2001 From: gitea Date: Wed, 19 Nov 2025 23:41:04 +0100 Subject: [PATCH] formspree added --- app/feedback/page.tsx | 197 ++++++++++++++++++++++++++++++++++------- components/Contact.tsx | 188 +++++++++++++++++++++++++++++++++------ 2 files changed, 327 insertions(+), 58 deletions(-) diff --git a/app/feedback/page.tsx b/app/feedback/page.tsx index a1ac3be..5347e09 100644 --- a/app/feedback/page.tsx +++ b/app/feedback/page.tsx @@ -1,7 +1,7 @@ 'use client'; import { useState } from 'react'; -import { motion } from 'framer-motion'; +import { motion, AnimatePresence } from 'framer-motion'; import Navigation from '@/components/Navigation'; import Footer from '@/components/Footer'; @@ -12,25 +12,52 @@ export default function FeedbackPage() { message: '', }); - const [isSubmitting, setIsSubmitting] = useState(false); + const [status, setStatus] = useState<'idle' | 'submitting' | 'success' | 'error'>('idle'); + const [errorMessage, setErrorMessage] = useState(''); - const handleSubmit = async (e: React.FormEvent) => { + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - setIsSubmitting(true); - - // Handle form submission here - console.log('Feedback submitted:', formData); - - // Simulate API call - await new Promise(resolve => setTimeout(resolve, 500)); - - alert('Thank you for your feedback! We appreciate your input.'); - setFormData({ - name: '', - email: '', - message: '', - }); - setIsSubmitting(false); + setStatus('submitting'); + setErrorMessage(''); + + const form = e.currentTarget; + const data = new FormData(form); + + try { + const response = await fetch(form.action, { + method: form.method, + body: data, + headers: { + 'Accept': 'application/json', + }, + }); + + if (response.ok) { + setStatus('success'); + setFormData({ + name: '', + email: '', + message: '', + }); + form.reset(); + + // Reset success message after 5 seconds + setTimeout(() => { + setStatus('idle'); + }, 5000); + } else { + const data = await response.json(); + if (data.errors) { + setErrorMessage(data.errors.map((error: { message: string }) => error.message).join(', ')); + } else { + setErrorMessage('Oops! There was a problem submitting your form.'); + } + setStatus('error'); + } + } catch (error) { + setErrorMessage('Oops! There was a problem submitting your form.'); + setStatus('error'); + } }; const handleChange = (e: React.ChangeEvent) => { @@ -64,10 +91,76 @@ export default function FeedbackPage() { animate={{ opacity: 1, y: 0 }} transition={{ duration: 0.8, delay: 0.2, ease: [0.25, 0.1, 0.25, 1] }} > -
+ + {status === 'success' ? ( + + + + + + + + Thanks for your submission! + + + Your feedback helps me improve my services. + + + ) : ( +
diff --git a/components/Contact.tsx b/components/Contact.tsx index 3bda747..dff3574 100644 --- a/components/Contact.tsx +++ b/components/Contact.tsx @@ -1,10 +1,9 @@ 'use client'; import { useState } from 'react'; -import { motion } from 'framer-motion'; +import { motion, AnimatePresence } from 'framer-motion'; export default function Contact() { - const [formData, setFormData] = useState({ firstName: '', lastName: '', @@ -13,18 +12,54 @@ export default function Contact() { message: '', }); - const handleSubmit = (e: React.FormEvent) => { + const [status, setStatus] = useState<'idle' | 'submitting' | 'success' | 'error'>('idle'); + const [errorMessage, setErrorMessage] = useState(''); + + const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); - // Handle form submission here - console.log('Form submitted:', formData); - alert('Thank you for your message! We will get back to you soon.'); - setFormData({ - firstName: '', - lastName: '', - email: '', - phone: '', - message: '', - }); + setStatus('submitting'); + setErrorMessage(''); + + const form = e.currentTarget; + const data = new FormData(form); + + try { + const response = await fetch(form.action, { + method: form.method, + body: data, + headers: { + 'Accept': 'application/json', + }, + }); + + if (response.ok) { + setStatus('success'); + setFormData({ + firstName: '', + lastName: '', + email: '', + phone: '', + message: '', + }); + form.reset(); + + // Reset success message after 5 seconds + setTimeout(() => { + setStatus('idle'); + }, 5000); + } else { + const data = await response.json(); + if (data.errors) { + setErrorMessage(data.errors.map((error: { message: string }) => error.message).join(', ')); + } else { + setErrorMessage('Oops! There was a problem submitting your form.'); + } + setStatus('error'); + } + } catch (error) { + setErrorMessage('Oops! There was a problem submitting your form.'); + setStatus('error'); + } }; const handleChange = (e: React.ChangeEvent) => { @@ -61,10 +96,76 @@ export default function Contact() { viewport={{ once: true, margin: '-100px' }} transition={{ duration: 0.8, delay: 0.2, ease: [0.25, 0.1, 0.25, 1] }} > -
+ + {status === 'success' ? ( + + + + + + + + Thank you for your message! + + + I'll get back to you soon. + + + ) : ( +
- - + + {errorMessage && ( + +

{errorMessage}

+
+ )} +
+ + + + )} +