From 9a393a0725c99b3665d09b99e561106928b6c905 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 9 Nov 2022 13:28:06 +0000 Subject: [PATCH] Change conditional question controller to accommodate all models --- .../controllers/conditional_question_controller.js | 6 +++--- app/helpers/question_attribute_helper.rb | 10 +++++++++- spec/helpers/question_attribute_helper_spec.rb | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/frontend/controllers/conditional_question_controller.js b/app/frontend/controllers/conditional_question_controller.js index fb52d07c1..d24d5c6c3 100644 --- a/app/frontend/controllers/conditional_question_controller.js +++ b/app/frontend/controllers/conditional_question_controller.js @@ -10,14 +10,14 @@ export default class extends Controller { const selectedValue = this.element.value const dataInfo = JSON.parse(this.element.dataset.info) const conditionalFor = dataInfo.conditional_questions - const logType = dataInfo.log_type + const type = dataInfo.type Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => { if (!conditions.map(String).includes(String(selectedValue))) { - const textNumericInput = document.getElementById(`${logType}-log-${targetQuestion.replaceAll('_', '-')}-field`) + const textNumericInput = document.getElementById(`${type}-${targetQuestion.replaceAll('_', '-')}-field`) if (textNumericInput == null) { const dateInputs = [1, 2, 3].map((idx) => { - return document.getElementById(`${logType}_log_${targetQuestion}_${idx}i`) + return document.getElementById(`${type.replaceAll('-', '_')}_${targetQuestion}_${idx}i`) }) this.clearDateInputs(dateInputs) } else { diff --git a/app/helpers/question_attribute_helper.rb b/app/helpers/question_attribute_helper.rb index 857ce5eb1..020ea1909 100644 --- a/app/helpers/question_attribute_helper.rb +++ b/app/helpers/question_attribute_helper.rb @@ -7,6 +7,14 @@ module QuestionAttributeHelper merge_controller_attributes(*attribs) end + def basic_conditional_html_attributes(conditional_for, type) + { + "data-controller": "conditional-question", + "data-action": "click->conditional-question#displayConditional", + "data-info": { conditional_questions: conditional_for, type: type }.to_json, + } + end + private def numeric_question_html_attributes(question) @@ -27,7 +35,7 @@ private { "data-controller": "conditional-question", "data-action": "click->conditional-question#displayConditional", - "data-info": { conditional_questions: question.conditional_for, log_type: question.form.type }.to_json, + "data-info": { conditional_questions: question.conditional_for, type: "#{question.form.type}-log" }.to_json, } end end diff --git a/spec/helpers/question_attribute_helper_spec.rb b/spec/helpers/question_attribute_helper_spec.rb index 2be903535..9d769cdf6 100644 --- a/spec/helpers/question_attribute_helper_spec.rb +++ b/spec/helpers/question_attribute_helper_spec.rb @@ -48,7 +48,7 @@ RSpec.describe QuestionAttributeHelper do "data-action": "input->numeric-question#calculateFields click->conditional-question#displayConditional", "data-target": "lettings-log-#{question.result_field.to_s.dasherize}-field", "data-calculated": question.fields_to_add.to_json, - "data-info": { conditional_questions: question.conditional_for, log_type: "lettings" }.to_json, + "data-info": { conditional_questions: question.conditional_for, type: "lettings-log" }.to_json, } end