Skip to content

Commit

Permalink
Add AppSessionTableComponent
Browse files Browse the repository at this point in the history
This adds a component which we can use to render a list of sessions,
which we'll be using a number of places.
  • Loading branch information
thomasleese committed Sep 25, 2024
1 parent 6ac580d commit 683b2f2
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
77 changes: 77 additions & 0 deletions app/components/app_session_table_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<div class="nhsuk-table__panel-with-heading-tab">
<h3 class="nhsuk-table__heading-tab"><%= heading %></h3>

<%= govuk_table(html_attributes: { class: "nhsuk-table-responsive" }) do |table| %>
<% table.with_head do |head| %>
<% head.with_row do |row| %>
<% row.with_cell(text: "Location") %>
<% row.with_cell(text: "Dates") %>
<% row.with_cell(text: "Programmes") if show_programmes %>
<% row.with_cell(text: "Consent period") if show_consent_period %>
<% row.with_cell(text: "Children", numeric: true) %>
<% end %>
<% end %>
<% table.with_body do |body| %>
<% sessions.each do |session| %>
<% body.with_row do |row| %>
<% row.with_cell do %>
<span class="nhsuk-table-responsive__heading">Location</span>

<span>
<%= govuk_link_to session.location.name, session_path(session) %>
<% if (location = session.location).has_address? %>
<br />
<span class="nhsuk-u-secondary-text-color">
<%= helpers.format_address_single_line(location) %>
</span>
<% end %>
</span>
<% end %>
<% row.with_cell do %>
<span class="nhsuk-table-responsive__heading">Dates</span>

<ul class="nhsuk-list">
<% session.dates.each do |date| %>
<li><%= date.value.to_fs(:long) %></li>
<% end %>
</ul>
<% end %>
<% if show_programmes %>
<% row.with_cell do %>
<span class="nhsuk-table-responsive__heading">Programmes</span>

<ul class="nhsuk-list">
<% session.programmes.each do |programme| %>
<li><%= programme.name %></li>
<% end %>
</ul>
<% end %>
<% end %>
<% if show_consent_period %>
<% row.with_cell do %>
<span class="nhsuk-table-responsive__heading">Consent period</span>

<% if session.close_consent_at.nil? %>
n/a
<% elsif session.close_consent_at.past? %>
<%= "Closed #{session.close_consent_at.to_fs(:short)}" %>
<% else %>
<%= "Open until #{session.close_consent_at.to_fs(:short)}" %>
<% end %>
<% end %>
<% end %>
<% row.with_cell(numeric: true) do %>
<span class="nhsuk-table-responsive__heading">Children</span>
<%= (count = session.patients.count).zero? ? "None" : count %>
<% end %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
25 changes: 25 additions & 0 deletions app/components/app_session_table_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class AppSessionTableComponent < ViewComponent::Base
def initialize(
sessions,
description: "sessions",
show_programmes: false,
show_consent_period: false
)
super

@sessions = sessions
@description = description
@show_programmes = show_programmes
@show_consent_period = show_consent_period
end

private

attr_reader :sessions, :show_programmes, :show_consent_period

def heading
pluralize(sessions.count, @description)
end
end
65 changes: 65 additions & 0 deletions spec/components/app_session_table_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

describe AppSessionTableComponent do
subject(:rendered) { render_inline(component) }

let(:component) { described_class.new(sessions) }

let(:sessions) do
[
create(
:session,
academic_year: 2024,
date: Date.new(2024, 10, 1),
location: create(:location, :school, name: "Waterloo Road"),
programme: create(:programme, :hpv)
)
] + create_list(:session, 9)
end

before { create_list(:patient, 5, session: sessions.first) }

it { should have_css(".nhsuk-table__heading-tab", text: "10 sessions") }

context "with a custom description" do
let(:component) do
described_class.new(sessions, description: "active sessions")
end

it { should have_content("10 active sessions") }
end

it "renders the headers" do
expect(rendered).to have_css(".nhsuk-table__header", text: "Location")
expect(rendered).to have_css(".nhsuk-table__header", text: "Dates")
expect(rendered).to have_css(".nhsuk-table__header", text: "Children")
end

it "renders the rows" do
expect(rendered).to have_css(
".nhsuk-table__body .nhsuk-table__row",
count: 10
)

expect(rendered).to have_css(".nhsuk-table__cell", text: "Waterloo Road")

expect(rendered).to have_css(".nhsuk-table__cell", text: "1 October 2024")

expect(rendered).to have_css(".nhsuk-table__cell", text: "5")
expect(rendered).to have_css(".nhsuk-table__cell", text: "None")
end

context "when showing programmes" do
let(:component) { described_class.new(sessions, show_programmes: true) }

it { should have_css(".nhsuk-table__header", text: "Programmes") }
it { should have_css(".nhsuk-table__cell", text: "HPV") }
end

context "when showing consent period" do
let(:component) { described_class.new(sessions, show_consent_period: true) }

it { should have_css(".nhsuk-table__header", text: "Consent period") }
it { should have_css(".nhsuk-table__cell", text: "Open until 1 October") }
end
end

0 comments on commit 683b2f2

Please sign in to comment.