From 4197b96e20c98e04d91979d0289e421ce2ff6c63 Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 9 Jan 2025 11:57:55 +0000 Subject: [PATCH] Refactor date_picker into component, add date_picker to location startdate --- app/controllers/locations_controller.rb | 8 +++--- app/views/components/_date_picker.html.erb | 25 ++++++++++++++++ app/views/form/_date_question.html.erb | 33 ++++++---------------- app/views/locations/availability.erb | 12 ++++---- spec/requests/locations_controller_spec.rb | 12 ++++---- 5 files changed, 51 insertions(+), 39 deletions(-) create mode 100644 app/views/components/_date_picker.html.erb diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index a90833945..9109730dd 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -137,9 +137,9 @@ class LocationsController < ApplicationController def availability; end def update_availability - day = location_params["startdate(3i)"] - month = location_params["startdate(2i)"] - year = location_params["startdate(1i)"] + day = location_params["startdate"].split("/")[0] + month = location_params["startdate"].split("/")[1] + year = location_params["startdate"].split("/")[2] @location.startdate = if [day, month, year].none?(&:blank?) && Date.valid_date?(year.to_i, month.to_i, day.to_i) Time.zone.local(year.to_i, month.to_i, day.to_i) end @@ -258,7 +258,7 @@ private end def location_params - required_params = params.require(:location).permit(:postcode, :location_admin_district, :location_code, :name, :units, :type_of_unit, :mobility_type, "startdate(1i)", "startdate(2i)", "startdate(3i)").merge(scheme_id: @scheme.id) + required_params = params.require(:location).permit(:postcode, :location_admin_district, :location_code, :name, :units, :type_of_unit, :mobility_type, :startdate).merge(scheme_id: @scheme.id) required_params[:postcode] = PostcodeService.clean(required_params[:postcode]) if required_params[:postcode] required_params[:location_admin_district] = nil if required_params[:location_admin_district] == "Select an option" required_params diff --git a/app/views/components/_date_picker.html.erb b/app/views/components/_date_picker.html.erb new file mode 100644 index 000000000..a9faee16e --- /dev/null +++ b/app/views/components/_date_picker.html.erb @@ -0,0 +1,25 @@ +
+ <% question_has_errors = resource.errors[question_id].any? %> +
"> + <% if legend.present? %> + +

+ + <%= legend[:caption][:text] if legend[:caption].present? %> + + <%= legend[:text] %> +

+
+ <% end %> + +
> + <%= hint %> +
+ <% if question_has_errors %> +

> + Error: <%= resource.errors[question_id].first %> +

