Browse Source

view component

pull/832/head
Ted-U 3 years ago
parent
commit
f5b8fa5388
  1. 14
      app/components/check_answers_summary_list_card_component.html.erb
  2. 18
      app/components/check_answers_summary_list_card_component.rb
  3. 4
      app/helpers/check_answers_helper.rb
  4. 3
      app/models/form/question.rb
  5. 1
      app/models/form/subsection.rb
  6. 5
      app/views/form/check_answers.html.erb
  7. 5
      app/views/form/review.html.erb
  8. 10
      config/forms/2021_2022.json
  9. 16
      spec/components/check_answers_summary_list_card_component_spec.rb

14
app/views/form/_check_answers_summary_list.html.erb → app/components/check_answers_summary_list_card_component.html.erb

@ -1,23 +1,25 @@
<div class="check-answers-summary-list">
<%= govuk_summary_list do |summary_list| %> <%= govuk_summary_list do |summary_list| %>
<% total_applicable_questions(subsection, @case_log, current_user).each do |question| %> <% applicable_questions.each do |question| %>
<% summary_list.row do |row| %> <% summary_list.row do |row| %>
<% row.key { question.check_answer_label.to_s.presence || question.header.to_s } %> <% row.key { question.check_answer_label.to_s.presence || question.header.to_s } %>
<% row.value do %> <% row.value do %>
<span class="govuk-!-margin-right-4"><%= get_answer_label(question, @case_log) %></span> <span class="govuk-!-margin-right-4"><%= get_answer_label(question) %></span>
<% extra_value = question.get_extra_check_answer_value(@case_log) %> <% extra_value = question.get_extra_check_answer_value(case_log) %>
<% if extra_value %> <% if extra_value %>
<span class="govuk-!-font-weight-regular app-!-colour-muted"><%= extra_value %></span> <span class="govuk-!-font-weight-regular app-!-colour-muted"><%= extra_value %></span>
<% end %> <% end %>
<br> <br>
<% question.get_inferred_answers(@case_log).each do |inferred_answer| %> <% question.get_inferred_answers(case_log).each do |inferred_answer| %>
<span class="govuk-!-font-weight-regular app-!-colour-muted"><%= inferred_answer %></span> <span class="govuk-!-font-weight-regular app-!-colour-muted"><%= inferred_answer %></span>
<% end %> <% end %>
<% end %> <% end %>
<% row.action( <% row.action(
text: question.action_text(@case_log), text: question.action_text(case_log),
href: question.action_href(@case_log, question.page.id), href: question.action_href(case_log, question.page.id),
visually_hidden_text: question.check_answer_label.to_s.downcase, visually_hidden_text: question.check_answer_label.to_s.downcase,
) %> ) %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>
</div>

18
app/components/check_answers_summary_list_card_component.rb

@ -0,0 +1,18 @@
class CheckAnswersSummaryListCardComponent < ViewComponent::Base
attr_reader :questions, :case_log, :user
def initialize(questions:, case_log:, user:)
@questions = questions
@case_log = case_log
@user = user
super
end
def applicable_questions
questions.reject { |q| q.hidden_in_check_answers?(case_log, user) }
end
def get_answer_label(question)
question.answer_label(case_log).presence || "<span class=\"app-!-colour-muted\">You didn’t answer this question</span>".html_safe
end
end

4
app/helpers/check_answers_helper.rb

@ -28,8 +28,4 @@ private
def total_applicable_questions(subsection, case_log, current_user) def total_applicable_questions(subsection, case_log, current_user)
subsection.applicable_questions(case_log).reject { |q| q.hidden_in_check_answers?(case_log, current_user) } subsection.applicable_questions(case_log).reject { |q| q.hidden_in_check_answers?(case_log, current_user) }
end end
def get_answer_label(question, case_log)
question.answer_label(case_log).presence || "<span class=\"app-!-colour-muted\">You didn’t answer this question</span>".html_safe
end
end end

3
app/models/form/question.rb

