@@ -4,11 +4,14 @@ import { useState } from "react";
44import { motion } from "framer-motion" ;
55import SectionHeader from "../ui/SectionHeader" ;
66import { BiSend , BiLoaderAlt } from "react-icons/bi" ;
7-
7+ type StatusType = "Loading" | "Success" | "Error" ;
88const Contact = ( ) => {
99 const [ form , setForm ] = useState ( { email : "" , message : "" } ) ;
1010 const [ errors , setErrors ] = useState < { [ key : string ] : string } > ( { } ) ;
11- const [ status , setStatus ] = useState ( "" ) ;
11+ const [ status , setStatus ] = useState < {
12+ message : string ;
13+ status : StatusType ;
14+ } | null > ( null ) ;
1215 const [ isSending , setIsSending ] = useState ( false ) ;
1316
1417 const emailUrl =
@@ -46,7 +49,7 @@ const Contact = () => {
4649 if ( ! validateForm ( ) ) return ;
4750
4851 setIsSending ( true ) ;
49- setStatus ( "Sending..." ) ;
52+ setStatus ( { message : "Sending..." , status : "Loading" } ) ;
5053
5154 const formData = new FormData ( ) ;
5255 formData . set ( "Name" , "@PORTFOLIO-v2" ) ;
@@ -60,17 +63,25 @@ const Contact = () => {
6063 } ) ;
6164
6265 const result = await response . json ( ) ;
63-
64- setStatus ( `${ result . result } ` ) ;
66+ console . log ( "Email response:" , result ) ;
67+ setStatus ( {
68+ message : "Thanks for reaching out! I'll get back to you soon." ,
69+ status : "Success" ,
70+ } ) ;
6571 setForm ( { email : "" , message : "" } ) ;
6672 } catch ( error ) {
67- setStatus ( "Something went wrong. Please try again." ) ;
73+ setStatus ( {
74+ message : "Something went wrong. Please try again." ,
75+ status : "Error" ,
76+ } ) ;
6877 } finally {
6978 setIsSending ( false ) ;
70- setTimeout ( ( ) => setStatus ( "" ) , 3000 ) ;
79+ setTimeout ( ( ) => setStatus ( null ) , 3000 ) ;
7180 }
7281 } ;
7382
83+ const color = status ?. status === "Success" ? "text-green-500" : status ?. status === "Error" ? "text-red-500" : "text-gray-400" ;
84+
7485 return (
7586 < section className = "w-full mt-25 px-6" >
7687 < div className = "max-w-7xl mx-auto flex flex-col gap-8 justify-center items-center" >
@@ -99,7 +110,6 @@ const Contact = () => {
99110 isSending ? "opacity-70 pointer-events-none" : ""
100111 } `}
101112 >
102-
103113 { /* Email */ }
104114 < div className = "flex flex-col" >
105115 < label htmlFor = "email" className = "text-sm text-gray-400 mb-2" >
@@ -115,9 +125,7 @@ const Contact = () => {
115125 className = { `px-4 py-3 rounded-md bg-transparent border ${
116126 errors . email ? "border-red-500" : "border-gray-700"
117127 } focus:outline-none focus:ring-2 ${
118- errors . email
119- ? "focus:ring-red-500"
120- : "focus:ring-gray-500"
128+ errors . email ? "focus:ring-red-500" : "focus:ring-gray-500"
121129 } placeholder-gray-500 transition-all duration-200`}
122130 disabled = { isSending }
123131 />
@@ -141,9 +149,7 @@ const Contact = () => {
141149 className = { `px-4 py-3 rounded-md bg-transparent border ${
142150 errors . message ? "border-red-500" : "border-gray-700"
143151 } focus:outline-none focus:ring-2 ${
144- errors . message
145- ? "focus:ring-red-500"
146- : "focus:ring-gray-500"
152+ errors . message ? "focus:ring-red-500" : "focus:ring-gray-500"
147153 } placeholder-gray-500 resize-none transition-all duration-200`}
148154 disabled = { isSending }
149155 />
@@ -154,6 +160,18 @@ const Contact = () => {
154160 ) }
155161 </ div >
156162
163+
164+ { /* Status Message */ }
165+ { status && (
166+ < motion . p
167+ initial = { { opacity : 0 } }
168+ animate = { { opacity : 1 } }
169+ className = { `text-sm mt-2 ${ color } ` }
170+ >
171+ { status . message }
172+ </ motion . p >
173+ ) }
174+
157175 { /* Submit Button */ }
158176 < button
159177 type = "submit"
@@ -174,17 +192,6 @@ const Contact = () => {
174192 </ >
175193 ) }
176194 </ button >
177-
178- { /* Status Message */ }
179- { status && (
180- < motion . p
181- initial = { { opacity : 0 } }
182- animate = { { opacity : 1 } }
183- className = "text-sm mt-2 text-gray-400"
184- >
185- { status }
186- </ motion . p >
187- ) }
188195 </ motion . form >
189196 </ div >
190197 </ section >
0 commit comments