Browse Source

Use date_picker for location deactivation periods

pull/2891/head
Kat 4 months ago
parent
commit
16acf7363f
  1. 8
      app/controllers/locations_controller.rb
  2. 6
      app/views/components/_date_picker.html.erb
  3. 2
      app/views/form/_date_question.html.erb
  4. 11
      app/views/locations/toggle_active.html.erb
  5. 2
      config/locales/en.yml
  6. 34
      spec/requests/locations_controller_spec.rb

8
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)

6
app/views/components/_date_picker.html.erb

@ -2,7 +2,7 @@
<% question_has_errors = resource.errors[question_id].any? %>
<div class="govuk-form-group<%= " govuk-form-group--error" if question_has_errors %>">
<% if legend.present? %>
<legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
<legend class="govuk-fieldset__legend govuk-fieldset__legend--<%= legend[:size] %>">
<h1 class="govuk-fieldset__heading">
<span class="govuk-caption-l">
<%= legend[:caption][:text] if legend[:caption].present? %>
@ -12,11 +12,11 @@
</legend>
<% end %>
<div id=<%= [resource_type.dasherize, question_id.dasherize, "hint"].join("-") %>>
<div class="govuk-hint" id="<%= [resource_type.dasherize, question_id.to_s.dasherize, "hint"].join("-") %>">
<%= hint %>
</div>
<% if question_has_errors %>
<p id=<%= [resource_type.dasherize, question_id.dasherize, "error"].join("-") %>>
<p class="govuk-error-message" id="<%= [resource_type.dasherize, question_id.to_s.dasherize, "error"].join("-") %>">
<span class="govuk-visually-hidden">Error:</span> <%= resource.errors[question_id].first %>
</p>
<% end %>

2
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 + "</br></br>".html_safe)) + "For example, #{date_mid_collection_year_formatted(@log.startdate).tr(' ', '/')}",
f:,

11
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),
<%= render partial: "components/date_picker", locals: {
resource: @location,
question_id: date_question(action),
legend: { text: "Date", size: "m" },
hint: { text: "For example, 27 3 2022" },
width: 20 %>
resource_type: "location",
hint: "For example, 27/3/2024",
f:,
} %>
<% end %>
<% end %>
<%= f.govuk_submit "Continue" %>

2
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:

34
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)

Loading…
Cancel
Save