diff --git a/app/admin/form.rb b/app/admin/form.rb index 0363616..4db17ea 100644 --- a/app/admin/form.rb +++ b/app/admin/form.rb @@ -4,7 +4,7 @@ menu priority: 10 permit_params :name, :style, :title, :body, :redirect_url, :action, :success, :created_by_id, :use_recaptcha, :recaptcha_key, :recaptcha_secret, :origin, - form_fields_attributes: [:id, :field_id, :label, :help, :required, :placeholder, :position, :_destroy, + form_fields_attributes: [:id, :field_id, :label, :help, :required, :placeholder, :position, :persist, :_destroy, campaign_options_attributes: %i[id campaign_code label position _destroy],], campaign_codes: [] @@ -87,6 +87,7 @@ fields_f.input :placeholder, hint: "Override field placeholder." fields_f.input :help, hint: "Optional help message." fields_f.input :required, as: :boolean, label: "Is field required?" + fields_f.input :persist, as: :boolean, label: "Store/retrieve value from other forms?" fields_f.has_many :campaign_options, allow_destroy: true, sortable: :position, heading: "Campaigns" do |campaigns_f| campaigns_f.inputs do diff --git a/app/javascript/campaign-form.js b/app/javascript/campaign-form.js index bc5da52..69e21fd 100644 --- a/app/javascript/campaign-form.js +++ b/app/javascript/campaign-form.js @@ -42,6 +42,17 @@ if (typeof window.campaignForms === 'undefined') { window._satellite.track('aa-email-signup') } + const cfPersisted = Array.from( + document.querySelectorAll("input[cfpersisted]") + ).map(({ id, value }) => ({ + key: id.match(/(.*)_\d+/)[1], + value + })); + sessionStorage.setItem( + "campaignFormsPersistedInputs", + JSON.stringify(cfPersisted) + ); + // Call optional success callback if defined if (typeof campaignForm.successCallback === 'function') { window.campaignForm.successCallback(data.master_person_id) @@ -136,6 +147,16 @@ if (typeof window.campaignForms === 'undefined') { } } }) + const cfPersisted = JSON.parse( + sessionStorage.getItem("campaignFormsPersistedInputs") + ); + cfPersisted.forEach(({ key, value }) => { + const element = document.querySelector(`input[cfpersisted][id^='${key}']`); + if (element) { + element.value = value; + element.readOnly = true; + } + }); } })(jQuery) } @@ -155,4 +176,4 @@ $(document).ready(function() { $("[id^='cf_US_State_']").show() } }) -}) \ No newline at end of file +}) diff --git a/app/views/fields/_input.html.erb b/app/views/fields/_input.html.erb index e0e0265..ef55390 100644 --- a/app/views/fields/_input.html.erb +++ b/app/views/fields/_input.html.erb @@ -1,4 +1,4 @@
- " class="form-control" type="<%= field.input %>" name="<%= field.name %>" placeholder="<%= field.placeholder_value %>" <%== 'required' if field.required %>> + " class="form-control"<%= ' cfpersisted' if field.persist %> type="<%= field.input %>" name="<%= field.name %>" placeholder="<%= field.placeholder_value %>" <%== 'required' if field.required %>>
diff --git a/db/migrate/20200727214421_add_persist_to_form_fields.rb b/db/migrate/20200727214421_add_persist_to_form_fields.rb new file mode 100644 index 0000000..301314c --- /dev/null +++ b/db/migrate/20200727214421_add_persist_to_form_fields.rb @@ -0,0 +1,5 @@ +class AddPersistToFormFields < ActiveRecord::Migration[6.0] + def change + add_column :form_fields, :persist, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 9e48aec..c94c17b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_07_08_170559) do +ActiveRecord::Schema.define(version: 2020_07_27_214421) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -57,6 +57,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "placeholder" + t.boolean "persist", default: false t.index ["field_id"], name: "index_form_fields_on_field_id" t.index ["form_id"], name: "index_form_fields_on_form_id" end