-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8242 from alphagov/refactor-preview-functionality…
…-into-view-component Move preview edition functionality into a View Component
- Loading branch information
Showing
6 changed files
with
199 additions
and
82 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
app/components/admin/editions/show/preview_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
test/components/admin/editions/show/preview_component_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters