Skip to content

Commit

Permalink
Resolve issue with autocomplete pre-filling back link
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie committed Jan 31, 2024
1 parent cbda8d1 commit 7cb1f46
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 25 deletions.
6 changes: 5 additions & 1 deletion app/controllers/claims/support/schools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def show
end

def new
@school_form = SchoolOnboardingForm.new
@school_form = if params[:school].present?
school_form
else
SchoolOnboardingForm.new
end
end

def school_options
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/placements/support/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ class Placements::Support::ProvidersController < Placements::Support::Applicatio
before_action :redirect_to_provider_options, only: :check, if: -> { javascript_disabled? }

def new
@provider_form = ProviderOnboardingForm.new
@provider_form = if params[:provider].present?
provider_form
else
ProviderOnboardingForm.new
end
end

def show
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/placements/support/schools_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ class Placements::Support::SchoolsController < Placements::Support::ApplicationC
before_action :redirect_to_school_options, only: :check, if: -> { javascript_disabled? }

def new
@school_form = SchoolOnboardingForm.new
@school_form = if params[:school].present?
school_form
else
SchoolOnboardingForm.new
end
end

def school_options
Expand Down
14 changes: 10 additions & 4 deletions app/forms/provider_onboarding_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ProviderOnboardingForm < ApplicationForm
attr_accessor :id, :javascript_disabled

validate :id_presence
validate :provider_exists?
validate :provider_already_onboarded?

def onboard
Expand All @@ -11,14 +12,19 @@ def onboard
end

def provider
@provider ||= Provider.find(id)
rescue ActiveRecord::RecordNotFound
errors.add(:id, :blank)
nil
@provider ||= Provider.find_by(id:)
end

def as_form_params
{ "provider" => { id:, javascript_disabled: } }
end

private

def provider_exists?
errors.add(:id, :blank) if provider.blank?
end

def provider_already_onboarded?
if provider&.placements_service?
errors.add(:id, :already_added, provider_name: provider.name)
Expand Down
14 changes: 10 additions & 4 deletions app/forms/school_onboarding_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class SchoolOnboardingForm < ApplicationForm

validate :id_presence
validates :service, presence: true, inclusion: { in: %i[placements claims] }
validate :school_exists?
validate :school_already_onboarded?

def onboard
Expand All @@ -12,14 +13,19 @@ def onboard
end

def school
@school ||= School.find(id)
rescue ActiveRecord::RecordNotFound
errors.add(:id, :blank)
nil
@school ||= School.find_by(id:)
end

def as_form_params
{ "school" => { id:, service:, javascript_disabled: } }
end

private

def school_exists?
errors.add(:id, :blank) if school.blank?
end

