Skip to content

Commit

Permalink
Refactor status tag component
Browse files Browse the repository at this point in the history
This refactors the component to accept multiple statuses and render the
tags next to each other.
  • Loading branch information
thomasleese committed Sep 12, 2023
1 parent af22f6e commit e01a873
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 45 deletions.
4 changes: 3 additions & 1 deletion app/components/status_tag/component.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<%= govuk_tag(text:, colour:, html_attributes: { id: }, classes:) %>
<% tags.each do |tag| %>
<%= govuk_tag(**tag) %>
<% end %>
25 changes: 12 additions & 13 deletions app/components/status_tag/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

module StatusTag
class Component < ViewComponent::Base
def initialize(status:, id: nil, class_context: nil)
def initialize(statuses, id: nil, class_context: nil)
super
@status = status.to_sym
@statuses = statuses
@id = id
@class_context = class_context
end

attr_reader :id
attr_reader :statuses, :id, :class_context

def text
I18n.t(@status, scope: %i[components status_tag])
end

def classes
@class_context ? ["#{@class_context}__tag"] : []
def tags
Array(statuses).map do |status|
{
text: I18n.t(status, scope: %i[components status_tag]),
colour: COLOURS[status.to_sym],
classes: class_context ? ["#{class_context}__tag"] : [],
html_attributes: id ? { id: } : {},
}
end
end

COLOURS = {
Expand Down Expand Up @@ -54,9 +57,5 @@ def classes
waiting_on_reference: "yellow",
withdrawn: "red",
}.freeze

def colour
COLOURS[@status]
end
end
end
2 changes: 1 addition & 1 deletion app/components/task_list/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<% end %>
</span>

<%= render(StatusTag::Component.new(id: status_id, status: item[:status], class_context: "app-task-list")) %>
<%= render(StatusTag::Component.new(item[:status], id: status_id, class_context: "app-task-list")) %>
</li>
<% end %>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion app/components/timeline_entry/component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<% elsif timeline_event.assessment_section_recorded? %>
<p class="govuk-body">
<%= description_vars[:section_name] %>:
<%= render StatusTag::Component.new(status: description_vars[:passed] ? "accepted" : "rejected") %>
<%= render StatusTag::Component.new(description_vars[:passed] ? "accepted" : "rejected") %>
</p>

<% if (visible_failure_reasons = description_vars[:visible_failure_reasons]).present? %>
Expand Down
4 changes: 2 additions & 2 deletions app/components/timeline_entry/component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ def state_changed_vars
old_state:
render(
StatusTag::Component.new(
status: timeline_event.old_state,
timeline_event.old_state,
class_context: "timeline-event",
),
).strip,
new_state:
render(
StatusTag::Component.new(
status: timeline_event.new_state,
timeline_event.new_state,
class_context: "timeline-event",
),
).strip,
Expand Down
35 changes: 11 additions & 24 deletions app/helpers/application_form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def application_form_summary_rows(
text: I18n.t("application_form.summary.status"),
},
value: {
text: application_form_status_tags(application_form, class_context:),
text: application_form_status_tag(application_form, class_context:),
},
},
].compact
Expand All @@ -164,29 +164,16 @@ def application_form_display_work_history_before_teaching_qualification_banner?(

private

def application_form_status_tags(application_form, class_context:)
if %w[overdue received waiting_on].include?(application_form.status)
components =
%w[
further_information
professional_standing
qualification
reference
].filter_map do |requestable|
status = "#{application_form.status}_#{requestable}"
if application_form.send(status)
StatusTag::Component.new(status:, class_context:)
end
end
def application_form_status_tag(application_form, class_context:)
statuses =
if %w[overdue received waiting_on].include?(application_form.status)
%w[further_information professional_standing qualification reference]
.map { |requestable| "#{application_form.status}_#{requestable}" }
.filter { |status| application_form.send(status) }
else
application_form.status
end

components.map { |component| render(component) }.join(" ").html_safe
else
render(
StatusTag::Component.new(
status: application_form.status,
class_context:,
),
)
end
render(StatusTag::Component.new(statuses, class_context:))
end
end
4 changes: 2 additions & 2 deletions app/views/personas/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
row.with_cell { persona_check_tag(persona[:status_check]) }
row.with_cell { persona_check_tag(persona[:sanction_check]) }
row.with_cell do
render(StatusTag::Component.new(status: persona[:status]))
render(StatusTag::Component.new(persona[:status]))
end
row.with_cell { persona[:teacher].email }

Expand Down Expand Up @@ -169,7 +169,7 @@
row.with_cell(text: reference_request.application_form.teacher.email)

row.with_cell(width: "one-third") do
render(StatusTag::Component.new(status: reference_request.state))
render(StatusTag::Component.new(reference_request.state))
end

row.with_cell(numeric: true) do
Expand Down
2 changes: 1 addition & 1 deletion spec/components/status_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

RSpec.describe StatusTag::Component, type: :component do
subject(:component) do
render_inline(described_class.new(id:, status:, class_context:))
render_inline(described_class.new(status, id:, class_context:))
end

let(:id) { "id" }
Expand Down

0 comments on commit e01a873

Please sign in to comment.