Browse Source

Intoduce Turbo-Frames!

pull/21/head
baarkerlounger 3 years ago
parent
commit
fe13d78191
  1. 24
      app/controllers/case_logs_controller.rb
  2. 24
      app/controllers/form_controller.rb
  3. 1
      app/javascript/packs/application.js
  4. 2
      app/views/case_logs/_tasklist.html.erb
  5. 6
      app/views/case_logs/edit.html.erb
  6. 5
      app/views/case_logs/household/tenant_age.html.erb
  7. 5
      app/views/case_logs/household/tenant_code.html.erb
  8. 5
      app/views/case_logs/household/tenant_gender.html.erb
  9. 16
      app/views/case_logs/show.html.erb
  10. 2
      config/routes.rb
  11. 2
      package.json
  12. 10
      yarn.lock

24
app/controllers/case_logs_controller.rb

@ -4,29 +4,19 @@ class CaseLogsController < ApplicationController
@in_progress_case_logs = CaseLog.where(status: 0)
end
# We don't have a dedicated non-editable show view
def show
@case_log = CaseLog.find(params[:id])
render :edit
end
def edit
@case_log = CaseLog.find(params[:id])
render "case_logs/household/tenant_code"
end
def update
@case_log = CaseLog.find(params[:id])
@case_log.update!(tenant_code: params[:case_log][:tenant_code]) if params[:case_log]
render_next_question(@case_log)
end
private
def render_next_question(_case_log)
previous = params[:case_log].keys.first
next_question = {
"tenant_code" => "case_logs/household/tenant_age",
"tenant_age" => "case_logs/household/tenant_gender",
}[previous]
render next_question
end
# def update
# @case_log = CaseLog.find(params[:id])
# @case_log.update!(tenant_code: params[:case_log][:tenant_code]) if params[:case_log]
# render_next_question(@case_log)
# end
end

24
app/controllers/form_controller.rb

@ -0,0 +1,24 @@
class FormController < ApplicationController
FIRST_QUESTION_FOR_SUBSECTION = {
"Household characteristics" => "case_logs/household/tenant_code",
}
NEXT_QUESTION = {
"tenant_code" => "case_logs/household/tenant_age",
"tenant_age" => "case_logs/household/tenant_gender",
"tenant_gender" => "case_logs/household/tenant_ethnic_group",
"tenant_ethnic_group" => "case_logs/household/tenant_nationality"
}
def next_question
subsection = params[:subsection]
result = if subsection
FIRST_QUESTION_FOR_SUBSECTION[subsection]
else
NEXT_QUESTION[params[:previous_question]]
end
render result
end
end

1
app/javascript/packs/application.js

@ -9,6 +9,7 @@ import Rails from "@rails/ujs"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
import { initAll } from 'govuk-frontend'
import "@hotwired/turbo-rails"
Rails.start()

2
app/views/case_logs/_tasklist.html.erb

@ -18,7 +18,7 @@
<ul class="app-task-list__items">
<% subsections.map do |subsection| %>
<li class="app-task-list__item">
<%= link_to subsection, edit_case_log_path, class: "task-name" %>
<%= link_to subsection, form_path(subsection: subsection), class: "task-name" %>
<strong class="govuk-tag govuk-tag--grey app-task-list__tag">Not started</strong>
</li>
<% end %>

6
app/views/case_logs/edit.html.erb

@ -1,4 +1,5 @@
<div class="govuk-grid-row">
<%= turbo_frame_tag "case_log_form" do %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">Tasklist for log
<%= @case_log.id %></h1>
@ -13,4 +14,5 @@
<%= render "tasklist" %>
</div>
</div>
</div>
<% end %>

5
app/views/case_logs/household/tenant_age.html.erb

@ -1,8 +1,11 @@
<%= form_with model: @case_log, method: "patch", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= turbo_frame_tag "case_log_form" do %>
<%= form_with action: '/form', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_number_field :tenant_age,
hint: { text: "More detail" },
label: { text: "What is the tenant's age?", size: "l"},
min: 0, max: 150, step: 1, width: 20
%>
<%= f.hidden_field :previous_question, value: :tenant_age %>
<%= f.govuk_submit "Save and continue" %>
<% end %>
<% end %>

5
app/views/case_logs/household/tenant_code.html.erb

