Browse Source

Dynamic back button

pull/21/head
baarkerlounger 3 years ago
parent
commit
377e055e47
  1. 8
      app/javascript/styles/_task-list.scss
  2. 4
      app/models/form.rb
  3. 9
      app/views/form/questions/previous_housing_situation.html.erb
  4. 8
      app/views/form/questions/tenant_age.html.erb
  5. 8
      app/views/form/questions/tenant_code.html.erb
  6. 9
      app/views/form/questions/tenant_gender.html.erb
  7. 16
      spec/models/form_spec.rb

8
app/javascript/styles/_task-list.scss

@ -81,7 +81,7 @@
} }
} }
// turbo-frame { turbo-frame {
// display: block; display: block;
// border: 1px solid blue border: 1px solid blue
// } }

4
app/models/form.rb

@ -36,4 +36,8 @@ class Form < ApplicationRecord
def self.next_question(previous_question) def self.next_question(previous_question)
Form::QUESTIONS[previous_question] Form::QUESTIONS[previous_question]
end end
def self.previous_question(current_question)
Hash[QUESTIONS.to_a.map(&:reverse)][current_question]
end
end end

9
app/views/form/questions/previous_housing_situation.html.erb

@ -1,4 +1,4 @@
<% situations = [ <% situations = [
OpenStruct.new(id: 0, name: "Owner occupation (private)"), OpenStruct.new(id: 0, name: "Owner occupation (private)"),
OpenStruct.new(id: 1, name: "Owner occupation (low cost home ownership)"), OpenStruct.new(id: 1, name: "Owner occupation (low cost home ownership)"),
OpenStruct.new(id: 2, name: "Private sector tenancy"), OpenStruct.new(id: 2, name: "Private sector tenancy"),
@ -22,7 +22,12 @@
OpenStruct.new(id: 20, name: "Lifetime 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: 21, name: "Fixed term PRP General Needs tenancy"),
OpenStruct.new(id: 22, name: "Lifetime 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 %> <%= turbo_frame_tag "case_log_form", target: "_top" do %>
<%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>

8
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 %> <%= 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| %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_number_field :tenant_age, <%= f.govuk_number_field :tenant_age,
hint: { text: "More detail" }, hint: { text: "More detail" },

8
app/views/form/questions/tenant_code.html.erb

@ -1,7 +1,9 @@
<%= turbo_frame_tag "case_log_form", target: "_top" do %> <% previous_question = Form.previous_question("tenant_code") %>
<%= govuk_back_link href: "/case_logs/#{case_log_id}" do %> <%= govuk_back_link href: "/case_logs/#{case_log_id}/#{previous_question}" do %>
Back Back
<% end %> <% end %>
<%= turbo_frame_tag "case_log_form", target: "_top" do %>
<%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_text_field :tenant_code, <%= f.govuk_text_field :tenant_code,
hint: { text: "More detail" }, hint: { text: "More detail" },

9
app/views/form/questions/tenant_gender.html.erb

@ -1,9 +1,14 @@
<% genders = [ <% genders = [
OpenStruct.new(id: 0, name: "Male"), OpenStruct.new(id: 0, name: "Male"),
OpenStruct.new(id: 1, name: "Female"), OpenStruct.new(id: 1, name: "Female"),
OpenStruct.new(id: 2, name: "Non-binary"), OpenStruct.new(id: 2, name: "Non-binary"),
OpenStruct.new(id: 3, name: "Prefer not to say") 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 %> <%= turbo_frame_tag "case_log_form", target: "_top" do %>
<%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %> <%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>

16
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") expect(Form.first_question_for_subsection(subsection)).to eq("tenant_code")
end end
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 end

Loading…
Cancel
Save