Skip to content

Commit

Permalink
Add confirm discard page as an extra step when discarding a draft (un…
Browse files Browse the repository at this point in the history
…published) manual

Replace the existing system alert popup with a separate page to provide a better user experience.
  • Loading branch information
mtaylorgds committed Aug 23, 2023
1 parent 4c962e3 commit daa5f67
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 7 deletions.
27 changes: 25 additions & 2 deletions app/controllers/manuals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,29 @@ def publish
)
end

def confirm_discard
service = Manual::ShowService.new(
manual_id:,
user: current_user,
)
manual = service.call

if !manual.has_ever_been_published?
render(
:confirm_discard,
layout: "design_system",
locals: {
manual:,
},
)
else
redirect_to(
manual_path(manual_id),
flash: { error: "#{manual.title} cannot be discarded as it has already been published" },
)
end
end

def discard_draft
service = Manual::DiscardDraftService.new(
user: current_user,
Expand All @@ -171,12 +194,12 @@ def discard_draft
if result.successful?
redirect_to(
manuals_path,
flash: { notice: "Discarded draft of #{result.manual_title}" },
flash: { success: "Discarded draft of #{result.manual_title}" },
)
else
redirect_to(
manual_path(manual_id),
flash: { notice: "Unable to discard draft of #{result.manual_title}" },
flash: { error: "Unable to discard draft of #{result.manual_title}" },
)
end
end
Expand Down
39 changes: 39 additions & 0 deletions app/views/manuals/confirm_discard.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<% content_for :title, "Discard #{manual.title}" %>

<% content_for :title_margin_bottom, 6 %>

<%= 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: "Discard"
},
]
} %>

<%= render "govuk_publishing_components/components/title", {
title: "Discard #{manual.title}"
} %>

<p class="govuk-body">You are about to discard "<%= manual.title %>".</p>
<p class="govuk-body">Are you sure you want to discard this draft manual?</p>

<%= form_tag(discard_draft_manual_path(manual), method: :delete) do %>
<div class="govuk-button-group govuk-!-margin-bottom-6">
<%= render "govuk_publishing_components/components/button", {
text: "Discard manual",
name: "submit",
destructive: true
} %>

<%= link_to("Cancel", manual_path(manual), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
4 changes: 1 addition & 3 deletions app/views/manuals/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@
<div class="panel panel-default">
<div class="panel-heading"><h3>Discard draft manual</h3></div>
<div class="panel-body">
<%= form_tag(discard_draft_manual_path(manual), method: :delete) do %>
<button name="submit" class="btn btn-danger" data-module="confirm" data-message="Are you sure you want to discard this draft manual?">Discard draft manual</button>
<% end -%>
<%= link_to 'Discard draft', confirm_discard_manual_path(manual), class: 'btn btn-danger' %>
</div>
</div>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
delete :discard_draft, on: :member

get :confirm_publish, on: :member
get :confirm_discard, on: :member

get :original_publication_date, on: :member, action: :edit_original_publication_date
put :original_publication_date, on: :member, action: :update_original_publication_date
Expand Down
8 changes: 8 additions & 0 deletions features/deleting-a-manual.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ Feature: Rake task to delete a manual
Given a draft manual exists without any sections
And a draft section exists for the manual
When I discard the draft manual
And I confirm draft deletion
Then the manual and its sections are deleted

Scenario: Deleting a published manual
Given a published manual exists
When I run the deletion script
Then the script raises an error
And the manual and its sections still exist

Scenario: UI safeguarding against deleting a published manual
Given a published manual exists
And I am on the show page for that manual
Then I should not see the discard draft button
And I visit the confirm discard page directly
Then I am on the show page for that manual
17 changes: 17 additions & 0 deletions features/step_definitions/deleting_manuals_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@
When(/^I discard the draft manual$/) do
discard_draft_manual(@manual.title)
end

When(/^I confirm draft deletion$/) do
expect(page).to have_button("Discard manual")
click_on "Discard manual"
end

Then(/^I should not see the discard draft button$/) do
expect(page).not_to have_button("Discard manual")
end

When(/^I visit the confirm discard page directly$/) do
visit confirm_discard_manual_path(@manual)
end

Then(/^I am on the show page for that manual$/) do
expect(page).to have_content(@manual_fields[:summary])
end
4 changes: 2 additions & 2 deletions spec/controllers/manuals_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
let(:discard_success) { true }

it "sets a flash message indicating success" do
expect(flash[:notice]).to include("Discarded draft of My manual")
expect(flash[:success]).to include("Discarded draft of My manual")
end

it "redirects to the manuals index" do
Expand All @@ -61,7 +61,7 @@
let(:discard_success) { false }

it "sets a flash message indicating failure" do
expect(flash[:notice]).to include("Unable to discard draft of My manual")
expect(flash[:error]).to include("Unable to discard draft of My manual")
end

it "redirects to the show page for the manual" do
Expand Down

0 comments on commit daa5f67

Please sign in to comment.