@ -1,8 +1,11 @@
<%= form_with model: @case_log, method: "patch", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= turbo_frame_tag "case_log_form" do %>
<%= form_with action: '/form', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_text_field :tenant_code,
hint: { text: "More detail" },
label: { text: "What is the tenant code?", size: "l"},
width: 20
%>
<%= f.hidden_field :previous_question, value: :tenant_code %>
<%= f.govuk_submit "Save and continue" %>
<% end %>
<% end %>

5
app/views/case_logs/household/tenant_gender.html.erb

@ -5,7 +5,10 @@
OpenStruct.new(id: 3, name: "Prefer not to say")
] %>
<%= form_with model: @case_log, method: "patch", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= turbo_frame_tag "case_log_form" do %>
<%= form_with action: '/form', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_collection_radio_buttons :tenant_gender, genders, :id, :name, legend: { text: "Which of these best describes the tenant's gender identity?", size: "l" } %>
<%= f.hidden_field :previous_question, value: :tenant_gender %>
<%= f.govuk_submit "Save and continue" %>
<% end %>
<% end %>

16
app/views/case_logs/show.html.erb

@ -1,16 +0,0 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<h1 class="govuk-heading-xl">Tasklist for log
<%= @case_log.id %></h1>
<h2 class="govuk-heading-s govuk-!-margin-bottom-2">This submission is
<%= @case_log.status %></h2>
<p class="govuk-body govuk-!-margin-bottom-7">You've completed 0 of 9 sections.</p>
<p class="govuk-body govuk-!-margin-bottom-7">
<a href="#">Skip to next incomplete section</a>
</p>
<%= render "tasklist" %>
</div>
</div>

2
config/routes.rb

@ -2,6 +2,8 @@ Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
get "about", to: "about#index"
get "/", to: "test#index"
get "form", to: "form#next_question"
post "form", to: "form#next_question"
resources :case_logs
end

2
package.json

@ -2,7 +2,7 @@
"name": "data-collector",
"private": true,
"dependencies": {
"@hotwired/stimulus": "^3.0.0-beta.1",
"@hotwired/stimulus": "^3.0.0-beta.2",
"@hotwired/turbo": "^7.0.0-rc.3",
"@hotwired/turbo-rails": "^7.0.0-rc.3",
"@rails/actioncable": "^6.0.0",

10
yarn.lock

@ -885,14 +885,14 @@
resolved "https://registry.npmjs.org/@csstools/convert-colors/-/convert-colors-1.4.0.tgz"
integrity sha512-5a6wqoJV/xEdbRNKVo6I4hO3VjyDq//8q2f9I6PBAvMesJHFauXDorcNCsr9RzvsZnaWi5NYCcfyqP1QeFHFbw==
"@hotwired/stimulus@^3.0.0-beta.1":
version "3.0.0-beta.1"
resolved "https://registry.npmjs.org/@hotwired/stimulus/-/stimulus-3.0.0-beta.1.tgz"
integrity sha512-CrKSNYwwWJ1rCZiKgPADrLwRlmBQdvdbTgYlATjZP4MaU2SvGQ0wo1vsjjr7qzfEUvP/14GBnuQfMuhvHGrCNQ==
"@hotwired/stimulus@^3.0.0-beta.2":
version "3.0.0-beta.2"
resolved "https://registry.yarnpkg.com/@hotwired/stimulus/-/stimulus-3.0.0-beta.2.tgz#64ca67af9cc473ca97bb99d98fee5bc08e4c3914"
integrity sha512-rp2XojlugD0mvqi3qbqM660PbjqS7hnq/8FYa7SK/DL2rvWDu0/Ka3KW8cyXtcB1RiVG4nX5Oh4oq3nmuDjENQ==
"@hotwired/turbo-rails@^7.0.0-rc.3":
version "7.0.0-rc.3"
resolved "https://registry.npmjs.org/@hotwired/turbo-rails/-/turbo-rails-7.0.0-rc.3.tgz"
resolved "https://registry.yarnpkg.com/@hotwired/turbo-rails/-/turbo-rails-7.0.0-rc.3.tgz#ebb088a0c9f6eed23a99b396c2ae59d0c57644e0"
integrity sha512-J5NLTGSvlFW86pRxEm2WlYKdNeIkmQT9ZmIo1Rputj7BLymZYDCglaZ2vhMVFnQJ4qXEq4ttS3xeQt3cCG2Tdg==
dependencies:
"@hotwired/turbo" "^7.0.0-rc.3"

Loading…
Cancel
Save