@ -3,7 +3,7 @@ class Form::Question
:type, :min, :max, :step, :width, :fields_to_add, :result_field, :type, :min, :max, :step, :width, :fields_to_add, :result_field,
:conditional_for, :readonly, :answer_options, :page, :check_answer_label, :conditional_for, :readonly, :answer_options, :page, :check_answer_label,
:inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value, :inferred_answers, :hidden_in_check_answers, :inferred_check_answers_value,
:guidance_partial, :prefix, :suffix, :requires_js, :fields_added, :derived :guidance_partial, :prefix, :suffix, :requires_js, :fields_added, :derived, :check_answers_card_number
module GuidancePosition module GuidancePosition
TOP = 1 TOP = 1
@ -37,6 +37,7 @@ class Form::Question
@suffix = hsh["suffix"] @suffix = hsh["suffix"]
@requires_js = hsh["requires_js"] @requires_js = hsh["requires_js"]
@fields_added = hsh["fields_added"] @fields_added = hsh["fields_added"]
@check_answers_card_number = hsh["check_answers_card_number"]
end end
end end

1
app/models/form/subsection.rb

@ -34,6 +34,7 @@ class Form::Subsection
qs = applicable_questions(case_log) qs = applicable_questions(case_log)
qs_optional_removed = qs.reject { |q| case_log.optional_fields.include?(q.id) } qs_optional_removed = qs.reject { |q| case_log.optional_fields.include?(q.id) }
binding.pry if id == "income_and_benefits"
return :not_started if qs.count.positive? && qs.all? { |question| case_log[question.id].blank? || question.read_only? || question.derived? } return :not_started if qs.count.positive? && qs.all? { |question| case_log[question.id].blank? || question.read_only? || question.derived? }
return :completed if qs_optional_removed.all? { |question| question.completed?(case_log) } return :completed if qs_optional_removed.all? { |question| question.completed?(case_log) }

5
app/views/form/check_answers.html.erb

@ -17,10 +17,7 @@
<% end %> <% end %>
<%= display_answered_questions_summary(subsection, @case_log, current_user) %> <%= display_answered_questions_summary(subsection, @case_log, current_user) %>
<%= render partial: "form/check_answers_summary_list", locals: { <%= render CheckAnswersSummaryListCardComponent.new(questions: subsection.applicable_questions(@case_log), case_log: @case_log, user: current_user) %>
subsection:,
case_log: @case_log,
} %>
<%= form_with model: @case_log, method: "get" do |f| %> <%= form_with model: @case_log, method: "get" do |f| %>
<%= f.govuk_submit "Save and return to log" do %> <%= f.govuk_submit "Save and return to log" do %>

5
app/views/form/review.html.erb

@ -21,10 +21,7 @@
<h3 class="x-govuk-summary-card__title"><%= subsection.label %></h3> <h3 class="x-govuk-summary-card__title"><%= subsection.label %></h3>
</div> </div>
<div class="x-govuk-summary-card__body"> <div class="x-govuk-summary-card__body">
<%= render partial: "form/check_answers_summary_list", locals: { <%= render CheckAnswersSummaryListCardComponent.new(questions: subsection.applicable_questions(@case_log), case_log: @case_log, user: current_user) %>
subsection:,
case_log: @case_log,
} %>
</div> </div>
</div> </div>
<% end %> <% end %>

10
config/forms/2021_2022.json

