@@ -99,6 +99,51 @@ function resolveApiEndpoint(form) {
9999 return new URL ( action , envBase . endsWith ( "/" ) ? envBase : `${ envBase } /` ) . toString ( ) ;
100100}
101101
102+ function formActionPath ( form ) {
103+ const action = String ( form . getAttribute ( "action" ) || "" ) . trim ( ) ;
104+ return action . replace ( / ^ h t t p s ? : \/ \/ [ ^ / ] + / i, "" ) ;
105+ }
106+
107+ function buildApiPayload ( form , fd ) {
108+ const actionPath = formActionPath ( form ) ;
109+
110+ if ( actionPath === "/api/small-group" ) {
111+ return {
112+ name : String ( fd . get ( "name" ) || "" ) . trim ( ) ,
113+ area : String ( fd . get ( "area" ) || "" ) . trim ( ) ,
114+ phone : String ( fd . get ( "phone" ) || "" ) . trim ( ) ,
115+ message : String ( fd . get ( "message" ) || "" ) . trim ( )
116+ } ;
117+ }
118+
119+ if ( actionPath === "/api/volunteer" ) {
120+ return {
121+ name : String ( fd . get ( "name" ) || "" ) . trim ( ) ,
122+ email : String ( fd . get ( "email" ) || "" ) . trim ( ) ,
123+ ministry : String ( fd . get ( "ministry" ) || "" ) . trim ( ) ,
124+ message : String ( fd . get ( "message" ) || "" ) . trim ( )
125+ } ;
126+ }
127+
128+ return {
129+ name : String ( fd . get ( "name" ) || "" ) . trim ( ) ,
130+ email : String ( fd . get ( "email" ) || "" ) . trim ( ) ,
131+ subject : String ( fd . get ( "subject" ) || "" ) . trim ( ) ,
132+ message : String ( fd . get ( "message" ) || "" ) . trim ( )
133+ } ;
134+ }
135+
136+ function successMessageForForm ( form ) {
137+ const actionPath = formActionPath ( form ) ;
138+ if ( actionPath === "/api/small-group" ) {
139+ return "Thank you. Your small group registration has been received." ;
140+ }
141+ if ( actionPath === "/api/volunteer" ) {
142+ return "Thank you. Your volunteer signup has been received." ;
143+ }
144+ return "Thank you. Your message has been sent." ;
145+ }
146+
102147function initEmailApiForms ( ) {
103148 bySelAll ( "form[data-email-submit]" ) . forEach ( ( form ) => {
104149 form . addEventListener ( "submit" , async ( e ) => {
@@ -112,12 +157,7 @@ function initEmailApiForms() {
112157 }
113158
114159 const fd = new FormData ( form ) ;
115- const payload = {
116- name : String ( fd . get ( "name" ) || "" ) . trim ( ) ,
117- email : String ( fd . get ( "email" ) || "" ) . trim ( ) ,
118- subject : String ( fd . get ( "subject" ) || "" ) . trim ( ) ,
119- message : String ( fd . get ( "message" ) || "" ) . trim ( )
120- } ;
160+ const payload = buildApiPayload ( form , fd ) ;
121161
122162 try {
123163 const res = await fetch ( resolveApiEndpoint ( form ) , {
@@ -129,12 +169,24 @@ function initEmailApiForms() {
129169 body : JSON . stringify ( payload )
130170 } ) ;
131171
132- if ( ! res . ok ) throw new Error ( "Failed to submit message." ) ;
172+ if ( ! res . ok ) {
173+ let apiError = "Failed to submit form." ;
174+ try {
175+ const data = await res . json ( ) ;
176+ if ( data && typeof data . error === "string" && data . error ) {
177+ apiError = data . error ;
178+ }
179+ } catch ( _jsonErr ) {
180+ // Keep fallback API error message when response body is not JSON.
181+ }
182+ throw new Error ( apiError ) ;
183+ }
133184
134- setFormMessage ( form , "Thank you. Your message has been sent." ) ;
185+ setFormMessage ( form , successMessageForForm ( form ) ) ;
135186 form . reset ( ) ;
136- } catch ( _err ) {
137- setFormMessage ( form , "Unable to send right now. Please try again shortly." ) ;
187+ } catch ( err ) {
188+ const msg = err && err . message ? err . message : "Unable to send right now. Please try again shortly." ;
189+ setFormMessage ( form , msg ) ;
138190 } finally {
139191 if ( submitBtn ) {
140192 submitBtn . disabled = false ;
0 commit comments