Skip to content

Commit

Permalink
Merge pull request #4526 from DFE-Digital/bug/fix-back-to-results-bac…
Browse files Browse the repository at this point in the history
…k-button

Fix back to results back button
  • Loading branch information
gms-gs authored Sep 20, 2024
2 parents 054b985 + 3d007d8 commit 5e701ba
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 26 deletions.
3 changes: 2 additions & 1 deletion app/components/find/results/results_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<%= render Find::Results::SearchResultComponent.new(
course:,
filtered_by_location: results.location_filter?,
sites_count: results.sites_count(course)
sites_count: results.sites_count(course),
search_params:
) %>
<% end %>
</ul>
Expand Down
5 changes: 3 additions & 2 deletions app/components/find/results/results_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ module Results
class ResultsComponent < ViewComponent::Base
include ::ViewHelper

attr_reader :results, :courses
attr_reader :results, :courses, :search_params

def initialize(results:, courses:)
def initialize(results:, courses:, search_params:)
super
@results = results
@courses = courses
@search_params = search_params
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/components/find/results/search_result_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ class SearchResultComponent < ViewComponent::Base

delegate :age_range_in_years_and_level, :course_length_with_study_mode, to: :course

def initialize(course:, filtered_by_location: false, sites_count: 0)
def initialize(course:, search_params:, filtered_by_location: false, sites_count: 0)
super
@course = course.decorate
@filtered_by_location = filtered_by_location
@sites_count = sites_count
@search_params = search_params
end

def filtered_by_location?
Expand All @@ -27,7 +28,7 @@ def has_sites?
def coure_title_link
t(
'.course_title_html',
course_path: find_course_path(provider_code: course.provider_code, course_code: course.course_code),
course_path: find_course_path(provider_code: course.provider_code, course_code: course.course_code, search_params: @search_params.to_query),
provider_name: helpers.smart_quotes(course.provider.provider_name),
course_name: course.name_and_code
)
Expand Down
58 changes: 58 additions & 0 deletions app/controllers/find/courses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CoursesController < ApplicationController
before_action -> { render_not_found if provider.nil? }

before_action :render_feedback_component, only: :show
before_action :set_search_params, only: :show

