Browse Source

Cldc 340 read only questions (#34)

pull/37/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
0b47b5ccd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      app/helpers/numeric_questions_helper.rb
  2. 12
      app/javascript/controllers/numeric_question_controller.js
  3. 4
      app/views/form/_numeric_question.html.erb
  4. 27
      config/forms/2021_2022.json
  5. 16
      spec/features/case_log_spec.rb
  6. 21
      spec/helpers/numeric_questions_helper_spec.rb

12
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

12
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;
}
}

4
app/views/form/_numeric_question.html.erb

@ -1,5 +1,7 @@
<%= f.govuk_number_field question_key, <%= f.govuk_number_field question_key,
hint: { text: question["hint_text"] }, hint: { text: question["hint_text"] },
label: { text: question["header"].html_safe, size: "l"}, 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)
%> %>

27
config/forms/2021_2022.json

@ -287,6 +287,7 @@
"header": "Has the tenant ever served in the UK armed forces?", "header": "Has the tenant ever served in the UK armed forces?",
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",
"check_answer_label": "Armed Forces",
"answer_options": { "answer_options": {
"0": "Yes - a regular", "0": "Yes - a regular",
"1": "Yes - a reserve", "1": "Yes - a reserve",
@ -302,6 +303,7 @@
"header": "Are they still serving?", "header": "Are they still serving?",
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",
"check_answer_label": "When did they leave the Armed Forces?",
"answer_options": { "answer_options": {
"0": "Yes", "0": "Yes",
"1": "No - they left up to 5 years ago", "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?", "header": "Were they seriously injured or ill as a result of their service?",
"hint_text": "", "hint_text": "",
"type": "radio", "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": { "answer_options": {
"0": "Yes", "0": "Yes",
"1": "No", "1": "No",
@ -323,6 +326,7 @@
"header": "Was the tenant the spouse or civil partner of someone who served in the UK armed forces?", "header": "Was the tenant the spouse or civil partner of someone who served in the UK armed forces?",
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",
"check_answer_label": "Was the tenant the spouse or civil partner of someone who served in the UK armed forces?",
"answer_options": { "answer_options": {
"0": "Yes - was the spouse or civil partner of a UK Armed Forces member and have separated within the last 2 years", "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", "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:<ul><li>Physical Condition</li><li>Mental Health Condition</li><li>Other Illness</li></ul>", "header": "Does anyone in the household have any of the following that they expect to last for 12 months or more:<ul><li>Physical Condition</li><li>Mental Health Condition</li><li>Other Illness</li></ul>",
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",
"check_answer_label": "Physical, mental health or illness in the household",
"answer_options": { "answer_options": {
"0": "Yes", "0": "Yes",
"1": "No", "1": "No",
@ -357,6 +362,7 @@
"header": "Is anyone in the household pregnant?", "header": "Is anyone in the household pregnant?",
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",
"check_answer_label": "Pregnancy in the household",
"answer_options": { "answer_options": {
"0": "Yes", "0": "Yes",
"1": "No", "1": "No",
@ -373,6 +379,7 @@
"header": "Are any of these affected by their condition or illness?", "header": "Are any of these affected by their condition or illness?",
"hint_text": "Select all that apply", "hint_text": "Select all that apply",
"type": "checkbox", "type": "checkbox",
"check_answer_label": "Disability requirements",
"answer_options": { "answer_options": {
"0": "Fully wheelchair accessible housing", "0": "Fully wheelchair accessible housing",
"1": "Wheelchair access to essential rooms", "1": "Wheelchair access to essential rooms",
@ -395,6 +402,7 @@
"header": "Are any of these affected by their condition or illness?", "header": "Are any of these affected by their condition or illness?",
"hint_text": "Select all that apply", "hint_text": "Select all that apply",
"type": "checkbox", "type": "checkbox",
"check_answer_label": "Conditions or illnesses",
"answer_options": { "answer_options": {
"0": "Vision - such as blindness or partial sight", "0": "Vision - such as blindness or partial sight",
"1": "Hearing - such as deafness or partial hearing", "1": "Hearing - such as deafness or partial hearing",
@ -1136,7 +1144,9 @@
"hint_text": "Eligible for housing benefit or Universal Credit", "hint_text": "Eligible for housing benefit or Universal Credit",
"type": "numeric", "type": "numeric",
"min": 0, "min": 0,
"step": 1 "step": 1,
"fields-to-add": ["basic_rent", "service_charge", "personal_service_charge", "support_charge"],
"result-field": "total_charge"
}, },
"service_charge": { "service_charge": {
"check_answer_label": "Service Charge", "check_answer_label": "Service Charge",
@ -1144,7 +1154,9 @@
"hint_text": "Eligible for housing benefit or Universal Credit", "hint_text": "Eligible for housing benefit or Universal Credit",
"type": "numeric", "type": "numeric",
"min": 0, "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": { "personal_service_charge": {
"check_answer_label": "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.", "hint_text": "Not eligible for housing benefit or Universal Credit. For example, hot water excluding water rates.",
"type": "numeric", "type": "numeric",
"min": 0, "min": 0,
"step": 1 "step": 1,
"fields-to-add": ["basic_rent", "service_charge", "personal_service_charge", "support_charge"],
"result-field": "total_charge"
}, },
"support_charge": { "support_charge": {
"check_answer_label": "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", "hint_text": "This is to fund housing-related support services included in the tenancy agreement",
"type": "numeric", "type": "numeric",
"min": 0, "min": 0,
"step": 1 "step": 1,
"fields-to-add": ["basic_rent", "service_charge", "personal_service_charge", "support_charge"],
"result-field": "total_charge"
}, },
"total_charge": { "total_charge": {
"check_answer_label": "Total Charge", "check_answer_label": "Total Charge",
@ -1168,7 +1184,8 @@
"hint_text": "This is the total of rent and all charges", "hint_text": "This is the total of rent and all charges",
"type": "numeric", "type": "numeric",
"min": 0, "min": 0,
"step": 1 "step": 1,
"readonly": true
}, },
"outstanding_amount": { "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?", "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?",

16
spec/features/case_log_spec.rb

@ -114,6 +114,22 @@ RSpec.describe "Test Features" do
}.from(original_value).to(answer) }.from(original_value).to(answer)
end end
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 end
describe "Back link directs correctly" do describe "Back link directs correctly" do

21
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
Loading…
Cancel
Save