+ <% end %> + <%= f.text_field question_id.to_sym, class: "govuk-input moj-js-datepicker-input#{' govuk-input--error' if question_has_errors}", id: [resource_type, question_id].join("_"), aria: { describedby: "#{[resource_type, question_id].join('_')}-hint#{"#{[resource_type, question_id].join('_')} -error" if question_has_errors}" }, autocomplete: "off", value: resource[question_id]&.strftime("%d/%m/%Y") %> +
+
diff --git a/app/views/form/_date_question.html.erb b/app/views/form/_date_question.html.erb index 6258a83f0..daf845e59 100644 --- a/app/views/form/_date_question.html.erb +++ b/app/views/form/_date_question.html.erb @@ -1,29 +1,14 @@ <%= render partial: "form/guidance/#{question.top_guidance_partial}" if question.top_guidance? %> +<%= render partial: "components/date_picker", locals: + { + resource: @log, + question_id: question.id, + legend: { text: legend(question, page_header, conditional)[:text], caption: caption(caption_text, page_header, conditional) }, + resource_type: @log.log_type, + hint: (question.hint_text.blank? ? "" : (question.hint_text.html_safe + "

".html_safe)) + "For example, #{date_mid_collection_year_formatted(@log.startdate).tr(' ', '/')}", + f:, + } %> -
- <% question_has_errors = @log.errors[question.id].any? %> -
"> - -

- - <% caption = caption(caption_text, page_header, conditional) %> - <%= caption[:text] if caption.present? %> - - <%= legend(question, page_header, conditional)[:text] %> -

-
- -
class="govuk-hint"> - <%= (question.hint_text.blank? ? "" : (question.hint_text.html_safe + "

".html_safe)) + "For example, #{date_mid_collection_year_formatted(@log.startdate).tr(' ', '/')}" %> -
- <% if question_has_errors %> -

> - Error: <%= @log.errors[question.id].first %> -

- <% end %> - <%= f.text_field question.id.to_sym, class: "govuk-input moj-js-datepicker-input#{' govuk-input--error' if question_has_errors}", id: "date", aria: { describedby: "date-hint#{' date-error' if question_has_errors}" }, autocomplete: "off", value: @log[question.id]&.strftime("%d/%m/%Y") %> -
-
<%= govuk_inset_text(text: question.unresolved_hint_text) if question.unresolved_hint_text.present? && @log.unresolved %> <%= render partial: "form/guidance/#{question.bottom_guidance_partial}" if question.bottom_guidance? %> diff --git a/app/views/locations/availability.erb b/app/views/locations/availability.erb index 8a00ddc89..9c6aca26d 100644 --- a/app/views/locations/availability.erb +++ b/app/views/locations/availability.erb @@ -12,11 +12,13 @@ <%= f.govuk_error_summary %> <%= render partial: "organisations/headings", locals: { main: I18n.t("questions.location.startdate"), sub: "Add a location to #{@scheme.service_name}" } %> - - <%= f.govuk_date_field :startdate, - hint: { text: I18n.t("hints.location.startdate") }, - legend: nil, - width: 20 %> + <%= render partial: "components/date_picker", locals: { + resource: @location, + question_id: "startdate", + legend: nil, + resource_type: "location", + hint: I18n.t("hints.location.startdate"), + f: } %>
<% if params[:referrer] == "check_answers" %> diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 9afdc94f2..d86190552 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1151,7 +1151,7 @@ RSpec.describe LocationsController, type: :request do end context "when startdate is submitted" do - let(:params) { { location: { "startdate(1i)": "2022", "startdate(2i)": "1", "startdate(3i)": "2" } } } + let(:params) { { location: { "startdate": "2/1/2022" } } } before do patch "/schemes/#{scheme.id}/locations/#{location.id}/availability", params: @@ -1168,7 +1168,7 @@ RSpec.describe LocationsController, type: :request do end context "when startdate is submitted with leading zeroes" do - let(:params) { { location: { "startdate(1i)": "2022", "startdate(2i)": "01", "startdate(3i)": "02" } } } + let(:params) { { location: { "startdate": "02/01/2022" } } } before do patch "/schemes/#{scheme.id}/locations/#{location.id}/availability", params: @@ -1185,7 +1185,7 @@ RSpec.describe LocationsController, type: :request do end context "when startdate is missing" do - let(:params) { { location: { "startdate(1i)": "", "startdate(2i)": "", "startdate(3i)": "" } } } + let(:params) { { location: { "startdate": "" } } } before do patch "/schemes/#{scheme.id}/locations/#{location.id}/availability", params: @@ -1225,7 +1225,7 @@ RSpec.describe LocationsController, type: :request do end context "when startdate is submitted" do - let(:params) { { location: { "startdate(1i)": "2022", "startdate(2i)": "1", "startdate(3i)": "2" } } } + let(:params) { { location: { "startdate": "2/1/2022" } } } before do patch "/schemes/#{scheme.id}/locations/#{location.id}/availability", params: @@ -1242,7 +1242,7 @@ RSpec.describe LocationsController, type: :request do end context "when startdate is submitted with leading zeroes" do - let(:params) { { location: { "startdate(1i)": "2022", "startdate(2i)": "01", "startdate(3i)": "02" } } } + let(:params) { { location: { "startdate": "02/01/2022" } } } before do patch "/schemes/#{scheme.id}/locations/#{location.id}/availability", params: @@ -1259,7 +1259,7 @@ RSpec.describe LocationsController, type: :request do end context "when startdate is missing" do - let(:params) { { location: { "startdate(1i)": "", "startdate(2i)": "", "startdate(3i)": "" } } } + let(:params) { { location: { "startdate": "" } } } before do patch "/schemes/#{scheme.id}/locations/#{location.id}/availability", params: