From 16acf7363fade331718de79e0faeec77b8d93c5e Mon Sep 17 00:00:00 2001 From: Kat <54268893+kosiakkatrina@users.noreply.github.com> Date: Thu, 9 Jan 2025 12:59:45 +0000 Subject: [PATCH] Use date_picker for location deactivation periods --- app/controllers/locations_controller.rb | 8 ++--- app/views/components/_date_picker.html.erb | 6 ++-- app/views/form/_date_question.html.erb | 2 +- app/views/locations/toggle_active.html.erb | 15 ++++++---- config/locales/en.yml | 2 +- spec/requests/locations_controller_spec.rb | 34 +++++++++++----------- 6 files changed, 35 insertions(+), 32 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 9109730dd..6494c481f 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -297,13 +297,11 @@ private return elsif params[:location_deactivation_period]["#{key}_type".to_sym] == "default" return FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period - elsif params[:location_deactivation_period][key.to_sym].present? - return params[:location_deactivation_period][key.to_sym] end - day = params[:location_deactivation_period]["#{key}(3i)"] - month = params[:location_deactivation_period]["#{key}(2i)"] - year = params[:location_deactivation_period]["#{key}(1i)"] + day = params[:location_deactivation_period][key.to_s].split("/")[0] + month = params[:location_deactivation_period][key.to_s].split("/")[1] + year = params[:location_deactivation_period][key.to_s].split("/")[2] return nil if [day, month, year].any?(&:blank?) Time.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) diff --git a/app/views/components/_date_picker.html.erb b/app/views/components/_date_picker.html.erb index a9faee16e..f7fd59f5a 100644 --- a/app/views/components/_date_picker.html.erb +++ b/app/views/components/_date_picker.html.erb @@ -2,7 +2,7 @@ <% question_has_errors = resource.errors[question_id].any? %>
"> <% if legend.present? %> - +

<%= legend[:caption][:text] if legend[:caption].present? %> @@ -12,11 +12,11 @@ <% end %> -
> +
"> <%= hint %>
<% if question_has_errors %> -

> +

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

