11import Button from '../reusable/Button' ;
22import FormInput from '../reusable/FormInput' ;
33import { useState } from 'react' ;
4+ import toastr from 'toastr' ; // Make sure toastr is installed and imported
5+ import 'toastr/build/toastr.min.css' ; // Include toastr CSS
46
57const ContactForm = ( ) => {
68 const [ formData , setFormData ] = useState ( {
@@ -11,6 +13,7 @@ const ContactForm = () => {
1113 } ) ;
1214
1315 const [ errors , setErrors ] = useState ( { } ) ;
16+ const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
1417
1518 // Validation Function
1619 const validateForm = ( ) => {
@@ -55,7 +58,7 @@ const ContactForm = () => {
5558 }
5659 } ;
5760
58- const onSubmit = ( e ) => {
61+ const onSubmit = async ( e ) => {
5962 e . preventDefault ( ) ;
6063
6164 // Validate the form
@@ -66,18 +69,38 @@ const ContactForm = () => {
6669 return ;
6770 }
6871
69- // If no errors, log the form data
70- console . log ( 'Form submitted:' , formData ) ;
71-
72- // Clear form values after successful submission
73- setFormData ( {
74- name : '' ,
75- email : '' ,
76- subject : '' ,
77- message : '' ,
78- } ) ;
79- setErrors ( { } ) ;
80-
72+ setIsSubmitting ( true ) ;
73+
74+ try {
75+ // Make the API call
76+ const response = await fetch ( 'http://localhost:8081/contact' , {
77+ method : 'POST' ,
78+ headers : {
79+ 'Content-Type' : 'application/json' ,
80+ } ,
81+ body : JSON . stringify ( formData ) ,
82+ } ) ;
83+
84+ const data = await response . json ( ) ;
85+
86+ if ( data . success ) {
87+ toastr . success ( data . message ) ; // Display toastr notification
88+ // Clear form values after successful submission
89+ setFormData ( {
90+ name : '' ,
91+ email : '' ,
92+ subject : '' ,
93+ message : '' ,
94+ } ) ;
95+ } else {
96+ toastr . error ( 'Failed to send the message. Please try again.' ) ;
97+ }
98+ } catch ( error ) {
99+ console . error ( 'Error sending message:' , error ) ;
100+ toastr . error ( 'An error occurred. Please try again later.' ) ;
101+ } finally {
102+ setIsSubmitting ( false ) ;
103+ }
81104 } ;
82105
83106 return (
@@ -167,9 +190,10 @@ const ContactForm = () => {
167190 { /* Submit Button */ }
168191 < div className = "font-general-medium w-40 px-4 py-2.5 text-white text-center font-medium tracking-wider bg-indigo-500 hover:bg-indigo-600 focus:ring-1 focus:ring-indigo-900 rounded-lg mt-6 duration-500" >
169192 < Button
170- title = " Send Message"
193+ title = { isSubmitting ? 'Sending...' : ' Send Message' }
171194 type = "submit"
172195 aria-label = "Send Message"
196+ disabled = { isSubmitting }
173197 />
174198 </ div >
175199 </ form >
0 commit comments