From 377e055e4754a645b08dc0d394af6db5fd5d18e6 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 17 Sep 2021 15:59:21 +0100 Subject: [PATCH] Dynamic back button --- app/javascript/styles/_task-list.scss | 8 +-- app/models/form.rb | 4 ++ .../previous_housing_situation.html.erb | 55 ++++++++++--------- app/views/form/questions/tenant_age.html.erb | 8 ++- app/views/form/questions/tenant_code.html.erb | 8 ++- .../form/questions/tenant_gender.html.erb | 17 ++++-- spec/models/form_spec.rb | 16 ++++++ 7 files changed, 75 insertions(+), 41 deletions(-) diff --git a/app/javascript/styles/_task-list.scss b/app/javascript/styles/_task-list.scss index d0f929f86..d00cd8aeb 100644 --- a/app/javascript/styles/_task-list.scss +++ b/app/javascript/styles/_task-list.scss @@ -81,7 +81,7 @@ } } -// turbo-frame { -// display: block; -// border: 1px solid blue -// } +turbo-frame { + display: block; + border: 1px solid blue +} diff --git a/app/models/form.rb b/app/models/form.rb index 6f322cc33..aa029bab8 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -36,4 +36,8 @@ class Form < ApplicationRecord def self.next_question(previous_question) Form::QUESTIONS[previous_question] end + + def self.previous_question(current_question) + Hash[QUESTIONS.to_a.map(&:reverse)][current_question] + end end diff --git a/app/views/form/questions/previous_housing_situation.html.erb b/app/views/form/questions/previous_housing_situation.html.erb index b57886419..9b6e3713d 100644 --- a/app/views/form/questions/previous_housing_situation.html.erb +++ b/app/views/form/questions/previous_housing_situation.html.erb @@ -1,28 +1,33 @@ - <% situations = [ - OpenStruct.new(id: 0, name: "Owner occupation (private)"), - OpenStruct.new(id: 1, name: "Owner occupation (low cost home ownership)"), - OpenStruct.new(id: 2, name: "Private sector tenancy"), - OpenStruct.new(id: 3, name: "Tied housing or rented with job"), - OpenStruct.new(id: 4, name: "Supported housing"), - OpenStruct.new(id: 5, name: "Sheltered accomodation"), - OpenStruct.new(id: 6, name: "Residential care home"), - OpenStruct.new(id: 7, name: "Living with friends or family"), - OpenStruct.new(id: 8, name: "Refuge"), - OpenStruct.new(id: 9, name: "Hospital"), - OpenStruct.new(id: 10, name: "Prison / Approved Probation Hostel"), - OpenStruct.new(id: 11, name: "Direct access hostel"), - OpenStruct.new(id: 12, name: "Bed & Breakfast"), - OpenStruct.new(id: 13, name: "Mobile home / caravan"), - OpenStruct.new(id: 14, name: "Any other temporary accommodation"), - OpenStruct.new(id: 15, name: "Home Office Asylum Support"), - OpenStruct.new(id: 16, name: "Children’s home / foster care"), - OpenStruct.new(id: 17, name: "Rough sleeping"), - OpenStruct.new(id: 18, name: "Other"), - OpenStruct.new(id: 19, name: "Fixed term Local Authority General Needs tenancy"), - OpenStruct.new(id: 20, name: "Lifetime Local Authority General Needs tenancy"), - OpenStruct.new(id: 21, name: "Fixed term PRP General Needs tenancy"), - OpenStruct.new(id: 22, name: "Lifetime PRP General Needs tenancy"), - ] %> +<% situations = [ + OpenStruct.new(id: 0, name: "Owner occupation (private)"), + OpenStruct.new(id: 1, name: "Owner occupation (low cost home ownership)"), + OpenStruct.new(id: 2, name: "Private sector tenancy"), + OpenStruct.new(id: 3, name: "Tied housing or rented with job"), + OpenStruct.new(id: 4, name: "Supported housing"), + OpenStruct.new(id: 5, name: "Sheltered accomodation"), + OpenStruct.new(id: 6, name: "Residential care home"), + OpenStruct.new(id: 7, name: "Living with friends or family"), + OpenStruct.new(id: 8, name: "Refuge"), + OpenStruct.new(id: 9, name: "Hospital"), + OpenStruct.new(id: 10, name: "Prison / Approved Probation Hostel"), + OpenStruct.new(id: 11, name: "Direct access hostel"), + OpenStruct.new(id: 12, name: "Bed & Breakfast"), + OpenStruct.new(id: 13, name: "Mobile home / caravan"), + OpenStruct.new(id: 14, name: "Any other temporary accommodation"), + OpenStruct.new(id: 15, name: "Home Office Asylum Support"), + OpenStruct.new(id: 16, name: "Children’s home / foster care"), + OpenStruct.new(id: 17, name: "Rough sleeping"), + OpenStruct.new(id: 18, name: "Other"), + OpenStruct.new(id: 19, name: "Fixed term Local Authority General Needs tenancy"), + OpenStruct.new(id: 20, name: "Lifetime Local Authority General Needs tenancy"), + OpenStruct.new(id: 21, name: "Fixed term PRP General Needs tenancy"), + OpenStruct.new(id: 22, name: "Lifetime PRP General Needs tenancy"), +] %> + +<% previous_question = Form.previous_question("previous_housing_situation") %> +<%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + Back +<% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> diff --git a/app/views/form/questions/tenant_age.html.erb b/app/views/form/questions/tenant_age.html.erb index 4567f2081..12236c363 100644 --- a/app/views/form/questions/tenant_age.html.erb +++ b/app/views/form/questions/tenant_age.html.erb @@ -1,7 +1,9 @@ +<% previous_question = Form.previous_question("tenant_age") %> +<%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + Back +<% end %> + <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= govuk_back_link GovukComponent::BackLinkComponent.new href: 'tenant_code' do %> - back - <% end %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_number_field :tenant_age, hint: { text: "More detail" }, diff --git a/app/views/form/questions/tenant_code.html.erb b/app/views/form/questions/tenant_code.html.erb index ab1e36468..9cc6e9cdb 100644 --- a/app/views/form/questions/tenant_code.html.erb +++ b/app/views/form/questions/tenant_code.html.erb @@ -1,7 +1,9 @@ +<% previous_question = Form.previous_question("tenant_code") %> +<%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + Back +<% end %> + <%= turbo_frame_tag "case_log_form", target: "_top" do %> - <%= govuk_back_link href: "/case_logs/#{case_log_id}" do %> - Back - <% end %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= f.govuk_text_field :tenant_code, hint: { text: "More detail" }, diff --git a/app/views/form/questions/tenant_gender.html.erb b/app/views/form/questions/tenant_gender.html.erb index d5897d8f9..73fdb86f0 100644 --- a/app/views/form/questions/tenant_gender.html.erb +++ b/app/views/form/questions/tenant_gender.html.erb @@ -1,9 +1,14 @@ - <% genders = [ - OpenStruct.new(id: 0, name: "Male"), - OpenStruct.new(id: 1, name: "Female"), - OpenStruct.new(id: 2, name: "Non-binary"), - OpenStruct.new(id: 3, name: "Prefer not to say") - ] %> +<% genders = [ + OpenStruct.new(id: 0, name: "Male"), + OpenStruct.new(id: 1, name: "Female"), + OpenStruct.new(id: 2, name: "Non-binary"), + OpenStruct.new(id: 3, name: "Prefer not to say") +] %> + +<% previous_question = Form.previous_question("tenant_gender") %> +<%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %> + Back +<% end %> <%= turbo_frame_tag "case_log_form", target: "_top" do %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 91125ecb3..c19f3fdd4 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -14,4 +14,20 @@ RSpec.describe Form, type: :model do expect(Form.first_question_for_subsection(subsection)).to eq("tenant_code") end end + + describe ".previous_question" do + context "given a question in the middle of a subsection" do + let(:current_question) { "tenant_age" } + it "returns the previous question given the current" do + expect(Form.previous_question(current_question)).to eq("tenant_code") + end + end + + context "given the first question in a subsection" do + let(:current_question) { "tenant_code" } + it "returns empty string" do + expect(Form.previous_question(current_question)).to be_nil + end + end + end end