diff --git a/app/frontend/controllers/conditional_question_controller.js b/app/frontend/controllers/conditional_question_controller.js index 974deeb2e..f84f6a7d8 100644 --- a/app/frontend/controllers/conditional_question_controller.js +++ b/app/frontend/controllers/conditional_question_controller.js @@ -7,15 +7,20 @@ export default class extends Controller { displayConditional () { if (this.element.checked) { + console.log(this.element) + const selectedValue = this.element.value - const conditionalFor = JSON.parse(this.element.dataset.info) + + const dataInfo = JSON.parse(this.element.dataset.info) + const conditionalFor = dataInfo.conditional_questions + const logType = dataInfo.log_type Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => { if (!conditions.map(String).includes(String(selectedValue))) { - const textNumericInput = document.getElementById(`lettings-log-${targetQuestion.replaceAll('_', '-')}-field`) + const textNumericInput = document.getElementById(`${logType}-log-${targetQuestion.replaceAll('_', '-')}-field`) if (textNumericInput == null) { const dateInputs = [1, 2, 3].map((idx) => { - return document.getElementById(`lettings_log_${targetQuestion}_${idx}i`) + return document.getElementById(`${logType}_log_${targetQuestion}_${idx}i`) }) this.clearDateInputs(dateInputs) } else { diff --git a/app/helpers/question_attribute_helper.rb b/app/helpers/question_attribute_helper.rb index f2f148568..857ce5eb1 100644 --- a/app/helpers/question_attribute_helper.rb +++ b/app/helpers/question_attribute_helper.rb @@ -27,7 +27,7 @@ private { "data-controller": "conditional-question", "data-action": "click->conditional-question#displayConditional", - "data-info": question.conditional_for.to_json, + "data-info": { conditional_questions: question.conditional_for, log_type: question.form.type }.to_json, } end end diff --git a/spec/features/form/conditional_questions_spec.rb b/spec/features/form/conditional_questions_spec.rb index 4f3f44b5a..e68cdf098 100644 --- a/spec/features/form/conditional_questions_spec.rb +++ b/spec/features/form/conditional_questions_spec.rb @@ -12,6 +12,14 @@ RSpec.describe "Form Conditional Questions" do managing_organisation: user.organisation, ) end + let(:sales_log) do + FactoryBot.create( + :sales_log, + :completed, + owning_organisation: user.organisation, + managing_organisation: user.organisation, + ) + end let(:id) { lettings_log.id } let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } @@ -44,5 +52,15 @@ RSpec.describe "Form Conditional Questions" do visit("/lettings-logs/#{id}/property-postcode") expect(page).to have_field("lettings-log-postcode-full-field", with: "NW1 6RT") end + + it "gets cleared if the conditinoal question is hidden after editing the answer" do + sales_log.update!(national: 12, othernational: "other") + visit("/sales-logs/#{sales_log.id}/buyer-1-nationality") + expect(page).to have_field("sales-log-othernational-field", with: "other") + + choose("sales-log-national-18-field", allow_label_click: true) + choose("sales-log-national-12-field", allow_label_click: true) + expect(page).to have_field("sales-log-othernational-field", with: "") + end end end