Skip to content

Commit a4b86af

Browse files
committed
Refactor AppTriageFormComponent
This refactors the component to simplify its use and make it work in a similar way to the `AppVaccinateFormComponent`.
1 parent 618e3cf commit a4b86af

File tree

5 files changed

+49
-76
lines changed

5 files changed

+49
-76
lines changed

app/components/app_patient_page_component.html.erb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
<%= render AppTriageFormComponent.new(
2626
patient_session:,
2727
programme:,
28-
url: session_patient_programme_triages_path(
29-
session,
30-
patient,
31-
programme,
32-
@triage
33-
),
3428
triage: @triage,
3529
legend: :bold,
3630
) %>
Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,31 @@
11
<%= form_with(
22
model: @triage,
3-
url: @url,
4-
method: @method,
3+
url:,
4+
method: :post,
55
builder: GOVUKDesignSystemFormBuilder::FormBuilder,
66
) do |f| %>
77
<% content_for(:before_content) { f.govuk_error_summary } %>
88

9-
<%= f.govuk_radio_buttons_fieldset(:status, **fieldset_options) do %>
10-
<%= f.govuk_radio_button(
11-
:status, :ready_to_vaccinate,
12-
label: { text: "Yes, it’s safe to vaccinate" },
13-
link_errors: true,
14-
) %>
9+
<%= f.govuk_radio_buttons_fieldset :status, **fieldset_options do %>
10+
<%= f.govuk_radio_button :status, :ready_to_vaccinate,
11+
label: { text: "Yes, it’s safe to vaccinate" },
12+
link_errors: true %>
13+
1514
<%= f.govuk_radio_divider %>
16-
<%= f.govuk_radio_button(
17-
:status, :do_not_vaccinate,
18-
label: { text: "No, do not vaccinate" },
19-
) %>
20-
<%= f.govuk_radio_button(
21-
:status, :delay_vaccination,
22-
label: { text: "No, delay vaccination (and invite to clinic)" },
23-
) %>
24-
<%= f.govuk_radio_button(
25-
:status, :needs_follow_up,
26-
label: { text: "No, keep in triage" },
27-
) %>
15+
16+
<%= f.govuk_radio_button :status, :do_not_vaccinate,
17+
label: { text: "No, do not vaccinate" } %>
18+
19+
<%= f.govuk_radio_button :status, :delay_vaccination,
20+
label: { text: "No, delay vaccination (and invite to clinic)" } %>
21+
22+
<%= f.govuk_radio_button :status, :needs_follow_up,
23+
label: { text: "No, keep in triage" } %>
2824
<% end %>
2925

30-
<%= f.govuk_text_area(
31-
:notes,
32-
label: { text: "Triage notes (optional)" },
33-
rows: 5,
34-
) %>
26+
<%= f.govuk_text_area :notes,
27+
label: { text: "Triage notes (optional)" },
28+
rows: 5 %>
3529

36-
<%= f.govuk_submit @method == :put ? "Continue" : "Save triage" %>
30+
<%= f.govuk_submit @triage.new_record? ? "Save triage" : "Continue" %>
3731
<% end %>
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
11
# frozen_string_literal: true
22

33
class AppTriageFormComponent < ViewComponent::Base
4-
def initialize(
5-
patient_session:,
6-
programme:,
7-
url:,
8-
method: :post,
9-
triage: nil,
10-
legend: nil
11-
)
4+
def initialize(patient_session:, programme:, triage: nil, legend: nil)
125
super
136

147
@patient_session = patient_session
158
@programme = programme
16-
@triage =
17-
triage ||
18-
Triage.new.tap do |t|
19-
if (
20-
latest_triage =
21-
patient_session.patient.triage_outcome.latest[programme]
22-
)
23-
t.status = latest_triage.status
24-
end
25-
end
26-
@url = url
27-
@method = method
9+
@triage = triage || default_triage
2810
@legend = legend
2911
end
3012

3113
private
3214

33-
attr_reader :programme
15+
attr_reader :patient_session, :programme, :triage, :legend
16+
17+
delegate :patient, :session, to: :patient_session
18+
19+
def url
20+
session_patient_programme_triages_path(session, patient, programme, triage)
21+
end
3422

3523
def fieldset_options
36-
text = "Is it safe to vaccinate #{@patient_session.patient.given_name}?"
24+
text = "Is it safe to vaccinate #{patient.given_name}?"
3725

38-
case @legend
26+
case legend
3927
when :bold
4028
{ legend: { text:, tag: :h2 } }
4129
when :hidden
@@ -44,4 +32,12 @@ def fieldset_options
4432
{ legend: { text:, size: "s", class: "app-fieldset__legend--reset" } }
4533
end
4634
end
35+
36+
def default_triage
37+
if (latest_triage = patient.triage_outcome.latest[programme])
38+
Triage.new(status: latest_triage.status)
39+
else
40+
Triage.new
41+
end
42+
end
4743
end

app/views/triages/new.html.erb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,5 @@
1010
<%= render AppTriageFormComponent.new(
1111
patient_session: @patient_session,
1212
programme: @programme,
13-
url: session_patient_programme_triages_path(
14-
@session,
15-
@patient,
16-
@programme,
17-
@triage,
18-
),
1913
triage: @triage,
2014
) %>
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# frozen_string_literal: true
22

33
describe AppTriageFormComponent do
4-
subject(:rendered) { render_inline(component) }
4+
subject { render_inline(component) }
55

6-
let(:component) do
7-
described_class.new(patient_session:, programme:, url: "#")
8-
end
6+
let(:component) { described_class.new(patient_session:, programme:) }
97

108
let(:programme) { create(:programme) }
119
let(:patient_session) { create(:patient_session, programmes: [programme]) }
@@ -14,8 +12,8 @@
1412
it { should have_text("Is it safe to vaccinate") }
1513
it { should have_css(".app-fieldset__legend--reset") }
1614

17-
describe "triage instance variable" do
18-
subject(:triage) { component.instance_variable_get(:@triage) }
15+
describe "triage private method" do
16+
subject { component.send(:triage) }
1917

2018
context "patient_session has no existing triage" do
2119
it { should be_a(Triage) }
@@ -31,7 +29,7 @@
3129

3230
describe "with a bold legend" do
3331
let(:component) do
34-
described_class.new(patient_session:, programme:, url: "#", legend: :bold)
32+
described_class.new(patient_session:, programme:, legend: :bold)
3533
end
3634

3735
it { should have_css("h2") }
@@ -40,22 +38,19 @@
4038

4139
describe "with a hidden legend" do
4240
let(:component) do
43-
described_class.new(
44-
patient_session:,
45-
programme:,
46-
url: "#",
47-
legend: :hidden
48-
)
41+
described_class.new(patient_session:, programme:, legend: :hidden)
4942
end
5043

5144
it { should have_css("legend.nhsuk-visually-hidden") }
5245
end
5346

54-
describe "with the put method" do
47+
describe "with an existing triage" do
5548
let(:component) do
56-
described_class.new(patient_session:, programme:, url: "#", method: :put)
49+
described_class.new(patient_session:, programme:, triage:)
5750
end
5851

52+
let(:triage) { create(:triage) }
53+
5954
it { should have_text("Continue") }
6055
end
6156
end

0 commit comments

Comments
 (0)