Browse Source

Change conditional question controller to accommodate all models

pull/980/head
Kat 2 years ago committed by natdeanlewissoftwire
parent
commit
9a393a0725
  1. 6
      app/frontend/controllers/conditional_question_controller.js
  2. 10
      app/helpers/question_attribute_helper.rb
  3. 2
      spec/helpers/question_attribute_helper_spec.rb

6
app/frontend/controllers/conditional_question_controller.js

@ -10,14 +10,14 @@ export default class extends Controller {
const selectedValue = this.element.value const selectedValue = this.element.value
const dataInfo = JSON.parse(this.element.dataset.info) const dataInfo = JSON.parse(this.element.dataset.info)
const conditionalFor = dataInfo.conditional_questions const conditionalFor = dataInfo.conditional_questions
const logType = dataInfo.log_type const type = dataInfo.type
Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => { Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => {
if (!conditions.map(String).includes(String(selectedValue))) { 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) { if (textNumericInput == null) {
const dateInputs = [1, 2, 3].map((idx) => { 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) this.clearDateInputs(dateInputs)
} else { } else {

10
app/helpers/question_attribute_helper.rb

@ -7,6 +7,14 @@ module QuestionAttributeHelper
merge_controller_attributes(*attribs) merge_controller_attributes(*attribs)
end 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 private
def numeric_question_html_attributes(question) def numeric_question_html_attributes(question)
@ -27,7 +35,7 @@ private
{ {
"data-controller": "conditional-question", "data-controller": "conditional-question",
"data-action": "click->conditional-question#displayConditional", "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
end end

2
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-action": "input->numeric-question#calculateFields click->conditional-question#displayConditional",
"data-target": "lettings-log-#{question.result_field.to_s.dasherize}-field", "data-target": "lettings-log-#{question.result_field.to_s.dasherize}-field",
"data-calculated": question.fields_to_add.to_json, "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 end

Loading…
Cancel
Save