Skip to content

Commit

Permalink
Migrate the reorder sections page to the design system
Browse files Browse the repository at this point in the history
Update the page for reordering sections within a manual so that it is
 using the `design_system` layout, and is built using UI components from
 the design system.

The `reorderable_list` component structures the form data differently to
 how the original page did, requiring changes on the server side to
 handle this difference when the data is submitted.
  • Loading branch information
mtaylorgds authored and acavalla committed Sep 6, 2023
1 parent 16e911c commit cd120b3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
//= require govuk_publishing_components/lib
//= require govuk_publishing_components/components/button
//= require govuk_publishing_components/components/layout-header
//= require govuk_publishing_components/components/reorderable-list
//= require govuk_publishing_components/components/skip-link
//= require govuk_publishing_components/components/table
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@import "govuk_publishing_components/components/button";
@import "govuk_publishing_components/components/layout-footer";
@import "govuk_publishing_components/components/layout-header";
@import "govuk_publishing_components/components/reorderable-list";
@import "govuk_publishing_components/components/skip-link";
@import "govuk_publishing_components/components/table";
@import "govuk_publishing_components/components/title";
Expand Down
11 changes: 10 additions & 1 deletion app/controllers/sections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def reorder

render(
:reorder,
layout: "design_system",
locals: {
manual: ManualViewAdapter.new(manual),
sections:,
Expand All @@ -140,7 +141,7 @@ def update_order
service = Section::ReorderService.new(
user: current_user,
manual_id: params.fetch(:manual_id),
section_order: params.fetch(:section_order),
section_order: update_section_order_params,
)
manual, _sections = service.call

Expand Down Expand Up @@ -198,6 +199,14 @@ def destroy

private

def update_section_order_params
params
.permit(section_order: {})[:section_order]
.to_h
.sort_by { |_key, value| value }
.map { |array| array[0] }
end

def section_params
params
.require(:section)
Expand Down
53 changes: 34 additions & 19 deletions app/views/sections/reorder.html.erb
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
<% content_for :page_title, "Reorder sections" %>

<% content_for :breadcrumbs do %>
<li><%= link_to "Your manuals", manuals_path %></li>
<li><%= link_to manual.title, manual_path(manual) %></li>
<li class="active">Reorder sections</li>
<%= render "govuk_publishing_components/components/breadcrumbs", {
collapse_on_mobile: true,
breadcrumbs: [
{
title: "Your manuals",
url: manuals_path
},
{
title: manual.title,
url: manual_path(manual)
},
{
title: "Reorder sections"
},
]
} %>
<% end %>

<h1 class="page-header">
<h1 class="govuk-heading-xl">
Reorder sections
</h1>

<%= form_tag(update_order_manual_sections_path(manual), method: :post) do %>
<div class="row">
<div class="col-md-8">
<ol class="reorderable-document-list">
<% sections.each do |section| %>
<li>
<input type="hidden" name="section_order[]" value="<%= section.uuid %>">
<%= section.title %>
</li>
<% end %>
</ol>
</div>
</div>
<%= render "govuk_publishing_components/components/reorderable_list", {
input_name: "section_order",
items: manual.sections.map do |section|
{
id: section.uuid,
title: section.title
}
end
} %>

<div class="govuk-button-group govuk-!-margin-bottom-6">
<%= render "govuk_publishing_components/components/button", {
text: "Save section order",
name: "submit",
} %>

<div class="actions">
<button name="submit" class="btn btn-primary">Save section order</button>
<%= link_to "Back", manual_path(manual), class: "action-link" %>
<%= link_to("Cancel", manual_path(manual), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion features/step_definitions/manual_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@
click_on("Reorder sections")
# Using capybara drag_to doesn't work reliably with our jQuery sortable
# therefore we have to take a manual approach to replicating the drag/drop
inputs = page.all(".reorderable-document-list li.ui-sortable-handle input", visible: false)
inputs = page.all(".gem-c-reorderable-list__item input", visible: false)
values = inputs.map(&:value).reverse
inputs.each_with_index { |input, index| input.execute_script("this.value = '#{values[index]}'") }

Expand Down

0 comments on commit cd120b3

Please sign in to comment.