From 010251b7e216fbfd4963315ef6d291d376691a43 Mon Sep 17 00:00:00 2001 From: Louis Kirkham Date: Mon, 30 Sep 2024 10:36:47 +0100 Subject: [PATCH] fix: update year when loading search Signed-off-by: Louis Kirkham --- app/controllers/concerns/form_answer_mixin.rb | 18 ++++++++++++++++-- app/views/admin/form_answers/index.html.slim | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/concerns/form_answer_mixin.rb b/app/controllers/concerns/form_answer_mixin.rb index 08241a68f..4eeec197d 100644 --- a/app/controllers/concerns/form_answer_mixin.rb +++ b/app/controllers/concerns/form_answer_mixin.rb @@ -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 admin_form_answers_path(search_id: search.id, year: search_params[:year]) and return + end end end diff --git a/app/views/admin/form_answers/index.html.slim b/app/views/admin/form_answers/index.html.slim index 9188484d9..1570feccf 100644 --- a/app/views/admin/form_answers/index.html.slim +++ b/app/views/admin/form_answers/index.html.slim @@ -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