@ -1056,6 +1056,7 @@
"header": "", "header": "",
"guidance_partial": "privacy_notice", "guidance_partial": "privacy_notice",
"check_answer_label": "Tenant has seen the privacy notice", "check_answer_label": "Tenant has seen the privacy notice",
"check_answers_card_number": 0,
"type": "checkbox", "type": "checkbox",
"answer_options": { "answer_options": {
"declaration": { "declaration": {
@ -1070,6 +1071,7 @@
"description": "", "description": "",
"questions": { "questions": {
"hhmemb": { "hhmemb": {
"check_answers_card_number": 0,
"check_answer_label": "Number of household members", "check_answer_label": "Number of household members",
"header": "How many people live in the household for this letting?", "header": "How many people live in the household for this letting?",
"hint_text": "You can provide details for a maximum of 8 people.", "hint_text": "You can provide details for a maximum of 8 people.",
@ -1165,6 +1167,7 @@
"description": "", "description": "",
"questions": { "questions": {
"age1_known": { "age1_known": {
"check_answers_card_number": 1,
"header": "Do you know the lead tenant’s age?", "header": "Do you know the lead tenant’s age?",
"hint_text": "The ’lead’ or ’main’ tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.", "hint_text": "The ’lead’ or ’main’ tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.",
"type": "radio", "type": "radio",
@ -1292,6 +1295,7 @@
"questions": { "questions": {
"sex1": { "sex1": {
"check_answer_label": "Lead tenant’s gender identity", "check_answer_label": "Lead tenant’s gender identity",
"check_answers_card_number": 1,
"header": "Which of these best describes the lead tenant’s gender identity?", "header": "Which of these best describes the lead tenant’s gender identity?",
"hint_text": "The lead tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.", "hint_text": "The lead tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.",
"type": "radio", "type": "radio",
@ -1400,6 +1404,7 @@
"questions": { "questions": {
"ethnic_group": { "ethnic_group": {
"check_answer_label": "Lead tenant’s ethnic group", "check_answer_label": "Lead tenant’s ethnic group",
"check_answers_card_number": 0,
"header": "What is the lead tenant’s ethnic group?", "header": "What is the lead tenant’s ethnic group?",
"hint_text": "The lead tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.", "hint_text": "The lead tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.",
"type": "radio", "type": "radio",
@ -1632,6 +1637,7 @@
"questions": { "questions": {
"ecstat1": { "ecstat1": {
"check_answer_label": "Lead tenant’s working situation", "check_answer_label": "Lead tenant’s working situation",
"check_answers_card_number": 0,
"header": "Which of these best describes the lead tenant’s working situation?", "header": "Which of these best describes the lead tenant’s working situation?",
"hint_text": "The lead tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.", "hint_text": "The lead tenant is the person in the household who does the most paid work. If several people do the same paid work, the lead tenant is whoever is the oldest.",
"type": "radio", "type": "radio",
@ -1770,6 +1776,7 @@
"questions": { "questions": {
"details_known_2": { "details_known_2": {
"check_answer_label": "Details known for person 2", "check_answer_label": "Details known for person 2",
"check_answers_card_number": 1,
"header": "Do you know details for person 2?", "header": "Do you know details for person 2?",
"hint_text": "You must provide details for everyone in the household if you know them.", "hint_text": "You must provide details for everyone in the household if you know them.",
"type": "radio", "type": "radio",
@ -1813,6 +1820,7 @@
"questions": { "questions": {
"relat2": { "relat2": {
"check_answer_label": "Person 2’s relationship to the lead tenant", "check_answer_label": "Person 2’s relationship to the lead tenant",
"check_answers_card_number": 1,
"header": "What is person 2’s relationship to the lead tenant?", "header": "What is person 2’s relationship to the lead tenant?",
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",
@ -1986,6 +1994,7 @@
"sex2": { "sex2": {
"check_answer_label": "Person 2’s gender identity", "check_answer_label": "Person 2’s gender identity",
"header": "Which of these best describes person 2’s gender identity?", "header": "Which of these best describes person 2’s gender identity?",
"check_answers_card_number": 1,
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",
"answer_options": { "answer_options": {
@ -2107,6 +2116,7 @@
"questions": { "questions": {
"ecstat2": { "ecstat2": {
"check_answer_label": "Person 2’s working situation", "check_answer_label": "Person 2’s working situation",
"check_answers_card_number": 1,
"header": "Which of these best describes person 2’s working situation?", "header": "Which of these best describes person 2’s working situation?",
"hint_text": "", "hint_text": "",
"type": "radio", "type": "radio",

16
spec/components/check_answers_summary_list_card_component_spec.rb

@ -0,0 +1,16 @@
require "rails_helper"
RSpec.describe CheckAnswersSummaryListCardComponent, type: :component do
context "when given a set of questions" do
let(:user) { FactoryBot.build(:user) }
let(:case_log) { FactoryBot.build(:case_log, :completed) }
let(:subsection_id) { "household_characteristics" }
let(:subsection) { case_log.form.get_subsection(subsection_id) }
let(:questions) { subsection.applicable_questions(case_log) }
it "renders a summary list card for the answers to those questions" do
result = render_inline(described_class.new(questions:, case_log:, user:))
expect(result).to have_content(questions.first.answer_label(case_log))
end
end
end
Loading…
Cancel
Save