1
1
import React , { useState } from 'react' ;
2
+ import emailjs from '@emailjs/browser' ;
2
3
3
4
const Contact = ( ) => {
4
- // Configuration for multiple emails and form IDs
5
- const getContactConfig = ( ) => {
6
- // Parse comma-separated lists from environment variables
7
- const emailList = process . env . REACT_APP_CONTACT_EMAILS
8
- ? process . env . REACT_APP_CONTACT_EMAILS . split ( ',' ) . map ( email => email . trim ( ) )
9
- :
( process . env . REACT_APP_CONTACT_EMAIL ?
[ process . env . REACT_APP_CONTACT_EMAIL ] :
[ '[email protected] ' ] ) ;
10
-
11
- const formIdList = process . env . REACT_APP_FORMSPREE_FORM_IDS
12
- ? process . env . REACT_APP_FORMSPREE_FORM_IDS . split ( ',' ) . map ( id => id . trim ( ) )
13
- : ( process . env . REACT_APP_FORMSPREE_FORM_ID ? [ process . env . REACT_APP_FORMSPREE_FORM_ID ] : [ 'YOUR_FORM_ID' ] ) ;
14
-
15
- return {
16
- emails : emailList ,
17
- formIds : formIdList
18
- } ;
5
+ // EmailJS configuration with your actual credentials
6
+ const EMAILJS_CONFIG = {
7
+ serviceId : 'm3_contact_service' ,
8
+ templateId : 'template_sn5rm19' ,
9
+ publicKey : 'aUrTfsE6oJtpIe1ac'
19
10
} ;
20
11
21
- // Development warnings for missing configuration
22
- if ( process . env . NODE_ENV === 'development' ) {
23
- const config = getContactConfig ( ) ;
24
-
25
- if ( config . formIds . length === 0 || config . formIds . some ( id => ! id || id === 'YOUR_FORM_ID' ) ) {
26
- console . warn ( '⚠️ Contact forms not configured: Please set REACT_APP_FORMSPREE_FORM_IDS (comma-separated) in webapp/.env' ) ;
27
- }
28
-
29
- if ( config . emails . length === 0 || config . emails . some ( email => ! email || email . includes ( '@example.com' ) ) ) {
30
- console . warn ( '⚠️ Contact emails not configured: Please set REACT_APP_CONTACT_EMAILS (comma-separated) in webapp/.env' ) ;
31
- }
32
-
33
- console . log ( `📧 Configured emails: ${ config . emails . join ( ', ' ) } ` ) ;
34
- console . log ( `📝 Configured form IDs: ${ config . formIds . join ( ', ' ) } ` ) ;
35
- }
36
-
37
12
const [ contactForm , setContactForm ] = useState ( {
38
13
email : '' ,
39
14
inquiryType : 'hospital' ,
40
15
message : ''
41
16
} ) ;
42
17
const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
43
- const [ submitStatus , setSubmitStatus ] = useState ( '' ) ;
18
+ const [ submitStatus , setSubmitStatus ] = useState ( null ) ;
44
19
45
20
const handleInputChange = ( e ) => {
46
21
const { name, value } = e . target ;
@@ -53,74 +28,57 @@ const Contact = () => {
53
28
const handleSubmit = async ( e ) => {
54
29
e . preventDefault ( ) ;
55
30
setIsSubmitting ( true ) ;
56
- setSubmitStatus ( '' ) ;
31
+ setSubmitStatus ( null ) ;
57
32
58
33
try {
59
- // Get configuration for multiple emails and form IDs
60
- const config = getContactConfig ( ) ;
61
-
62
- if ( config . formIds . length === 0 || config . formIds . some ( id => ! id || id === 'YOUR_FORM_ID' ) ) {
63
- throw new Error ( 'Form service not configured. Please set REACT_APP_FORMSPREE_FORM_IDS in .env file.' ) ;
64
- }
65
-
66
- // Submit to all configured form IDs simultaneously
67
- const submissionPromises = config . formIds . map ( formId =>
68
- fetch ( `https://formspree.io/f/${ formId } ` , {
69
- method : 'POST' ,
70
- headers : {
71
- 'Content-Type' : 'application/json' ,
72
- } ,
73
- body : JSON . stringify ( {
74
- email : contactForm . email ,
75
- inquiryType : contactForm . inquiryType ,
76
- message : contactForm . message ,
77
- destinationEmails : config . emails . join ( ', ' ) , // Include all destination emails for tracking
78
- subject : `M3 Contact: ${ contactForm . inquiryType === 'hospital' ? 'Hospital/EHR MCP Request' :
79
- contactForm . inquiryType === 'suggestions' ? 'Suggestions' : 'General Contact' } `
80
- } ) ,
81
- } )
34
+ // Prepare template parameters
35
+ const templateParams = {
36
+ user_email : contactForm . email ,
37
+ user_name : contactForm . email . split ( '@' ) [ 0 ] , // Extract name from email
38
+ inquiry_type : contactForm . inquiryType ,
39
+ message : contactForm . message || 'No additional message provided' ,
40
+ inquiry_type_label : contactForm . inquiryType === 'hospital' ? 'Hospital/EHR MCP Request' :
41
+ contactForm . inquiryType === 'suggestions' ? 'Suggestions & Feedback' : 'General Contact' ,
42
+ timestamp : new Date ( ) . toLocaleString ( )
43
+ } ;
44
+
45
+ // Send email using EmailJS
46
+ const response = await emailjs . send (
47
+ EMAILJS_CONFIG . serviceId ,
48
+ EMAILJS_CONFIG . templateId ,
49
+ templateParams ,
50
+ EMAILJS_CONFIG . publicKey
82
51
) ;
83
52
84
- // Wait for all submissions to complete
85
- const responses = await Promise . allSettled ( submissionPromises ) ;
86
-
87
- // Check if at least one submission was successful
88
- const hasSuccessfulSubmission = responses . some ( result =>
89
- result . status === 'fulfilled' && result . value . ok
90
- ) ;
53
+ console . log ( 'Email sent successfully:' , response ) ;
54
+ setSubmitStatus ( { type : 'success' , message : 'Message sent successfully! We\'ll get back to you soon.' } ) ;
55
+ setContactForm ( { email : '' , inquiryType : 'hospital' , message : '' } ) ;
91
56
92
- if ( hasSuccessfulSubmission ) {
93
- setSubmitStatus ( 'success' ) ;
94
- setContactForm ( { email : '' , inquiryType : 'hospital' , message : '' } ) ;
95
-
96
- // Log any failed submissions for debugging
97
- const failedSubmissions = responses . filter ( result =>
98
- result . status === 'rejected' || ( result . status === 'fulfilled' && ! result . value . ok )
99
- ) ;
100
-
101
- if ( failedSubmissions . length > 0 ) {
102
- console . warn ( `${ failedSubmissions . length } of ${ config . formIds . length } form submissions failed` ) ;
103
- }
104
- } else {
105
- throw new Error ( 'All form submissions failed' ) ;
106
- }
107
57
} catch ( error ) {
108
- console . error ( 'Error submitting form :' , error ) ;
58
+ console . error ( 'Error sending email :' , error ) ;
109
59
setSubmitStatus ( 'error' ) ;
60
+ // Store the specific error message for display
61
+ setSubmitStatus ( { type : 'error' , message : getErrorMessage ( error ) } ) ;
110
62
} finally {
111
63
setIsSubmitting ( false ) ;
112
64
}
113
65
} ;
114
66
115
- const handleMailtoFallback = ( ) => {
116
- // Use all configured emails
117
- const config = getContactConfig ( ) ;
118
- const emailList = config . emails . join ( ',' ) ;
119
-
120
- const subject = `M3 Contact: ${ contactForm . inquiryType === 'hospital' ? 'Hospital/EHR MCP Request' :
121
- contactForm . inquiryType === 'suggestions' ? 'Suggestions' : 'General Contact' } `;
122
- const body = `Email: ${ contactForm . email } %0D%0A%0D%0AMessage: ${ contactForm . message } ` ;
123
- window . open ( `mailto:${ emailList } ?subject=${ encodeURIComponent ( subject ) } &body=${ body } ` ) ;
67
+ const getErrorMessage = ( error ) => {
68
+ // Handle different types of errors
69
+ if ( error . message ?. includes ( 'rate limit' ) || error . status === 429 ) {
70
+ return 'Too many emails sent recently. Please try again in a few minutes.' ;
71
+ }
72
+ if ( error . message ?. includes ( 'invalid email' ) || error . status === 400 ) {
73
+ return 'Please check your email address and try again.' ;
74
+ }
75
+ if ( error . message ?. includes ( 'network' ) || ! navigator . onLine ) {
76
+ return 'Network error. Please check your internet connection and try again.' ;
77
+ }
78
+ if ( error . status === 403 ) {
79
+ return 'Service temporarily unavailable. Please try again later.' ;
80
+ }
81
+ return 'Failed to send message. Please try again later.' ;
124
82
} ;
125
83
126
84
return (
@@ -355,22 +313,15 @@ const Contact = () => {
355
313
</ button >
356
314
</ div >
357
315
358
- { submitStatus === 'success' && (
316
+ { submitStatus ?. type === 'success' && (
359
317
< div className = "status-message status-success" >
360
- ✅ Message sent successfully! We'll get back to you soon.
318
+ ✅ { submitStatus . message }
361
319
</ div >
362
320
) }
363
321
364
- { submitStatus === 'error' && (
322
+ { submitStatus ?. type === 'error' && (
365
323
< div className = "status-message status-error" >
366
- ❌ Failed to send message. Please try again or{ ' ' }
367
- < button
368
- type = "button"
369
- onClick = { handleMailtoFallback }
370
- style = { { background : 'none' , border : 'none' , color : 'inherit' , textDecoration : 'underline' , cursor : 'pointer' } }
371
- >
372
- send via email
373
- </ button > .
324
+ ❌ { submitStatus . message }
374
325
</ div >
375
326
) }
376
327
</ form >
0 commit comments