def school_already_onboarded?
if school&.try("#{service}_service?")
errors.add(:id, :already_added, school_name: school.name)
Expand Down
24 changes: 22 additions & 2 deletions app/javascript/controllers/autocomplete_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class extends Controller {
id: this.serverInputTarget.id,
showNoOptionsFound: true,
name: this.inputTarget.dataset.inputName,
defaultValue: this.serverInputTarget.value,
minLength,
source: debounce(request(this.pathValue), 900),
templates: {
Expand All @@ -33,23 +34,42 @@ export default class extends Controller {
// Hijack the original input to submit the selected value.
this.serverInputTarget.id = `old-${this.serverInputTarget.id}`;
this.serverInputTarget.type = "hidden";
this.serverInputTarget.value = this.serverInputTarget.dataset.previousSearch || '';
}

clearUndefinedSuggestions() {
const autocompleteElement = this.element.getElementsByClassName("autocomplete__wrapper")[0];
if (autocompleteElement) {
const autocompleteOptions = autocompleteElement.getElementsByClassName("autocomplete__option");
for (const option of autocompleteOptions) {
if ( option.innerHTML === ""){
option.remove();
}
}
}
}

private

inputValueTemplate(result) {
if (result) return result.name;
if (result && typeof result === "object") {
return result.name;
} else {
return ""
}
}

resultTemplate(result) {
if (result) {
if (result && typeof result === "object") {
var returnString = `${result.name}`
var attributesString = ''
this.returnAttributesValue.forEach(function(attribute) {
attributesString = attributesString.concat(`${result[attribute]} `)
});

return returnString.concat(` (${attributesString.trim()})`)
} else {
return ""
}
}

Expand Down
4 changes: 3 additions & 1 deletion app/views/claims/support/schools/check.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<%= content_for(:before_content) do %>
<%= govuk_back_link(href: new_claims_support_school_path) %>
<%= govuk_back_link(href: new_claims_support_school_path(
@school_form.as_form_params,
)) %>
<% end %>

<div class="govuk-width-container">
Expand Down
9 changes: 6 additions & 3 deletions app/views/claims/support/schools/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= content_tag(:div, class: class_names("govuk-form-group", "govuk-form-group--error": f.object.errors.present?)) do %>
<%= f.govuk_text_field :id, value: nil,
<%= f.govuk_text_field :id, value: @school_form&.school&.name,
label: { text: t(".title"), size: "l" },
caption: { text: t(".caption"), size: "l" },
data: { autocomplete_target: "serverInput" } %>
data: { autocomplete_target: "serverInput",
previous_search: @school_form&.school&.id } %>

<div class="govuk-!-margin-bottom-7" data-autocomplete-target="input" data-input-name="school[name]"></div>
<div class="govuk-!-margin-bottom-7" data-autocomplete-target="input" data-input-name="school[name]"
data-action="focusin->autocomplete#clearUndefinedSuggestions click->autocomplete#clearUndefinedSuggestions">
</div>
<% end %>

<%= f.govuk_submit t(".continue") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/placements/support/providers/check.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% content_for :page_title, t(".title") %>
<%= render "placements/support/primary_navigation", current_navigation: :organisations %>
<%= content_for(:before_content) do %>
<%= govuk_back_link(href: new_placements_support_provider_path) %>
<%= govuk_back_link(href: new_placements_support_provider_path(@provider_form.as_form_params)) %>
<% end %>

<div class="govuk-width-container">
Expand Down
9 changes: 6 additions & 3 deletions app/views/placements/support/providers/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= content_tag(:div, class: class_names("govuk-form-group", "govuk-form-group--error": f.object.errors.present?)) do %>
<%= f.govuk_text_field :id, value: nil,
<%= f.govuk_text_field :id, value: @provider_form&.provider&.name,
label: { text: t(".title"), size: "l" },
caption: { text: t(".caption"), size: "l" },
data: { autocomplete_target: "serverInput" } %>
data: { autocomplete_target: "serverInput",
previous_search: @provider_form&.provider&.id } %>

<div class="govuk-!-margin-bottom-7" data-autocomplete-target="input" data-input-name="provider[name]"></div>
<div class="govuk-!-margin-bottom-7" data-autocomplete-target="input"
data-input-name="provider[name]"
data-action="focusin->autocomplete#clearUndefinedSuggestions click->autocomplete#clearUndefinedSuggestions"></div>
<% end %>

<%= f.govuk_submit t(".continue") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/placements/support/schools/check.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<% content_for :page_title, t(".title") %>
<%= render "placements/support/primary_navigation", current_navigation: :organisations %>
<%= content_for(:before_content) do %>
<%= govuk_back_link(href: new_placements_support_school_path) %>
<%= govuk_back_link(href: new_placements_support_school_path(@school_form.as_form_params)) %>
<% end %>

<div class="govuk-width-container">
Expand Down
9 changes: 6 additions & 3 deletions app/views/placements/support/schools/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= content_tag(:div, class: class_names("govuk-form-group", "govuk-form-group--error": f.object.errors.present?)) do %>
<%= f.govuk_text_field :id, value: nil,
<%= f.govuk_text_field :id, value: @school_form&.school&.name,
label: { text: t(".title"), size: "l" },
caption: { text: t(".caption"), size: "l" },
data: { autocomplete_target: "serverInput" } %>
data: { autocomplete_target: "serverInput",
previous_search: @school_form&.school&.id } %>

<div class="govuk-!-margin-bottom-7" data-autocomplete-target="input" data-input-name="school[name]"></div>
<div class="govuk-!-margin-bottom-7" data-autocomplete-target="input" data-input-name="school[name]"
data-action="focusin->autocomplete#clearUndefinedSuggestions click->autocomplete#clearUndefinedSuggestions">
</div>
<% end %>

<%= f.govuk_submit t(".continue") %>
Expand Down
15 changes: 15 additions & 0 deletions spec/forms/provider_onboarding_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,19 @@
onboarding.to change(provider, :placements_service).from(false).to(true)
end
end

describe "as_form_params" do
it "returns form params" do
expect(described_class.new(
id: "1234",
javascript_disabled: true,
).as_form_params).to eq({
"provider" =>
{
id: "1234",
javascript_disabled: true,
},
})
end
end
end
17 changes: 17 additions & 0 deletions spec/forms/school_onboarding_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,21 @@
onboarding.to change(school, :placements_service).from(false).to(true)
end
end

describe "as_form_params" do
it "returns form params" do
expect(described_class.new(
id: "1234",
javascript_disabled: true,
service: "claims",
).as_form_params).to eq({
"school" =>
{
id: "1234",
javascript_disabled: true,
service: "claims",
},
})
end
end
end
23 changes: 23 additions & 0 deletions spec/system/claims/support/schools/add_a_school_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
then_i_see_an_error("Enter a school name, URN or postcode")
end

scenario "Colin reconsiders onboarding a school", js: true do
given_i_have_completed_the_form_to_onboard(school:)
when_i_click_back
then_i_see_the_search_input_pre_filled_with("School 1")
and_i_click_continue
then_i_see_the_check_details_page_for_school("School 1")
end

private

def and_there_is_an_existing_persona_for(persona_name)
Expand Down Expand Up @@ -79,6 +87,10 @@ def and_i_click_continue
click_on "Continue"
end

def when_i_click_back
click_on "Back"
end

def then_i_see_the_check_details_page_for_school(school_name)
expect(page).to have_css(".govuk-caption-l", text: "Add organisation")
expect(page).to have_content("Check your answers")
Expand Down Expand Up @@ -113,4 +125,15 @@ def and_a_school_is_listed(school_name:)
def and_i_see_success_message
expect(page).to have_content "Organisation added"
end

def given_i_have_completed_the_form_to_onboard(school:)
params = { school: { id: school.id, name: school.name } }
visit check_claims_support_schools_path(params)
end

def then_i_see_the_search_input_pre_filled_with(school_name)
within(".autocomplete__wrapper") do
expect(page.find("#school-id-field").value).to eq(school_name)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@
then_i_see_an_error("Select a school")
end

scenario "Colin reconsiders onboarding a school" do
given_i_have_completed_the_form_to_onboard("Manchester 1")
when_i_click_back
then_i_see_the_search_input_pre_filled_with("Manchester 1")
and_i_click_continue
then_i_see_list_of_schools
then_i_choose("Manchester 1")
and_i_click_continue
then_i_see_the_check_details_page_for_school("Manchester 1")
end

private

def and_there_is_an_existing_persona_for(persona_name)
Expand Down Expand Up @@ -84,6 +95,10 @@ def and_i_click_continue
click_on "Continue"
end

def when_i_click_back
click_on "Back"
end

def then_i_see_list_of_schools
expect(page).to have_content("Manchester 1")
expect(page).to have_content("Manchester 2")
Expand Down Expand Up @@ -124,4 +139,14 @@ def then_i_see_an_error(error_message)
def and_a_school_is_listed(school_name:)
expect(page).to have_content(school_name)
end

def given_i_have_completed_the_form_to_onboard(school_name)
school = School.find_by(name: school_name)
params = { school: { id: school.id, name: school.name } }
visit check_placements_support_schools_path(params)
end

def then_i_see_the_search_input_pre_filled_with(school_name)
expect(page.find("#school-id-field").value).to eq(school_name)
end
end
Loading

0 comments on commit 7cb1f46

Please sign in to comment.