@@ -10,6 +10,9 @@ function FBAform(d, N) {
1010 formElement : function ( ) {
1111 return this . formComponent ( ) . querySelector ( "form" ) ;
1212 } ,
13+ formLocalStorageKey : function ( ) {
14+ return `touchpoints:${ this . options . formId } `
15+ } ,
1316 isFormSubmitted : false , // defaults to false
1417 // enable Javascript experience
1518 javascriptIsEnabled : function ( ) {
@@ -38,9 +41,10 @@ function FBAform(d, N) {
3841 if ( this . options . formSpecificScript ) {
3942 this . options . formSpecificScript ( ) ;
4043 }
41- < % if form . enable_turnstile ? % >
42- this . loadTurnstile ( )
44+ < % - if form . enable_turnstile ? % >
45+ this . loadTurnstile ( ) ;
4346 < % end % >
47+ this.enableLocalStorage();
4448 d.dispatchEvent(new CustomEvent('onTouchpointsFormLoaded', {
4549 detail : {
4650 formComponent : this
@@ -433,6 +437,7 @@ function FBAform(d, N) {
433437 if (formElement) {
434438 // And clear the Form's Fields
435439 formElement.reset();
440+ localStorage.removeItem(this.formLocalStorageKey());
436441 if (formElement.querySelector('.touchpoints-form-body')) {
437442 var formBody = formElement.querySelector('.touchpoints-form-body');
438443 if(formBody) {
@@ -555,7 +560,7 @@ function FBAform(d, N) {
555560 xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8;");
556561 xhr.onload = callback.bind(this);
557562 xhr.send(JSON.stringify({
558- <% if form.enable_turnstile? %>
563+ <%- if form.enable_turnstile? %>
559564 "cf-turnstile-response" : form.querySelector("input[name='cf-turnstile-response']") ? form.querySelector("input[name='cf-turnstile-response']").value : null,
560565 <% end %>
561566 "submission": params,
@@ -645,23 +650,46 @@ function FBAform(d, N) {
645650 modalId: function() {
646651 return ` fba - modal - $ { this . options . formId } `;
647652 },
648- <% if form.enable_turnstile? %>
653+ <%- if form.enable_turnstile? %>
649654 loadTurnstile: function() {
650655 let script = document.createElement("script");
651656 script.src = "https://challenges.cloudflare.com/turnstile/v0/api.js";
652657 script.async = true;
653658 script.defer = true;
654- script.onload = function() {
655- document.querySelector("input[name='cf-turnstile-response']").value = token;
656- };
657659 document.head.appendChild(script);
658660 },
659661 <% end %>
662+ enableLocalStorage: function() {
663+ const form = this.formElement();
664+ const savedData = localStorage.getItem(this.formLocalStorageKey());
665+
666+ <%# Restore form data from localStorage %>
667+ if (savedData) {
668+ const formData = JSON.parse(savedData);
669+ for (const key in formData) {
670+ const input = form.querySelector(` [ name = "${key}" ] `);
671+ if (input) {
672+ input.value = formData[key];
673+ }
674+ }
675+ }
676+
677+ <%# Save data to localStorage as the user types %>
678+ form.addEventListener('input', (event) => {
679+ const inputData = {};
680+ const formData = new FormData(form);
681+ formData.forEach((value, key) => {
682+ inputData[key] = value;
683+ });
684+
685+ localStorage.setItem(this.formLocalStorageKey(), JSON.stringify(inputData));
686+ });
687+ },
660688 };
661689};
662690
663691// Specify the options for your form
664- const touchpointFormOptions<%= form.short_uuid %> = {
692+ var touchpointFormOptions<%= form.short_uuid %> = {
665693 'formId': "<%= form.short_uuid %>",
666694 'modalButtonText': "<%= form.modal_button_text %>",
667695 'elementSelector': "<%= form.element_selector %>",
@@ -703,10 +731,10 @@ const touchpointFormOptions<%= form.short_uuid %> = {
703731}
704732
705733// Create an instance of a Touchpoints form object
706- const touchpointForm<%= form.short_uuid %> = new FBAform(document, window).init(touchpointFormOptions<%= form.short_uuid %>);
734+ window.touchpointForm<%= form.short_uuid %> = new FBAform(document, window);
735+ window.touchpointForm<%= form.short_uuid %>.init(touchpointFormOptions<%= form.short_uuid %>);
707736
708737<%- if form.load_css && form.delivery_method != "touchpoints-hosted-only" %>
709-
710738// Load the USWDS JS, loads as module 'fbaUswds' in global scope
711739<%= render partial: 'components/widget/widget-uswds', formats: :js %>
712740
@@ -724,5 +752,4 @@ const touchpointForm<%= form.short_uuid %> = new FBAform(document, window).init(
724752 fbaUswds . Modal . on ( fbaModalElement ) ;
725753 }
726754} ) ( ) ;
727-
728755< % end % >
0 commit comments