From 0b47b5ccd4961e9ce8b12c1c6ab3c89273530782 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 7 Oct 2021 09:16:37 +0100 Subject: [PATCH] Cldc 340 read only questions (#34) --- app/helpers/numeric_questions_helper.rb | 12 +++++++++ .../numeric_question_controller.js | 12 +++++++++ app/views/form/_numeric_question.html.erb | 4 ++- config/forms/2021_2022.json | 27 +++++++++++++++---- spec/features/case_log_spec.rb | 16 +++++++++++ spec/helpers/numeric_questions_helper_spec.rb | 21 +++++++++++++++ 6 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 app/helpers/numeric_questions_helper.rb create mode 100644 app/javascript/controllers/numeric_question_controller.js create mode 100644 spec/helpers/numeric_questions_helper_spec.rb diff --git a/app/helpers/numeric_questions_helper.rb b/app/helpers/numeric_questions_helper.rb new file mode 100644 index 000000000..464f42723 --- /dev/null +++ b/app/helpers/numeric_questions_helper.rb @@ -0,0 +1,12 @@ +module NumericQuestionsHelper + def numeric_question_html_attributes(question) + return {} if question["fields-to-add"].blank? || question["result-field"].blank? + + { + "data-controller": "numeric-question", + "data-action": "numeric-question#calculateFields", + "data-target": "#{question['result-field'].to_s.dasherize}-field", + "data-calculated": question["fields-to-add"].to_json, + } + end +end diff --git a/app/javascript/controllers/numeric_question_controller.js b/app/javascript/controllers/numeric_question_controller.js new file mode 100644 index 000000000..6a0a58d78 --- /dev/null +++ b/app/javascript/controllers/numeric_question_controller.js @@ -0,0 +1,12 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + calculateFields() { + const affectedField = this.element.dataset.target; + const fieldsToAdd = JSON.parse(this.element.dataset.calculated).map(x => `${x.replaceAll("_","-")}-field`); + const valuesToAdd = fieldsToAdd.map(x => document.getElementById(x).value).filter(x => x); + const newValue = valuesToAdd.map(x => parseInt(x)).reduce((a, b) => a + b, 0); + const elementToUpdate = document.getElementById(affectedField); + elementToUpdate.value = newValue; + } +} diff --git a/app/views/form/_numeric_question.html.erb b/app/views/form/_numeric_question.html.erb index 1b98ed158..6de29a111 100644 --- a/app/views/form/_numeric_question.html.erb +++ b/app/views/form/_numeric_question.html.erb @@ -1,5 +1,7 @@ <%= f.govuk_number_field question_key, hint: { text: question["hint_text"] }, label: { text: question["header"].html_safe, size: "l"}, - min: question["min"], max: question["max"], step: question["step"], width: 20 + min: question["min"], max: question["max"], step: question["step"], + width: 20, :readonly => question["readonly"], + **numeric_question_html_attributes(question) %> diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index cc552eb9f..a5b3072ce 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -287,6 +287,7 @@ "header": "Has the tenant ever served in the UK armed forces?", "hint_text": "", "type": "radio", + "check_answer_label": "Armed Forces", "answer_options": { "0": "Yes - a regular", "1": "Yes - a reserve", @@ -302,6 +303,7 @@ "header": "Are they still serving?", "hint_text": "", "type": "radio", + "check_answer_label": "When did they leave the Armed Forces?", "answer_options": { "0": "Yes", "1": "No - they left up to 5 years ago", @@ -313,6 +315,7 @@ "header": "Were they seriously injured or ill as a result of their service?", "hint_text": "", "type": "radio", + "check_answer_label": "Has anyone in the household been seriously injured or ill as a result of their service in the armed forces?", "answer_options": { "0": "Yes", "1": "No", @@ -323,6 +326,7 @@ "header": "Was the tenant the spouse or civil partner of someone who served in the UK armed forces?", "hint_text": "", "type": "radio", + "check_answer_label": "Was the tenant the spouse or civil partner of someone who served in the UK armed forces?", "answer_options": { "0": "Yes - was the spouse or civil partner of a UK Armed Forces member and have separated within the last 2 years", "1": "Yes - was the spouse or civil partner of a UK Armed Forces member who died within the last 2 years", @@ -340,6 +344,7 @@ "header": "Does anyone in the household have any of the following that they expect to last for 12 months or more:", "hint_text": "", "type": "radio", + "check_answer_label": "Physical, mental health or illness in the household", "answer_options": { "0": "Yes", "1": "No", @@ -357,6 +362,7 @@ "header": "Is anyone in the household pregnant?", "hint_text": "", "type": "radio", + "check_answer_label": "Pregnancy in the household", "answer_options": { "0": "Yes", "1": "No", @@ -373,6 +379,7 @@ "header": "Are any of these affected by their condition or illness?", "hint_text": "Select all that apply", "type": "checkbox", + "check_answer_label": "Disability requirements", "answer_options": { "0": "Fully wheelchair accessible housing", "1": "Wheelchair access to essential rooms", @@ -395,6 +402,7 @@ "header": "Are any of these affected by their condition or illness?", "hint_text": "Select all that apply", "type": "checkbox", + "check_answer_label": "Conditions or illnesses", "answer_options": { "0": "Vision - such as blindness or partial sight", "1": "Hearing - such as deafness or partial hearing", @@ -1136,7 +1144,9 @@ "hint_text": "Eligible for housing benefit or Universal Credit", "type": "numeric", "min": 0, - "step": 1 + "step": 1, + "fields-to-add": ["basic_rent", "service_charge", "personal_service_charge", "support_charge"], + "result-field": "total_charge" }, "service_charge": { "check_answer_label": "Service Charge", @@ -1144,7 +1154,9 @@ "hint_text": "Eligible for housing benefit or Universal Credit", "type": "numeric", "min": 0, - "step": 1 + "step": 1, + "fields-to-add": ["basic_rent", "service_charge", "personal_service_charge", "support_charge"], + "result-field": "total_charge" }, "personal_service_charge": { "check_answer_label": "Personal Service Charge", @@ -1152,7 +1164,9 @@ "hint_text": "Not eligible for housing benefit or Universal Credit. For example, hot water excluding water rates.", "type": "numeric", "min": 0, - "step": 1 + "step": 1, + "fields-to-add": ["basic_rent", "service_charge", "personal_service_charge", "support_charge"], + "result-field": "total_charge" }, "support_charge": { "check_answer_label": "Support Charge", @@ -1160,7 +1174,9 @@ "hint_text": "This is to fund housing-related support services included in the tenancy agreement", "type": "numeric", "min": 0, - "step": 1 + "step": 1, + "fields-to-add": ["basic_rent", "service_charge", "personal_service_charge", "support_charge"], + "result-field": "total_charge" }, "total_charge": { "check_answer_label": "Total Charge", @@ -1168,7 +1184,8 @@ "hint_text": "This is the total of rent and all charges", "type": "numeric", "min": 0, - "step": 1 + "step": 1, + "readonly": true }, "outstanding_amount": { "check_answer_label": "After housing benefit and/or housing element of UC payment is received, will there be an outstanding amount for basic rent and/or benefit eligible charges?", diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index 46dde63c7..3f2b8039f 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -114,6 +114,22 @@ RSpec.describe "Test Features" do }.from(original_value).to(answer) end end + + it "updates total value of the rent", js: true do + visit("/case_logs/#{id}/rent") + + fill_in("basic_rent", with: 3) + expect(page).to have_field("total-charge-field", with: "3") + + fill_in("service_charge", with: 2) + expect(page).to have_field("total-charge-field", with: "5") + + fill_in("personal_service_charge", with: 1) + expect(page).to have_field("total-charge-field", with: "6") + + fill_in("support_charge", with: 4) + expect(page).to have_field("total-charge-field", with: "10") + end end describe "Back link directs correctly" do diff --git a/spec/helpers/numeric_questions_helper_spec.rb b/spec/helpers/numeric_questions_helper_spec.rb new file mode 100644 index 000000000..48877babe --- /dev/null +++ b/spec/helpers/numeric_questions_helper_spec.rb @@ -0,0 +1,21 @@ +require "rails_helper" + +RSpec.describe NumericQuestionsHelper do + let(:form) { Form.new(2021, 2022) } + let(:questions) { form.questions_for_page("rent") } + + describe "html attributes" do + it "returns empty hash if fields-to-add or result-field are empty " do + expect(numeric_question_html_attributes(questions["total_charge"])).to eq({}) + end + + it "returns html attributes if fields-to-add or result-field are not empty " do + expect(numeric_question_html_attributes(questions["basic_rent"])).to eq({ + "data-controller": "numeric-question", + "data-action": "numeric-question#calculateFields", + "data-target": "#{questions['basic_rent']['result-field'].to_s.dasherize}-field", + "data-calculated": questions["basic_rent"]["fields-to-add"].to_json, + }) + end + end +end