Skip to content

Commit

Permalink
Add new table to render publications to publications view
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtrussler committed Aug 22, 2024
1 parent 13fa5a7 commit a936f7f
Show file tree
Hide file tree
Showing 13 changed files with 699 additions and 9 deletions.
48 changes: 48 additions & 0 deletions app/assets/javascripts/modules/publications-table.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
function PublicationsTable ($module) {
this.$module = $module
this.details = this.$module.querySelectorAll('.govuk-details')
this.openDetailsCount = 0
this.numDetails = this.details.length
}

PublicationsTable.prototype.init = function () {
// Add Expand/Contract link to DOM
this.addExpandlink()

// Add Event listener for Expand All button
this.$module.querySelector('.publications-table__heading a').addEventListener('click', this.toggleDetails.bind(this))
}

// Add Expand/Contract link to DOM
PublicationsTable.prototype.addExpandlink = function () {
var expandLink = document.createElement('a')
expandLink.classList.add('govuk-link', 'publications-table--expand-link')
expandLink.textContent = 'Expand all'
this.$module.querySelector('.publications-table__heading').append(expandLink)
}

// Toggles the "More details" sections
PublicationsTable.prototype.toggleDetails = function (e) {
if (this.openDetailsCount < this.numDetails) {
this.details.forEach(function (section) {
section.setAttribute('open', true)
})

this.openDetailsCount = this.numDetails
e.target.textContent = 'Collapse all'
} else if (this.openDetailsCount === this.numDetails) {
this.details.forEach(function (section) {
section.removeAttribute('open')
})

this.openDetailsCount = 0
e.target.textContent = 'Expand all'
}
}

