Skip to content

Commit

Permalink
Merge pull request #8242 from alphagov/refactor-preview-functionality…
Browse files Browse the repository at this point in the history
…-into-view-component

Move preview edition functionality into a View Component
  • Loading branch information
davidgisbey authored Sep 13, 2023
2 parents c1095d2 + 4aeaa60 commit c334ee2
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 82 deletions.
64 changes: 64 additions & 0 deletions app/components/admin/editions/show/preview_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<section class="app-view-summary__section">
<%= render "govuk_publishing_components/components/heading", {
text: "Preview",
heading_level: 2,
font_size: "l",
margin_bottom: 3,
} %>

<% if versioning_completed %>
<p class="govuk-body">
<%= preview_link(primary_locale_link_text, edition.public_url(draft: true), "Preview on website") %>
</p>

<% if available_in_multiple_languages %>
<%= render "govuk_publishing_components/components/details", {
title: "Preview translated pages"
} do %>
<% capture do %>
<%= render "govuk_publishing_components/components/list", {
items: edition.non_english_translated_locales.map do |locale|
preview_link(
"Preview on website - #{locale.native_and_english_language_name} (opens in new tab)",
edition.public_url(locale: locale.code, draft: true),
"Preview on website - #{locale.native_and_english_language_name}",
)
end
} %>
<% end %>
<% end %>
<% end %>

<% if edition.has_enabled_shareable_preview? %>
<%= render "govuk_publishing_components/components/details", {
title: "Share document preview"
} do %>
<% capture do %>
<p class="govuk-body">Send this preview link to someone so they can see the content and how the document will appear on GOV.UK.</p>
<p class="govuk-body">No password is needed and anyone with the preview link can view it. You're responsible for who you share draft documents with. </p>
<p class="govuk-body">The preview link will expire on <%= Date.today.next_month.strftime('%-d %B %Y') %> or when the document is published.</p>

<%= render "govuk_publishing_components/components/copy_to_clipboard", {
label: "Copy and send this link to someone and they’ll be able to preview your draft on GOV.UK.",
copyable_content: show_url_with_auth_bypass_options(edition, draft: true, locale: edition.primary_locale),
button_text: "Copy link"
} %>

<p class="govuk-body govuk-!-margin-top-7">Reset and generate a new preview link if you've shared the preview with the wrong person or if the link has expired. This will disable the previous preview link.</p>

<%= form_with(url: update_bypass_id_admin_edition_path(edition), method: :patch) do |f| %>
<input type="hidden" name="_method" value="patch" />
<%= render "govuk_publishing_components/components/button", {
text: "Generate new link",
secondary_quiet: true
} %>
<% end %>
<% end %>
<% end %>
<% end %>
<% else %>
<%= render "govuk_publishing_components/components/inset_text", {
text: "To see the changes and share a document preview link, add a change note or mark the change type to minor."
} %>
<% end %>
</section>
50 changes: 50 additions & 0 deletions app/components/admin/editions/show/preview_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

class Admin::Editions::Show::PreviewComponent < ViewComponent::Base
include Admin::UrlOptionsHelper

def initialize(edition:)
@edition = edition
end

def render?
!edition.publicly_visible?
end

private

attr_reader :edition

def versioning_completed
@versioning_completed ||= edition.versioning_completed?
end

def preview_link(link_text, href, tracking_label)
link_to(link_text,
href,
class: "govuk-link",
target: "_blank",
data: {
module: "gem-track-click",
"track-category": "button-clicked",
"track-action": track_action,
"track-label": tracking_label,
}, rel: "noopener")
end

def primary_locale_link_text
if available_in_multiple_languages
"Preview on website - English (opens in new tab)"
else
"Preview on website (opens in new tab)"
end
end

def available_in_multiple_languages
@available_in_multiple_languages ||= edition.translatable? && edition.available_in_multiple_languages?
end

def track_action
@track_action ||= "#{edition.model_name.singular.dasherize}-button"
end
end
80 changes: 1 addition & 79 deletions app/views/admin/editions/show/_main.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,85 +29,7 @@

<%= render partial: 'admin/editions/show/main_notices', locals: { edition: @edition } %>

