Browse Source

CLDC-3465 Allow clearing all error questions (#2483)

* Allow clearing all error questions

* Update setup question link

* Update clear all to ignore setup questions

* Update correct_validation_action_href

* Extract some vriables
pull/2484/head
kosiakkatrina 9 months ago committed by GitHub
parent
commit
465bf39b0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      app/components/check_answers_summary_list_card_component.html.erb
  2. 4
      app/components/check_answers_summary_list_card_component.rb
  3. 14
      app/controllers/check_errors_controller.rb
  4. 17
      app/controllers/form_controller.rb
  5. 11
      app/helpers/check_errors_helper.rb
  6. 32
      app/views/check_errors/confirm_clear_all_answers.html.erb
  7. 2
      app/views/check_errors/confirm_clear_answer.html.erb
  8. 8
      app/views/form/check_errors.html.erb
  9. 137
      spec/requests/check_errors_controller_spec.rb

2
app/components/check_answers_summary_list_card_component.html.erb

@ -36,7 +36,7 @@
<% if @log.collection_period_open_for_editing? %>
<% row.with_action(
text: question.action_text(log, correcting_hard_validation: @correcting_hard_validation),
href: @correcting_hard_validation ? correct_validation_action_href(question, log, applicable_questions.map(&:id)) : action_href(question, log),
href: correct_validation_action_href(question, log, applicable_questions.map(&:id), @correcting_hard_validation),
visually_hidden_text: question.check_answer_label.to_s.downcase,
) %>
<% end %>

4
app/components/check_answers_summary_list_card_component.rb

@ -34,7 +34,9 @@ class CheckAnswersSummaryListCardComponent < ViewComponent::Base
send("#{log.model_name.param_key}_#{question.page.id}_path", log, referrer:)
end
def correct_validation_action_href(question, log, _related_question_ids)
def correct_validation_action_href(question, log, _related_question_ids, correcting_hard_validation)
return action_href(question, log) unless correcting_hard_validation
if question.displayed_as_answered?(log)
send("#{log.model_name.param_key}_confirm_clear_answer_path", log, question_id: question.id)
else

14
app/controllers/check_errors_controller.rb

@ -8,9 +8,21 @@ class CheckErrorsController < ApplicationController
return render_not_found unless @log
@related_question_ids = params[@log.model_name.param_key].keys.reject { |id| id == "page_id" }
@page = @log.form.get_page(params[@log.model_name.param_key]["page_id"])
if params["clear_all"]
@questions_to_clear = @related_question_ids.map { |id|
question = @log.form.get_question(id, @log)
next if question.subsection.id == "setup"
question.page.questions.map(&:id)
}.flatten.compact
render :confirm_clear_all_answers
else
question_id = @related_question_ids.find { |id| !params[id].nil? }
@question = @log.form.get_question(question_id, @log)
@page = @log.form.get_page(params[@log.model_name.param_key]["page_id"])
end
end
private

17
app/controllers/form_controller.rb

@ -230,7 +230,9 @@ private
if params[@log.model_name.param_key]["check_errors"]
@page = form.get_page(params[@log.model_name.param_key]["page"])
flash[:notice] = "You have successfully updated #{@page.questions.map(&:check_answer_label).to_sentence}"
return send("#{@log.class.name.underscore}_#{params[@log.model_name.param_key]['original_page_id']}_path", @log, { check_errors: true, related_question_ids: params[@log.model_name.param_key]["related_question_ids"].split(" ") }.compact)
original_page_id = params[@log.model_name.param_key]["original_page_id"]
related_question_ids = params[@log.model_name.param_key]["related_question_ids"].split(" ")
return send("#{@log.class.name.underscore}_#{original_page_id}_path", @log, { check_errors: true, related_question_ids: }.compact)
end
if params["referrer"] == "check_errors"
@ -402,11 +404,16 @@ private
end
def render_check_errors_page
if params[@log.model_name.param_key]["clear_question_id"]
question_id = params[@log.model_name.param_key]["clear_question_id"]
@log.form.get_question(question_id, @log).page.questions.map(&:id).each { |id| @log[id] = nil }
if params[@log.model_name.param_key]["clear_question_ids"].present?
question_ids = params[@log.model_name.param_key]["clear_question_ids"].split(" ")
question_ids.each do |question_id|
question = @log.form.get_question(question_id, @log)
next if question.subsection.id == "setup"
question.page.questions.map(&:id).each { |id| @log[id] = nil }
end
@log.save!
@questions = params[@log.model_name.param_key].keys.reject { |id| %w[clear_question_id page].include?(id) }.map { |id| @log.form.get_question(id, @log) }
@questions = params[@log.model_name.param_key].keys.reject { |id| %w[clear_question_ids page].include?(id) }.map { |id| @log.form.get_question(id, @log) }
else
responses_for_page = responses_for_page(@page)
@log.assign_attributes(responses_for_page)

11
app/helpers/check_errors_helper.rb

@ -0,0 +1,11 @@
module CheckErrorsHelper
include GovukLinkHelper
def check_errors_answer_text(question, log)
question.displayed_as_answered?(log) ? "Change" : "Answer"
end
def check_errors_answer_link(log, question, page, applicable_questions)
send("#{log.model_name.param_key}_#{question.page.id}_path", log, referrer: "check_errors", original_page_id: page.id, related_question_ids: applicable_questions.map(&:id))
end
end

32
app/views/check_errors/confirm_clear_all_answers.html.erb

@ -0,0 +1,32 @@
<% content_for :before_content do %>
<% content_for :title, "Are you sure you want to clear all?" %>
<% end %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<h1 class="govuk-heading-xl">
<%= content_for(:title) %>
</h1>
<p class="govuk-body">You've selected <%= @questions_to_clear.count %> answers to clear</p>
<%= govuk_warning_text(text: "You will not be able to undo this action") %>
<%= form_with model: @log, url: send("#{@log.model_name.param_key}_#{@page.id}_path", @log), method: "post", local: true do |f| %>
<% @related_question_ids.each do |id| %>
<%= f.hidden_field id, value: @log[id] %>
<% end %>
<%= f.hidden_field :clear_question_ids, value: @questions_to_clear %>
<%= f.hidden_field :page, value: @page.id %>
<div class="govuk-button-group">
<%= f.govuk_submit "Confirm and continue", name: "check_errors" %>
<%= govuk_button_link_to(
"Cancel",
"javascript:history.back()",
secondary: true,
) %>
</div>
<% end %>
</div>
</div>

2
app/views/check_errors/confirm_clear_answer.html.erb

@ -15,7 +15,7 @@
<%= f.hidden_field id, value: @log[id] %>
<% end %>
<%= f.hidden_field :clear_question_id, value: @question.id %>
<%= f.hidden_field :clear_question_ids, value: [@question.id] %>
<%= f.hidden_field :page, value: @page.id %>
<div class="govuk-button-group">

8
app/views/form/check_errors.html.erb

@ -11,7 +11,7 @@
Make sure these answers are correct:
</span>
<span class="govuk-body govuk-!-text-align-right govuk-grid-column-one-third">
<%= govuk_link_to "Clear all", send("#{@log.model_name.param_key}_confirm_clear_all_answers_path", @log) %>
<input type="submit" value="Clear all" class="govuk-body govuk-link submit-button-link" name="clear_all">
</span>
</div>
</h1>
@ -48,10 +48,10 @@
<% end %>
</dd>
<dd class="govuk-summary-list__actions">
<% if question.displayed_as_answered?(@log) %>
<input type="submit" value="Clear" class="govuk-body govuk-link submit-button-link" name="<%= question.id %>">
<% if !question.displayed_as_answered?(@log) || question.subsection.id == "setup" %>
<%= govuk_link_to check_errors_answer_text(question, @log), check_errors_answer_link(@log, question, @page, applicable_questions) %>
<% else %>
<%= govuk_link_to "Answer", send("#{@log.model_name.param_key}_#{question.page.id}_path", @log, referrer: "check_errors", original_page_id: @page.id, related_question_ids: applicable_questions.map(&:id)) %>
<input type="submit" value="Clear" class="govuk-body govuk-link submit-button-link" name="<%= question.id %>">
<% end %>
</dd>
</div>

137
spec/requests/check_errors_controller_spec.rb

@ -59,7 +59,7 @@ RSpec.describe CheckErrorsController, type: :request do
it "displays correct clear links" do
expect(page).to have_selector("input[type=submit][value='Clear']", count: 3)
expect(page).to have_link("Clear all", href: "/lettings-logs/#{lettings_log.id}/confirm-clear-all-answers")
expect(page).to have_button("Clear all")
end
end
@ -83,9 +83,10 @@ RSpec.describe CheckErrorsController, type: :request do
post "/sales-logs/#{sales_log.id}/buyer-1-income", params: params
end
it "displays correct clear links" do
expect(page).to have_button("Clear", count: 3)
expect(page).to have_link("Clear all", href: "/sales-logs/#{sales_log.id}/confirm-clear-all-answers")
it "displays correct clear and change links" do
expect(page.all(:button, value: "Clear").count).to eq(2)
expect(page).to have_link("Change", count: 1)
expect(page).to have_button("Clear all")
end
end
end
@ -179,6 +180,66 @@ RSpec.describe CheckErrorsController, type: :request do
end
end
describe "confirm clear all answers" do
context "when user is signed in" do
context "and clearing all lettings questions" do
let(:params) do
{
id: lettings_log.id,
clear_all: "Clear all",
lettings_log: {
earnings: "100000",
incfreq: "1",
hhmemb: "2",
page_id: "income_amount",
},
}
end
before do
sign_in user
post "/lettings-logs/#{lettings_log.id}/confirm-clear-answer", params:
end
it "displays correct clear links" do
expect(page).to have_content("Are you sure you want to clear all")
expect(page).to have_content("You've selected 5 answers to clear")
expect(page).to have_content("You will not be able to undo this action")
expect(page).to have_link("Cancel")
expect(page).to have_button("Confirm and continue")
end
end
context "and clearing all sales question" do
let(:params) do
{
id: sales_log.id,
clear_all: "Clear all",
sales_log: {
income1: "100000",
la: "E09000001",
ownershipsch: "1",
page_id: "buyer_1_income",
},
}
end
before do
sign_in user
post "/sales-logs/#{sales_log.id}/confirm-clear-answer", params:
end
it "displays correct clear links" do
expect(page).to have_content("Are you sure you want to clear all")
expect(page).to have_content("You've selected 3 answers to clear")
expect(page).to have_content("You will not be able to undo this action")
expect(page).to have_link("Cancel")
expect(page).to have_button("Confirm and continue")
end
end
end
end
describe "clear answer" do
context "when user is not signed in" do
it "redirects to sign in page for lettings" do
@ -219,7 +280,7 @@ RSpec.describe CheckErrorsController, type: :request do
earnings: "100000",
incfreq: "1",
hhmemb: "2",
clear_question_id: "hhmemb",
clear_question_ids: "hhmemb",
page: "income_amount",
},
check_errors: "",
@ -247,7 +308,7 @@ RSpec.describe CheckErrorsController, type: :request do
income1: "100000",
la: "E09000001",
ownershipsch: "1",
clear_question_id: "income1",
clear_question_ids: "income1",
page: "buyer_1_income",
},
check_errors: "",
@ -327,4 +388,68 @@ RSpec.describe CheckErrorsController, type: :request do
end
end
end
describe "clear all answers" do
context "when user is signed in" do
context "and clearing all lettings question" do
let(:params) do
{
id: lettings_log.id,
lettings_log: {
earnings: "100000",
incfreq: "1",
hhmemb: "2",
clear_question_ids: "earnings incfreq hhmemb",
page: "income_amount",
},
check_errors: "",
}
end
before do
sign_in user
post "/lettings-logs/#{lettings_log.id}/income-amount", params:
end
it "correctly clears the values" do
expect(page).to have_content("Make sure these answers are correct")
expect(page).to have_content("You didn’t answer this question")
expect(page.all(:button, value: "Clear").count).to eq(0)
expect(lettings_log.reload.earnings).to eq(nil)
expect(lettings_log.reload.incfreq).to eq(nil)
expect(lettings_log.reload.hhmemb).to eq(nil)
end
end
context "and clearing all sales question" do
let(:params) do
{
id: sales_log.id,
sales_log: {
income1: "100000",
la: "E09000001",
ownershipsch: "1",
clear_question_ids: "income1 la ownershipsch",
page: "buyer_1_income",
},
check_errors: "",
}
end
before do
sign_in user
post "/sales-logs/#{sales_log.id}/buyer-1-income", params:
end
it "displays correct clear links" do
expect(page).to have_content("Make sure these answers are correct")
expect(page).to have_content("You didn’t answer this question")
expect(page.all(:button, value: "Clear").count).to eq(0)
expect(sales_log.reload.income1).to eq(nil)
expect(sales_log.reload.la).to eq(nil)
expect(sales_log.reload.ownershipsch).not_to eq(nil)
end
end
end
end
end

Loading…
Cancel
Save