@@ -38,6 +38,9 @@ function FBAform(d, N) {
3838 if ( this . options . formSpecificScript ) {
3939 this . options . formSpecificScript ( ) ;
4040 }
41+ < % if form . enable_turnstile ? % >
42+ this . loadTurnstile ( )
43+ < % end % >
4144 d . dispatchEvent ( new CustomEvent ( 'onTouchpointsFormLoaded' , {
4245 detail : {
4346 formComponent : this
@@ -413,16 +416,16 @@ function FBAform(d, N) {
413416 successText: function() {
414417 return this.options.successText;
415418 },
416- showFormSuccess: function(e ) {
419+ showFormSuccess: function(headerText, bodyHTML ) {
417420 var formComponent = this.formComponent();
418421 var formElement = this.formElement();
419422 var alertElement = formComponent.querySelector(".fba-alert");
420423 var alertElementHeading = formComponent.querySelector(".usa-alert__heading");
421424 var alertElementBody = formComponent.querySelector(".usa-alert__text");
422425
423426 // Display success Message
424- alertElementHeading.innerHTML += this.successHeadingText() ;
425- alertElementBody.innerHTML = this.successText();
427+ alertElementHeading.innerHTML = headerText ;
428+ alertElementBody.innerHTML = bodyHTML
426429 alertElement.removeAttribute("hidden");
427430 this.formComponent().scrollIntoView();
428431
@@ -489,7 +492,11 @@ function FBAform(d, N) {
489492 if(submitButton) {
490493 submitButton.disabled = true;
491494 }
492- this.showFormSuccess();
495+
496+ const submission = JSON.parse(e.target.response).submission;
497+ const successHeaderText = submission.form.success_text_heading;
498+ const successBodyText = submission.form.success_text;
499+ this.showFormSuccess(successHeaderText, successBodyText);
493500 } else if (e.target.status === 422) { // FORM ERRORS
494501 this.successState = false;
495502 d.dispatchEvent(new Event('onTouchpointsFormSubmissionError'));
@@ -548,6 +555,9 @@ function FBAform(d, N) {
548555 xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8;");
549556 xhr.onload = callback.bind(this);
550557 xhr.send(JSON.stringify({
558+ <% if form.enable_turnstile? %>
559+ "cf-turnstile-response" : form.querySelector("input[name='cf-turnstile-response']") ? form.querySelector("input[name='cf-turnstile-response']").value : null,
560+ <% end %>
551561 "submission": params,
552562 <%- if form.verify_csrf? %>
553563 "authenticity_token": form.querySelector("#authenticity_token") ?
@@ -635,6 +645,18 @@ function FBAform(d, N) {
635645 modalId: function() {
636646 return ` fba - modal - $ { this . options . formId } `;
637647 },
648+ <% if form.enable_turnstile? %>
649+ loadTurnstile: function() {
650+ let script = document.createElement("script");
651+ script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js";
652+ script.async = true;
653+ script.defer = true;
654+ script.onload = function() {
655+ document.querySelector("input[name='cf-turnstile-response']").value = token;
656+ };
657+ document.head.appendChild(script);
658+ },
659+ <% end %>
638660 };
639661};
640662
0 commit comments