<section class="app-view-summary__section">
<% if !@edition.publicly_visible? && @edition.versioning_completed? %>
<%= render "govuk_publishing_components/components/heading", {
text: "Preview",
heading_level: 2,
font_size: "l",
margin_bottom: 3,
} %>
<p class="govuk-body">
<%= link_to("Preview on website #{"- English" if @edition.translatable? && @edition.available_in_multiple_languages?} (opens in new tab)".strip,
@edition.public_url(draft: true, locale: @edition.primary_locale),
class: "govuk-link",
target: "_blank",
data: {
module: "gem-track-click",
"track-category": "button-clicked",
"track-action": "#{@edition.model_name.singular.dasherize}-button",
"track-label": "Preview on website",
}) if @edition.available_in_english? || @edition.non_english_edition? %>
</p>
<% if @edition.translatable? && @edition.available_in_multiple_languages? %>
<%= render "govuk_publishing_components/components/details", {
title: "Preview translated pages"
} do %>
<%= render "govuk_publishing_components/components/list", {
items: @edition.non_english_translated_locales.map do |locale|
(link_to("Preview on website - #{locale.native_and_english_language_name}",
@edition.public_url(locale: locale.code, draft: true),
class: 'govuk-link',
target: '_blank',
data: {
module: "gem-track-click",
"track-category": "button-clicked",
"track-action": "#{@edition.model_name.singular.dasherize}-button",
"track-label": "Preview on website - #{locale.native_and_english_language_name}",
}) + "")
end
} %>
<% end %>
<% end %>

<% if @edition.has_enabled_shareable_preview? %>
<%= render "govuk_publishing_components/components/details", {
title: "Share document preview"
} do %>
<p class="govuk-body">Send this preview link to someone so they can see the content and how the document will appear on GOV.UK.</p>
<p class="govuk-body">No password is needed and anyone with the preview link can view it. You're responsible for who you share draft documents with. </p>
<p class="govuk-body">The preview link will expire on <%= Date.today.next_month.strftime('%-d %B %Y') %> or when the document is published.</p>

<%= render "govuk_publishing_components/components/copy_to_clipboard", {
label: "Copy and send this link to someone and they’ll be able to preview your draft on GOV.UK.",
copyable_content: show_url_with_auth_bypass_options(@edition, draft: true, locale: @edition.primary_locale),
button_text: "Copy link"
} %>
<% if !@edition.publicly_visible? %>
<p class="govuk-body govuk-!-margin-top-7">Reset and generate a new preview link if you've shared the preview with the wrong person or if the link has expired. This will disable the previous preview link.</p>
<%= form_with(url: update_bypass_id_admin_edition_path(@edition), method: :patch) do |f| %>
<input type="hidden" name="_method" value="patch" />
<%= render "govuk_publishing_components/components/button", {
text: "Generate new link",
secondary_quiet: true
} %>
<% end %>
<% end %>
<% end %>
<% end %>
<% elsif !@edition.publicly_visible? && !@edition.versioning_completed? %>
<%= render "govuk_publishing_components/components/heading", {
text: "Preview",
heading_level: 2,
font_size: "l",
margin_bottom: 3,
} %>

<%= render "govuk_publishing_components/components/inset_text", {
text: "To see the changes and share a document preview link, add a change note or mark the change type to minor."
} %>
<% end %>
</section>
<%= render Admin::Editions::Show::PreviewComponent.new(edition: @edition) %>

<% if @edition.change_note_required? %>
<section class="app-view-summary__section">
Expand Down
81 changes: 81 additions & 0 deletions test/components/admin/editions/show/preview_component_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# frozen_string_literal: true

require "test_helper"

class Admin::Editions::Show::PreviewComponentTest < ViewComponent::TestCase
setup do
@document = build(:document)
end

test "does not render if edition is publically visible" do
edition = build(:published_edition)
render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))

assert page.text.blank?
end

test "renders a link with tracking to preview the document when the edition is english only" do
edition = build_stubbed(:publication, document: @document)

render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
preview_link = page.find("a[href='#{edition.public_url(draft: true)}']", text: "Preview on website (opens in new tab)")

assert_tracking_attributes(preview_link, "Preview on website")
end

test "renders a link with tracking to preview the document when the edition is a foreign language only edition" do
edition = build_stubbed(:publication, document: @document, primary_locale: :fr)

