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. 15
      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 return
elsif params[:location_deactivation_period]["#{key}_type".to_sym] == "default" elsif params[:location_deactivation_period]["#{key}_type".to_sym] == "default"
return FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period 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 end
day = params[:location_deactivation_period]["#{key}(3i)"] day = params[:location_deactivation_period][key.to_s].split("/")[0]
month = params[:location_deactivation_period]["#{key}(2i)"] month = params[:location_deactivation_period][key.to_s].split("/")[1]
year = params[:location_deactivation_period]["#{key}(1i)"] year = params[:location_deactivation_period][key.to_s].split("/")[2]
return nil if [day, month, year].any?(&:blank?) 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) 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? %> <% question_has_errors = resource.errors[question_id].any? %>
<div class="govuk-form-group<%= " govuk-form-group--error" if question_has_errors %>"> <div class="govuk-form-group<%= " govuk-form-group--error" if question_has_errors %>">
<% if legend.present? %> <% 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"> <h1 class="govuk-fieldset__heading">
<span class="govuk-caption-l"> <span class="govuk-caption-l">
<%= legend[:caption][:text] if legend[:caption].present? %> <%= legend[:caption][:text] if legend[:caption].present? %>
@ -12,11 +12,11 @@
</legend> </legend>
<% end %> <% 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 %> <%= hint %>
</div> </div>
<% if question_has_errors %> <% 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 %> <span class="govuk-visually-hidden">Error:</span> <%= resource.errors[question_id].first %>
</p> </p>
<% end %> <% end %>

2
app/views/form/_date_question.html.erb

@ -3,7 +3,7 @@
{ {
resource: @log, resource: @log,
question_id: question.id, 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, 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(' ', '/')}", 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:, f:,

15
app/views/locations/toggle_active.html.erb

@ -25,11 +25,16 @@
"other", "other",
label: { text: "For tenancies starting after a certain date" }, label: { text: "For tenancies starting after a certain date" },
**basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "location") do %> **basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "location") do %>
<%= f.govuk_date_field date_question(action), <%= render partial: "components/date_picker", locals: {
legend: { text: "Date", size: "m" }, resource: @location,
hint: { text: "For example, 27 3 2022" }, question_id: date_question(action),
width: 20 %> legend: { text: "Date", size: "m" },
<% end %> resource_type: "location",
hint: "For example, 27/3/2024",
f:,
} %>
<% end %>
<% end %> <% end %>
<%= f.govuk_submit "Continue" %> <%= 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." 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." 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." 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: 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." 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: bulk_upload:

34
spec/requests/locations_controller_spec.rb

@ -1548,7 +1548,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "with other date" do 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 context "and affected logs" do
it "redirects to the confirmation page" do it "redirects to the confirmation page" do
@ -1708,7 +1708,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when invalid date is entered" do 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 it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -1717,7 +1717,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the date entered is before the beginning of current collection window" do 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 it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -1726,7 +1726,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the day is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -1735,7 +1735,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the month is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -1744,7 +1744,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the year is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content) 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 context "when deactivation date is during a deactivated period" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) } 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:) } 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 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 context "when there is an earlier open deactivation" do
let(:deactivation_date) { Time.zone.local(2023, 10, 10) } 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:) } 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 it "redirects to the location page and updates the existing deactivation period" do
@ -1780,7 +1780,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when there is a later open deactivation" do 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:) } 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 it "redirects to the confirmation page" do
@ -2078,7 +2078,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "with other date" do 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 it "redirects to the location page and displays a success banner" do
expect(response).to redirect_to("/schemes/#{scheme.id}/locations/#{location.id}") expect(response).to redirect_to("/schemes/#{scheme.id}/locations/#{location.id}")
@ -2096,7 +2096,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "with other future date" do 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 it "redirects to the location page and displays a success banner" do
expect(response).to redirect_to("/schemes/#{scheme.id}/locations/#{location.id}") expect(response).to redirect_to("/schemes/#{scheme.id}/locations/#{location.id}")
@ -2116,7 +2116,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when invalid date is entered" do 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 it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -2125,7 +2125,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the date is entered is before the beginning of current collection window" do 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 it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -2134,7 +2134,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the day is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -2143,7 +2143,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the month is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)
@ -2152,7 +2152,7 @@ RSpec.describe LocationsController, type: :request do
end end
context "when the year is not entered" do 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content) 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 context "when the reactivation date is before deactivation date" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) } 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 it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content) expect(response).to have_http_status(:unprocessable_content)

Loading…
Cancel
Save