Modules.PublicationsTable = PublicationsTable
})(window.GOVUK.Modules)
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ $govuk-page-width: 1140px;
@import "downtimes";
@import "summary-card";
@import "popular_links";
@import "publications-table";
82 changes: 82 additions & 0 deletions app/assets/stylesheets/publications-table.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
.publications-table {
position: relative;

&--expand-link {
float: right;
font-size: 1.1875rem;
}

.govuk-tag {
&--amends_needed {
@extend .govuk-tag--red; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--archived {
@extend .govuk-tag--blue; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--draft {
@extend .govuk-tag--yellow; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--fact_check {
@extend .govuk-tag--purple; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--fact_check_received {
@extend .govuk-tag--pink; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--ready {
@extend .govuk-tag--green; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--scheduled_for_publishing {
@extend .govuk-tag--turquoise; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--published {
@extend .govuk-tag--orange; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}

&--in_review {
@extend .govuk-tag--grey; // stylelint-disable-line scss/at-extend-no-missing-placeholder
}
}

.govuk-table {
border-top: 2px solid $govuk-text-colour;

.govuk-table__header {
white-space: nowrap;
}

.govuk-table__cell__updated {
white-space: nowrap;
}
}

.govuk-details {
margin-top: govuk-spacing(2);
}

.govuk-summary-list {
width: auto;
}

.govuk-details__summary {
&::before {
color: $govuk-text-colour;
}
}

.govuk-details__summary-text {
color: $govuk-text-colour;
}

.govuk-details__text {
dd {
word-break: break-word;
}
}
}
43 changes: 43 additions & 0 deletions app/helpers/publications_table_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module PublicationsTableHelper
def reviewer(publication, current_user)
if publication.state == "in_review"
if publication.reviewer.present?
publication.reviewer
elsif current_user != publication.assigned_to && current_user.has_editor_permissions?(publication)
render partial: "root/claim_2i", locals: { publication:, current_user: }
end
end
end

def edition_number(publication)
if (publication.state == "published" || publication.state == "archived") && publication.subsequent_siblings.first.present?
sanitize("#{publication.version_number} - #{link_to("##{publication.subsequent_siblings.first.version_number} in #{publication.subsequent_siblings.first.state.humanize.downcase}", edition_path(publication.subsequent_siblings.first), class: 'link-inherit')}")
else
publication.version_number.to_s
end
end

def format(publication)
publication.format.underscore.humanize
end

def important_note(publication)
publication.important_note.comment if publication.important_note.present?
end

def awaiting_review(publication)
time_ago_in_words(publication.review_requested_at) if publication.state == "in_review"
end

def sent_out(publication)
publication.last_fact_checked_at.to_date.to_fs(:govuk_date_short) if publication.state == "fact_check"
end

def scheduled(publication)
publication.publish_at.to_fs(:govuk_date_short) if publication.publish_at.present?
end

def published_by(publication)
publication.publisher.presence
end
end
8 changes: 7 additions & 1 deletion app/presenters/filtered_editions_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class FilteredEditionsPresenter
ITEMS_PER_PAGE = 20

def initialize(states_filter: [], assigned_to_filter: nil, format_filter: nil, title_filter: nil)
@states_filter = states_filter || []
@assigned_to_filter = assigned_to_filter
Expand All @@ -16,7 +18,11 @@ def editions
result = editions_by_format
result = apply_states_filter(result)
result = apply_assigned_to_filter(result)
apply_title_filter(result)
result = apply_title_filter(result)
result = result.where.not(_type: "PopularLinksEdition")
# Sets a temporary limit of one page and twenty items
# Pagination to follow
result.page(1).per(ITEMS_PER_PAGE)
end

private
Expand Down
7 changes: 7 additions & 0 deletions app/views/root/_claim_2i.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= form_for :edition, url: review_edition_path(publication._id), method: :put do |f| %>
<%= f.hidden_field :reviewer, value: current_user.name %>
<%= render "govuk_publishing_components/components/button", {
text: "Claim 2i",
} %>
<% end %>
55 changes: 55 additions & 0 deletions app/views/root/_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<div class="publications-table" data-module="publications-table">
<p class="govuk-heading-m govuk-!-margin-bottom-3 publications-table__heading"><%= @presenter.editions.length %> document(s)</p>

<%= render "govuk_publishing_components/components/table", {
head: [
{
text: "Title",
},
{
text: "Updated",
},
{
text: "Assigned to",
},
{
text: "Status",
},
],
rows: @presenter.editions.map do | publication |
[
{
text:
sanitize("<p class='title govuk-body'><a href='editions/#{publication._id}' class='govuk-link govuk-!-font-weight-bold'>#{publication.title}</a></p>") +
(render "govuk_publishing_components/components/details", {
title: "More details",
} do
render "govuk_publishing_components/components/summary_list", {
items: [
{field: "Edition", value: edition_number(publication)},
{field: "Format", value: format(publication)},
*([{field: "Important Note", value: important_note(publication)}] if important_note(publication)),
*([{field: "Awaiting review", value: awaiting_review(publication)}] if awaiting_review(publication)),
*([{field: "2i reviewer", value: reviewer(publication, current_user)}] if reviewer(publication, current_user)),
*([{field: "Sent Out", value: sent_out(publication)}] if sent_out(publication)),
*([{field: "Scheduled", value: scheduled(publication)}] if scheduled(publication)),
*([{field: "Published by", value: published_by(publication)}] if published_by(publication)),
],
borderless: true,
}
end
),
},
{
text: sanitize("<span class='govuk-table__cell__updated'>#{publication.updated_at.to_date.to_fs(:govuk_date_short)}</span>"),
},
{
text: publication.assignee,
},
{
text: sanitize("<span class='govuk-tag govuk-tag--#{publication.state}'>#{publication.state.humanize}</span>"),
},
]
end,
} %>
</div>
2 changes: 1 addition & 1 deletion app/views/root/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
<% end %>
</div>
<div class="govuk-grid-column-two-thirds">
<h2><%= @presenter.editions.count %> document(s)</h2>
<%= render :partial => "table" %>
</div>
Loading

0 comments on commit a936f7f

Please sign in to comment.