render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
preview_link = page.find("a[href='#{edition.public_url(draft: true)}']", text: "Preview on website (opens in new tab)")

assert_tracking_attributes(preview_link, "Preview on website")
end

test "renders a link with tracking to preview the document for each translation when there are multiple translations" do
edition = create(:publication, translated_into: %i[fr es], document: @document)

render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))
english_preview_link = page.find("a[href='#{edition.public_url(draft: true)}']", text: "Preview on website - English (opens in new tab)")
french_preview_link = page.find("a[href='#{edition.public_url(locale: 'fr', draft: true)}']", visible: false, text: "Preview on website - Français (French) (opens in new tab)")
spanish_preview_link = page.find("a[href='#{edition.public_url(locale: 'es', draft: true)}']", visible: false, text: "Preview on website - Español (Spanish) (opens in new tab)")

assert_tracking_attributes(english_preview_link, "Preview on website")
assert_tracking_attributes(french_preview_link, "Preview on website - Français (French)")
assert_tracking_attributes(spanish_preview_link, "Preview on website - Español (Spanish)")
end

test "renders sharable preview functionality when edition is a pre-publication state" do
edition = build_stubbed(:publication, document: @document)

render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))

assert_selector ".govuk-details__summary-text", text: "Share document preview"
end

test "does not render sharable preview functionality when edition is in a post-published state" do
edition = build(:published_publication, document: @document)

render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))

assert_selector ".govuk-details__summary-text", text: "Share document preview", count: 0
end

test "does not render preview or sharable preview functionality and informs the user when versioning needs to be completed" do
edition = build(:publication, document: @document)
edition.stubs(:versioning_completed?).returns(false)

render_inline(Admin::Editions::Show::PreviewComponent.new(edition:))

assert_selector "a[href='#{edition.public_url(draft: true)}']", text: "Preview on website (opens in new tab)", count: 0
assert_selector ".govuk-details__summary-text", text: "Share document preview", count: 0
assert_selector ".govuk-inset-text", text: "To see the changes and share a document preview link, add a change note or mark the change type to minor."
end

def assert_tracking_attributes(link, track_label)
assert_equal "gem-track-click", link["data-module"]
assert_equal "button-clicked", link["data-track-category"]
assert_equal "publication-button", link["data-track-action"]
assert_equal track_label, link["data-track-label"]
end
end
2 changes: 1 addition & 1 deletion test/functional/admin/consultations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Admin::ConsultationsControllerTest < ActionController::TestCase
stub_publishing_api_expanded_links_with_taxons(french_consultation.content_id, [])

get :show, params: { id: french_consultation }
assert_select ".app-view-summary__section a", text: "Preview on website (opens in new tab)", href: french_consultation.public_url(draft: true, locale: "fr")
assert_select ".app-view-summary__section a", text: "Preview on website (opens in new tab)", href: french_consultation.public_url(draft: true, locale: "fr")
end

view_test "edit displays consultation fields" do
Expand Down
4 changes: 2 additions & 2 deletions test/functional/admin/generic_editions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Admin::GenericEditionsControllerTest < ActionController::TestCase
stub_publishing_api_expanded_links_with_taxons(draft_edition.content_id, [])

get :show, params: { id: draft_edition }
assert_select ".govuk-link", text: "Preview on website (opens in new tab)", href: draft_edition.public_url(draft: true)
assert_select ".govuk-link", text: "Preview on website (opens in new tab)", href: draft_edition.public_url(draft: true)
end

view_test "GET :show doesn't render preview link if publically visible, change note is blank and edition is a major version" do
Expand All @@ -108,7 +108,7 @@ class Admin::GenericEditionsControllerTest < ActionController::TestCase

get :show, params: { id: draft_edition }

assert_select ".govuk-link", text: "Preview on website (opens in new tab)", href: draft_edition.public_url(draft: true), count: 0
assert_select ".govuk-link", text: "Preview on website (opens in new tab)", href: draft_edition.public_url(draft: true), count: 0
assert_select ".govuk-inset-text", text: "To see the changes and share a document preview link, add a change note or mark the change type to minor."
end
end

0 comments on commit c334ee2

Please sign in to comment.