Skip to content

Commit d13a79b

Browse files
authored
Forms: Add error message on network request failure for ajax submission (#44386)
* fix typo * add network error message to config * add debug dependency * show error on submission failure * auto-remove submission error notice * changelog * update error message
1 parent 9db1109 commit d13a79b

File tree

7 files changed

+37
-10
lines changed

7 files changed

+37
-10
lines changed

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: added
3+
4+
Forms: Add error message on network request failure for ajax submission

projects/packages/forms/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"@wordpress/url": "4.26.0",
6262
"clsx": "2.1.1",
6363
"copy-webpack-plugin": "13.0.0",
64+
"debug": "4.4.1",
6465
"email-validator": "2.0.4",
6566
"gridicons": "3.4.2",
6667
"js-sha256": "0.11.1",

projects/packages/forms/src/contact-form/class-contact-form-plugin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ protected function __construct() {
298298
'is_required' => __( 'This field is required.', 'jetpack-forms' ),
299299
'invalid_form_empty' => __( 'The form you are trying to submit is empty.', 'jetpack-forms' ),
300300
'invalid_form' => __( 'Please fill out the form correctly.', 'jetpack-forms' ),
301+
'network_error' => __( 'Connection issue while submitting the form. Check that you are connected to the Internet and try again.', 'jetpack-forms' ),
301302
),
302303
'admin_ajax_url' => admin_url( 'admin-ajax.php' ),
303304
);

projects/packages/forms/src/contact-form/class-contact-form.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ private static function format_submission_data( $data ) {
675675
* @return string HTML string for the error wrapper.
676676
*/
677677
private static function render_error_wrapper() {
678-
$html = '<div class="contact-form__error" data-wp-class--show-errors="state.showFromErrors">';
678+
$html = '<div class="contact-form__error" data-wp-class--show-errors="state.showFormErrors">';
679679
$html .= '<span class="contact-form__warning-icon"><span class="visually-hidden">' . __( 'Warning.', 'jetpack-forms' ) . '</span><i aria-hidden="true"></i></span>
680680
<span data-wp-text="state.getFormErrorMessage"></span>
681681
<ul>
@@ -684,6 +684,8 @@ private static function render_error_wrapper() {
684684
</template>
685685
</ul>';
686686
$html .= '</div>';
687+
688+
$html .= '<div class="contact-form__error" data-wp-class--show-errors="state.showSubmissionError" data-wp-text="context.submissionError"></div>';
687689
return $html;
688690
}
689691

projects/packages/forms/src/modules/form/shared.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* External dependencies
33
*/
44
import { getConfig } from '@wordpress/interactivity';
5+
import debugFactory from 'debug';
6+
7+
const debug = debugFactory( 'jetpack-forms:interactivity' );
58

69
const NAMESPACE = 'jetpack/form';
710
const config = getConfig( NAMESPACE );
@@ -61,13 +64,15 @@ export const submitForm = async formHash => {
6164
} );
6265

6366
if ( ! response.ok ) {
64-
return { success: false, error: response.status };
67+
debug( 'Form submission failed', response );
68+
return { success: false, error: config?.error_types?.network_error };
6569
}
6670

6771
const result = await response.json();
6872

6973
return result;
7074
} catch ( error ) {
71-
return { success: false, error: error.message };
75+
debug( 'Form submission failed', error );
76+
return { success: false, error: config?.error_types?.network_error };
7277
}
7378
};

projects/packages/forms/src/modules/form/view.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const withSyncEvent =
2121

2222
const NAMESPACE = 'jetpack/form';
2323
const config = getConfig( NAMESPACE );
24+
let errorTimeout = null;
2425

2526
const updateField = ( fieldId, value, showFieldError = false ) => {
2627
const context = getContext();
@@ -183,12 +184,18 @@ const { state } = store( NAMESPACE, {
183184
return ! Object.values( context.fields ).some( field => field.error !== 'yes' );
184185
},
185186

186-
get showFromErrors() {
187+
get showFormErrors() {
187188
const context = getContext();
188189

189190
return ! state.isFormValid && context.showErrors;
190191
},
191192

193+
get showSubmissionError() {
194+
const context = getContext();
195+
196+
return !! context.submissionError && ! state.showFormErrors;
197+
},
198+
192199
get getFormErrorMessage() {
193200
if ( state.isFormEmpty ) {
194201
const context = getContext();
@@ -229,11 +236,6 @@ const { state } = store( NAMESPACE, {
229236
const field = context.fields[ fieldId ];
230237
return field?.value || '';
231238
},
232-
233-
get getSubmissionError() {
234-
const context = getContext();
235-
return context.submissionError || '';
236-
},
237239
},
238240

239241
actions: {
@@ -339,12 +341,12 @@ const { state } = store( NAMESPACE, {
339341
if ( context.isResponseWithoutReloadEnabled ) {
340342
event.preventDefault();
341343
event.stopPropagation();
344+
context.submissionError = null;
342345

343346
const { success, error, data, refreshArgs } = yield submitForm( context.formHash );
344347

345348
if ( success ) {
346349
setSubmissionData( data );
347-
context.submissionError = null;
348350
context.submissionSuccess = true;
349351

350352
if ( refreshArgs ) {
@@ -357,6 +359,15 @@ const { state } = store( NAMESPACE, {
357359
}
358360
} else {
359361
context.submissionError = error;
362+
363+
if ( errorTimeout ) {
364+
clearTimeout( errorTimeout );
365+
}
366+
367+
errorTimeout = setTimeout( () => {
368+
context.submissionError = null;
369+
}, 5000 );
370+
360371
setSubmissionData( [] );
361372
}
362373

0 commit comments

Comments
 (0)