Browse Source

Update conditional question controller to work with both log types

pull/909/head^2
Kat 4 years ago
parent
commit
b7568c69c0
  1. 11
      app/frontend/controllers/conditional_question_controller.js
  2. 2
      app/helpers/question_attribute_helper.rb
  3. 18
      spec/features/form/conditional_questions_spec.rb

11
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 {

2
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

18
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

Loading…
Cancel
Save