Skip to content

Commit

Permalink
fix: update year when loading search
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Kirkham <[email protected]>
  • Loading branch information
TheDancingClown committed Sep 30, 2024
1 parent 84a5cd5 commit dfde2fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 16 additions & 2 deletions app/controllers/concerns/form_answer_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,33 @@ def save_or_load_search!
end
end

# If search is present, save it and clean up the params.
# Do not pass params[:year] to the redirect as this does not work with the non-js search
if params[:search] && params[:search][:search_filter] != FormAnswerSearch.default_search[:search_filter]
search = NominationSearch.create(serialized_query: params[:search].to_json)
redirect_to [namespace_name, :form_answers, search_id: search.id, year: params[:year]] and return
year = JSON.parse(search.serialized_query)[:year]
redirect_to [namespace_name, :form_answers, search_id: search.id, year: year] and return
end

if params[:search_id]
search = NominationSearch.find_by_id(params[:search_id])

if search.present?
payload = JSON.parse(search.serialized_query)
search_params[:search_filter] = payload["search_filter"]
search_params[:query] = payload["query"]
search_params[:sort] = payload["sort"]
search_params[:year] = payload["year"]

# Update the year parameter if it is different
if params[:year] && search_params[:year] != params[:year]
search_params[:year] = params[:year]
search.update_attribute(:serialized_query, search_params.to_json)
end

# Redirect to include the year parameter if it is not present
if !params[:year]
redirect_to [namespace_name, :form_answers, search_id: search.id, year: search_params[:year]] and return
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/form_answers/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ h1.govuk-heading-xl
| Nominations

= simple_form_for @search, url: admin_form_answers_path(year: params[:year]), method: :post, as: :search, html: { class: 'search-form'} do |f|
.govuk-grid-row
.govuk-grid-row.if-no-js-hide
= render "layouts/vertical_admin_award_year"

.govuk-grid-row
Expand Down

0 comments on commit dfde2fc

Please sign in to comment.