From f6d353f8e6911750a81cb3acef870430d19da4c8 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Tue, 18 Mar 2025 10:16:22 +0100 Subject: [PATCH] Add ability to edit parent details This adds a new page which allows the nurses to edit the parent details of a patient, including the name, relationship, phone number and email address. --- app/components/app_child_summary_component.rb | 12 ++ app/components/app_patient_card_component.rb | 2 +- .../parent_relationships_controller.rb | 21 ++++ app/controllers/patients_controller.rb | 1 + app/models/parent_relationship.rb | 2 + app/views/parent_relationships/edit.html.erb | 33 ++++++ app/views/patients/edit.html.erb | 10 +- config/locales/en.yml | 4 + config/routes.rb | 4 +- spec/features/edit_parent_spec.rb | 108 ++++++++++++++++++ 10 files changed, 189 insertions(+), 8 deletions(-) create mode 100644 app/views/parent_relationships/edit.html.erb create mode 100644 spec/features/edit_parent_spec.rb diff --git a/app/components/app_child_summary_component.rb b/app/components/app_child_summary_component.rb index 9431a52353..df23a86ee4 100644 --- a/app/components/app_child_summary_component.rb +++ b/app/components/app_child_summary_component.rb @@ -80,6 +80,18 @@ def call row.with_value do helpers.format_parent_with_relationship(parent_relationship) end + + if ( + href = + @change_links.dig(:parent, parent_relationship.parent_id) + ) + row.with_action( + text: "Change", + href:, + visually_hidden_text: parent_relationship.ordinal_label + ) + end + if ( href = @remove_links.dig(:parent, parent_relationship.parent_id) diff --git a/app/components/app_patient_card_component.rb b/app/components/app_patient_card_component.rb index e495f93188..3287e09f8e 100644 --- a/app/components/app_patient_card_component.rb +++ b/app/components/app_patient_card_component.rb @@ -3,7 +3,7 @@ class AppPatientCardComponent < ViewComponent::Base erb_template <<-ERB <%= render AppCardComponent.new do |card| %> - <% card.with_heading { "Child record" } %> + <% card.with_heading { "Child’s details" } %> <% if patient.date_of_death.present? %> <%= render AppStatusComponent.new( diff --git a/app/controllers/parent_relationships_controller.rb b/app/controllers/parent_relationships_controller.rb index 6a9895f20a..6398a1b00f 100644 --- a/app/controllers/parent_relationships_controller.rb +++ b/app/controllers/parent_relationships_controller.rb @@ -5,6 +5,17 @@ class ParentRelationshipsController < ApplicationController before_action :set_parent_relationship before_action :set_parent + def edit + end + + def update + if @parent_relationship.update(parent_relationship_params) + redirect_to edit_patient_path(@patient) + else + render :edit, status: :unprocessable_entity + end + end + def confirm_destroy = render :destroy def destroy @@ -33,4 +44,14 @@ def set_parent_relationship def set_parent @parent = @parent_relationship.parent end + + def parent_relationship_params + params.expect( + parent_relationship: [ + :type, + :other_name, + { parent_attributes: %i[id full_name email phone] } + ] + ) + end end diff --git a/app/controllers/patients_controller.rb b/app/controllers/patients_controller.rb index c2b2d451dd..afd1498aa7 100644 --- a/app/controllers/patients_controller.rb +++ b/app/controllers/patients_controller.rb @@ -28,6 +28,7 @@ def log end def edit + render layout: "full" end def update diff --git a/app/models/parent_relationship.rb b/app/models/parent_relationship.rb index 6faa4a841f..ea02f6a237 100644 --- a/app/models/parent_relationship.rb +++ b/app/models/parent_relationship.rb @@ -49,6 +49,8 @@ class ParentRelationship < ApplicationRecord before_validation -> { self.other_name = nil unless other? } + accepts_nested_attributes_for :parent, update_only: true + def label (other? ? other_name : human_enum_name(:type)).capitalize end diff --git a/app/views/parent_relationships/edit.html.erb b/app/views/parent_relationships/edit.html.erb new file mode 100644 index 0000000000..83b1e55c6c --- /dev/null +++ b/app/views/parent_relationships/edit.html.erb @@ -0,0 +1,33 @@ +<% content_for :before_main do %> + <%= render AppBacklinkComponent.new(edit_patient_path(@patient), name: "patient") %> +<% end %> + +<%= form_with model: @parent_relationship, url: patient_parent_relationship_path(@patient, @parent), method: :put do |f| %> + <%= f.govuk_error_summary %> + + <% page_title = "Details for #{@parent_relationship.ordinal_label}" %> + <%= h1 page_title: do %> + <%= @patient.full_name %> + <%= page_title %> + <% end %> + + <%= f.fields_for :parent do |parent_f| %> + <%= parent_f.govuk_text_field :full_name, label: { text: "Name" } %> + <% end %> + + <%= f.govuk_radio_buttons_fieldset :type, legend: { text: "Relationship to child", size: "s" } do %> + <%= f.govuk_radio_button :type, :mother, label: { text: "Mum" }, link_errors: true %> + <%= f.govuk_radio_button :type, :father, label: { text: "Dad" } %> + <%= f.govuk_radio_button :type, :guardian, label: { text: "Guardian" } %> + <%= f.govuk_radio_button :type, :other, label: { text: "Other" } do %> + <%= f.govuk_text_field :other_name, label: { text: "Relationship to the child" }, hint: { text: "For example, carer" } %> + <% end %> + <% end %> + + <%= f.fields_for :parent do |parent_f| %> + <%= parent_f.govuk_text_field :email, label: { text: "Email address" } %> + <%= parent_f.govuk_text_field :phone, label: { text: "Phone number" } %> + <% end %> + + <%= f.govuk_submit "Continue" %> +<% end %> diff --git a/app/views/patients/edit.html.erb b/app/views/patients/edit.html.erb index fe89054166..04f1c9f2ad 100644 --- a/app/views/patients/edit.html.erb +++ b/app/views/patients/edit.html.erb @@ -2,15 +2,13 @@ <%= render AppBacklinkComponent.new(patient_path(@patient), name: @patient.full_name) %> <% end %> -<% page_title = "Edit child record" %> - -<%= h1 page_title: do %> - <%= @patient.full_name %> - <%= page_title %> -<% end %> +<%= h1 "Edit child record" %> <% change_links = { nhs_number: edit_nhs_number_patient_path(@patient), + parent: @patient.parent_relationships.each_with_object({}) do |parent_relationship, memo| + memo[parent_relationship.parent_id] = edit_patient_parent_relationship_path(@patient, parent_relationship.parent_id) + end, } %> <% remove_links = { diff --git a/config/locales/en.yml b/config/locales/en.yml index 8a0cabce09..92d7d5f28e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -359,6 +359,10 @@ en: contact_method_other_details: blank: Enter details about how to contact you too_long: Enter details that are less than 300 characters long + email: + blank: Enter an email address + phone: + blank: Enter a phone number patient: attributes: nhs_number: diff --git a/config/routes.rb b/config/routes.rb index 1ec0ff6491..c350269c0d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -150,7 +150,9 @@ resources :patients, only: %i[index show edit update] do post "", action: :index, on: :collection - resources :parent_relationships, path: "parents", only: %i[destroy] do + resources :parent_relationships, + path: "parents", + only: %i[edit update destroy] do get "destroy", action: :confirm_destroy, on: :member, as: "destroy" end diff --git a/spec/features/edit_parent_spec.rb b/spec/features/edit_parent_spec.rb new file mode 100644 index 0000000000..087500e0e2 --- /dev/null +++ b/spec/features/edit_parent_spec.rb @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +describe "Edit parent" do + before { given_a_patient_with_a_parent_exists } + + scenario "User edits the name of a parent" do + when_i_visit_the_patient_page + and_i_click_on_edit_child_record + and_i_click_on_change_parent + then_i_see_the_edit_parent_page + + when_i_change_the_name_of_the_parent + then_i_see_the_new_name_of_the_parent + end + + scenario "User edits the relationship of a parent" do + when_i_visit_the_patient_page + and_i_click_on_edit_child_record + and_i_click_on_change_parent + then_i_see_the_edit_parent_page + + when_i_change_the_relationship_of_the_parent_to_mother + then_i_see_the_new_relationship_of_the_parent_of_mother + + when_i_click_on_change_parent + and_i_change_the_relationship_of_the_parent_to_other + then_i_see_the_new_relationship_of_the_parent_of_other + end + + scenario "User edits the contact details of a parent" do + when_i_visit_the_patient_page + and_i_click_on_edit_child_record + and_i_click_on_change_parent + then_i_see_the_edit_parent_page + + when_i_change_the_contact_details_of_the_parent + then_i_see_the_new_contact_details_of_the_parent + end + + def given_a_patient_with_a_parent_exists + organisation = create(:organisation) + @nurse = create(:nurse, organisation:) + + @patient = create(:patient, organisation:) + + @parent = create(:parent) + + create(:parent_relationship, patient: @patient, parent: @parent) + end + + def when_i_visit_the_patient_page + sign_in @nurse + visit patient_path(@patient) + end + + def and_i_click_on_edit_child_record + click_on "Edit child record" + end + + def when_i_click_on_change_parent + click_on "Change first parent or guardian" + end + + alias_method :and_i_click_on_change_parent, :when_i_click_on_change_parent + + def then_i_see_the_edit_parent_page + expect(page).to have_content("Details for first parent or guardian") + end + + def when_i_change_the_name_of_the_parent + fill_in "Name", with: "Selina Meyer" + click_on "Continue" + end + + def then_i_see_the_new_name_of_the_parent + expect(page).to have_content("Selina Meyer") + end + + def when_i_change_the_relationship_of_the_parent_to_mother + choose "Mum" + click_on "Continue" + end + + def then_i_see_the_new_relationship_of_the_parent_of_mother + expect(page).to have_content("Mum") + end + + def and_i_change_the_relationship_of_the_parent_to_other + choose "Other" + fill_in "Relationship to the child", with: "Someone" + click_on "Continue" + end + + def then_i_see_the_new_relationship_of_the_parent_of_other + expect(page).to have_content("Someone") + end + + def when_i_change_the_contact_details_of_the_parent + fill_in "Email address", with: "selina@meyer.com" + fill_in "Phone number", with: "07700 900 000" + click_on "Continue" + end + + def then_i_see_the_new_contact_details_of_the_parent + expect(page).to have_content("selina@meyer.com") + expect(page).to have_content("07700 900000") + end +end