<% end %> diff --git a/app/views/form/_date_question.html.erb b/app/views/form/_date_question.html.erb index daf845e59..55193b01a 100644 --- a/app/views/form/_date_question.html.erb +++ b/app/views/form/_date_question.html.erb @@ -3,7 +3,7 @@ { resource: @log, question_id: question.id, - legend: { text: legend(question, page_header, conditional)[:text], caption: caption(caption_text, page_header, conditional) }, + legend: { text: legend(question, page_header, conditional)[:text], size: "l", 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:, diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index 138f04fae..d2dc637a8 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -25,11 +25,16 @@ "other", label: { text: "For tenancies starting after a certain date" }, **basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "location") do %> - <%= f.govuk_date_field date_question(action), - legend: { text: "Date", size: "m" }, - hint: { text: "For example, 27 3 2022" }, - width: 20 %> - <% end %> + <%= render partial: "components/date_picker", locals: { + resource: @location, + question_id: date_question(action), + legend: { text: "Date", size: "m" }, + resource_type: "location", + hint: "For example, 27/3/2024", + f:, +} %> + <% end %> + <% end %> <%= f.govuk_submit "Continue" %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 97a6a4bff..155814574 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -413,7 +413,7 @@ en: name: "This is how you refer to this location within your organisation." units: "A unit is the space being let. For example, the property might be a block of flats and the unit would be the specific flat being let. A unit can also be a bedroom in a shared house or flat. Do not include spaces used for staff." toggle_active: "If the date is before %{date}, select ‘From the start of the open collection period’ because the previous period has now closed." - startdate: "For example, 27 3 2021." + startdate: "For example, 27/3/2024." scheme: toggle_active: "If the date is before %{date}, select ‘From the start of the open collection period’ because the previous period has now closed." bulk_upload: diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index d86190552..e8024bf76 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1548,7 +1548,7 @@ RSpec.describe LocationsController, type: :request do end context "with other date" do - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "10/10/2022" } } } context "and affected logs" do it "redirects to the confirmation page" do @@ -1708,7 +1708,7 @@ RSpec.describe LocationsController, type: :request do end context "when invalid date is entered" do - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "44", "deactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "10/44/2022" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -1717,7 +1717,7 @@ RSpec.describe LocationsController, type: :request do end context "when the date entered is before the beginning of current collection window" do - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "4", "deactivation_date(1i)": "2020" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "10/4/2020" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -1726,7 +1726,7 @@ RSpec.describe LocationsController, type: :request do end context "when the day is not entered" do - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "", "deactivation_date(2i)": "2", "deactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "/2/2022" } } } it "displays page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -1735,7 +1735,7 @@ RSpec.describe LocationsController, type: :request do end context "when the month is not entered" do - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "", "deactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "2//2022" } } } it "displays page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -1744,7 +1744,7 @@ RSpec.describe LocationsController, type: :request do end context "when the year is not entered" do - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "2", "deactivation_date(2i)": "2", "deactivation_date(1i)": "" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "2/2/" } } } it "displays page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -1754,7 +1754,7 @@ RSpec.describe LocationsController, type: :request do context "when deactivation date is during a deactivated period" do let(:deactivation_date) { Time.zone.local(2022, 10, 10) } - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "8/9/2022" } } } let(:add_deactivations) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 10, 12), location:) } it "displays page with an error message" do @@ -1765,7 +1765,7 @@ RSpec.describe LocationsController, type: :request do context "when there is an earlier open deactivation" do let(:deactivation_date) { Time.zone.local(2023, 10, 10) } - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2024" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "8/9/2024" } } } let(:add_deactivations) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2024, 6, 5), reactivation_date: nil, location:) } it "redirects to the location page and updates the existing deactivation period" do @@ -1780,7 +1780,7 @@ RSpec.describe LocationsController, type: :request do end context "when there is a later open deactivation" do - let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date(3i)": "8", "deactivation_date(2i)": "9", "deactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "8/9/2022" } } } let(:add_deactivations) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2024, 6, 5), reactivation_date: nil, location:) } it "redirects to the confirmation page" do @@ -2078,7 +2078,7 @@ RSpec.describe LocationsController, type: :request do end context "with other date" do - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "9", "reactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/9/2022" } } } it "redirects to the location page and displays a success banner" do expect(response).to redirect_to("/schemes/#{scheme.id}/locations/#{location.id}") @@ -2096,7 +2096,7 @@ RSpec.describe LocationsController, type: :request do end context "with other future date" do - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "14", "reactivation_date(2i)": "12", "reactivation_date(1i)": "2023" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "14/12/2023" } } } it "redirects to the location page and displays a success banner" do expect(response).to redirect_to("/schemes/#{scheme.id}/locations/#{location.id}") @@ -2116,7 +2116,7 @@ RSpec.describe LocationsController, type: :request do end context "when invalid date is entered" do - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "44", "reactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/44/2022" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -2125,7 +2125,7 @@ RSpec.describe LocationsController, type: :request do end context "when the date is entered is before the beginning of current collection window" do - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "4", "reactivation_date(1i)": "2020" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/4/2020" } } } it "displays the new page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -2134,7 +2134,7 @@ RSpec.describe LocationsController, type: :request do end context "when the day is not entered" do - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "", "reactivation_date(2i)": "2", "reactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "/2/2022" } } } it "displays page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -2143,7 +2143,7 @@ RSpec.describe LocationsController, type: :request do end context "when the month is not entered" do - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "2", "reactivation_date(2i)": "", "reactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "2//2022" } } } it "displays page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -2152,7 +2152,7 @@ RSpec.describe LocationsController, type: :request do end context "when the year is not entered" do - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "2", "reactivation_date(2i)": "2", "reactivation_date(1i)": "" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "2/2/" } } } it "displays page with an error message" do expect(response).to have_http_status(:unprocessable_content) @@ -2162,7 +2162,7 @@ RSpec.describe LocationsController, type: :request do context "when the reactivation date is before deactivation date" do let(:deactivation_date) { Time.zone.local(2022, 10, 10) } - let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date(3i)": "8", "reactivation_date(2i)": "9", "reactivation_date(1i)": "2022" } } } + let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "8/9/2022" } } } it "displays page with an error message" do expect(response).to have_http_status(:unprocessable_content)