def show
@course = provider.courses.includes(
Expand All @@ -19,5 +20,62 @@ def show

render_not_found unless @course.is_published?
end

def legacy_paramater_keys
%i[
fulltime
hasvacancies
lat
lng
parttime
prev_l
prev_lat
prev_lng
prev_loc
prev_lq
prev_query
prev_rad
qualifications
query
rad
senCourses
]
end

def set_search_params
return if params[:search_params].blank?

session[:search_params] = ActionController::Parameters.new(
Rack::Utils.parse_nested_query(params[:search_params])
).permit(
*legacy_paramater_keys,
:visa_status,
:age_group,
:c,
:can_sponsor_visa,
:degree_required,
:engineers_teach_physics,
:funding,
:has_vacancies,
:university_degree_status,
:applications_open,
:l,
:latitude,
:loc,
:long,
:longitude,
:lq,
:radius,
:send_courses,
:sortby,
'provider.provider_name',
c: [],
qualification: [],
qualifications: [], # Legacy
study_type: [],
subjects: [],
subject_codes: [] # Legacy
)
end
end
end
2 changes: 2 additions & 0 deletions app/controllers/find/results_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ResultsController < ApplicationController
def index
matched_params = MatchOldParams.call(request.query_parameters)

@search_params = matched_params

@results_view = ResultsView.new(query_parameters: matched_params)
@filters_view = ResultFilters::FiltersView.new(params: matched_params)
@courses = @results_view.courses.page params[:page]
Expand Down
2 changes: 1 addition & 1 deletion app/views/find/courses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<%= content_for(:before_content) do %>
<%= govuk_back_link(
text: t("find.courses.show.back_to_search"),
href: request.referer,
href: find_results_path(session[:search_params]),
html_attributes: {
data: { qa: "page-back" }
}
Expand Down
3 changes: 2 additions & 1 deletion app/views/find/results/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

<%= render Find::Results::ResultsComponent.new(
results: @results_view,
courses: @courses
courses: @courses,
search_params: @search_params
) %>
30 changes: 26 additions & 4 deletions spec/components/find/results/results_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ module Find
end

context 'when there are no search results' do
let(:search_params) do
{ 'age_group' => 'primary',
'applications_open' => 'true',
'can_sponsor_visa' => 'false',
'has_vacancies' => 'true',
'l' => '2',
'subjects' => ['00'],
'visa_status' => 'false' }
end

let(:results_view) do
instance_double(
Find::ResultsView,
Expand All @@ -29,21 +39,31 @@ module Find

it 'renders a "No courses found" message when there are no results' do
component = render_inline(
described_class.new(results: results_view, courses:)
described_class.new(results: results_view, courses:, search_params:)
)

expect(component.text).to include('No courses found')
end

it 'renders the inset text' do
component = render_inline(
described_class.new(results: results_view, courses:)
described_class.new(results: results_view, courses:, search_params:)
)
expect(component.text).to include('event near you')
end
end

context 'when there are 10 matching courses' do
let(:search_params) do
{ 'age_group' => 'primary',
'applications_open' => 'true',
'can_sponsor_visa' => 'false',
'has_vacancies' => 'true',
'l' => '2',
'subjects' => ['00'],
'visa_status' => 'false' }
end

let(:results_view) do
instance_double(
Find::ResultsView,
Expand All @@ -68,12 +88,13 @@ module Find
allow(Results::SearchResultComponent).to receive(:new).and_return(plain: '')

component = render_inline(
described_class.new(results: results_view, courses:)
described_class.new(results: results_view, courses:, search_params:)
)

courses.each do |course|
expect(Results::SearchResultComponent).to have_received(:new).with(
course:,
search_params:,
filtered_by_location: false,
sites_count: 2
)
Expand All @@ -84,12 +105,13 @@ module Find

it 'renders the inset text' do
component = render_inline(
described_class.new(results: results_view, courses:)
described_class.new(results: results_view, courses:, search_params:)
)

courses.each do |course|
expect(Results::SearchResultComponent).to have_received(:new).with(
course:,
search_params:,
filtered_by_location: false,
sites_count: 2
)
Expand Down
40 changes: 25 additions & 15 deletions spec/components/find/results/search_result_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

module Find
describe Results::SearchResultComponent, type: :component do
let(:search_params) do
{ 'age_group' => 'primary',
'applications_open' => 'true',
'can_sponsor_visa' => 'false',
'has_vacancies' => 'true',
'l' => '2',
'subjects' => ['00'],
'visa_status' => 'false' }
end

context 'delegations' do
subject { described_class.new(course: build(:course)) }
subject { described_class.new(course: build(:course), search_params:) }

it { is_expected.to delegate_method(:age_range_in_years_and_level).to(:course) }
it { is_expected.to delegate_method(:course_length_with_study_mode).to(:course) }
Expand All @@ -17,7 +27,7 @@ module Find
:course,
degree_grade: :two_one
)
result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).to include(
'2:1 bachelor’s degree',
Expand All @@ -34,7 +44,7 @@ module Find
can_sponsor_student_visa: false,
can_sponsor_skilled_worker_visa: true
)
result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).to include(
'Skilled Worker visas can be sponsored'
Expand All @@ -50,7 +60,7 @@ module Find
can_sponsor_student_visa: false,
can_sponsor_skilled_worker_visa: true
)
result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).to include(
'Visas cannot be sponsored'
Expand All @@ -61,7 +71,7 @@ module Find
context 'when the provider specifies student visa sponsorship' do
it 'renders correct message when only one kind of visa is sponsored' do
course = build(:course, :can_sponsor_student_visa, :fee_type_based)
result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).to include(
'Student visas can be sponsored'
Expand All @@ -74,7 +84,7 @@ module Find
can_sponsor_student_visa: false,
can_sponsor_skilled_worker_visa: false
)
result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).to include(
'Visas cannot be sponsored'
Expand All @@ -88,7 +98,7 @@ module Find
:course,
accrediting_provider: build(:provider, :accredited_provider, provider_name: 'ACME SCITT A1')
)
result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).to include('QTS ratified by ACME SCITT A1')
end
Expand All @@ -100,7 +110,7 @@ module Find
:course,
accrediting_provider: nil
)
result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).not_to include('QTS ratified by')
end
Expand All @@ -110,7 +120,7 @@ module Find
it 'renders the uk fees' do
course = create(:course, enrichments: [create(:course_enrichment, fee_uk_eu: 9250)]).decorate

result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))
expect(result.text).to include('£9,250 for UK citizens')
expect(result.text).to include('Course fee')
end
Expand All @@ -120,7 +130,7 @@ module Find
it 'renders the international fees' do
course = create(:course, enrichments: [create(:course_enrichment, fee_international: 14_000)]).decorate

result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))
expect(result.text).to include('£14,000 for Non-UK citizens')
end
end
Expand All @@ -129,7 +139,7 @@ module Find
it 'renders the uk fees and not the internation fee label' do
course = create(:course, enrichments: [create(:course_enrichment, fee_uk_eu: 9250, fee_international: nil)]).decorate

result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).to include('£9,250 for UK citizens')
expect(result.text).not_to include('Non-UK citizens')
Expand All @@ -140,7 +150,7 @@ module Find
it 'renders the international fees but not the uk fee label' do
course = create(:course, enrichments: [create(:course_enrichment, fee_uk_eu: nil, fee_international: 14_000)]).decorate

result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).not_to include('for UK citizens')
expect(result.text).to include('£14,000 for Non-UK citizens')
Expand All @@ -151,7 +161,7 @@ module Find
it 'does not render the row' do
course = create(:course, enrichments: [create(:course_enrichment, fee_uk_eu: nil, fee_international: nil)]).decorate

result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result.text).not_to include('for UK citizens')
expect(result.text).not_to include('£14,000 for Non-UK citizens')
Expand All @@ -165,7 +175,7 @@ module Find
level: 'secondary',
age_range_in_years: '11_to_16')

result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result).to have_text('Age range 11 to 16 - secondary', normalize_ws: true)
end
Expand All @@ -179,7 +189,7 @@ module Find
study_mode: 'full_time'
)

result = render_inline(described_class.new(course:))
result = render_inline(described_class.new(course:, search_params:))

expect(result).to have_text('Course length 1 year - full time', normalize_ws: true)
end
Expand Down
Loading

0 comments on commit 5e701ba

Please sign in to comment.