Browse Source

refactor into a helper

pull/1007/head
Kat 4 years ago
parent
commit
148f3126ba
  1. 17
      app/helpers/toggle_active_location_helper.rb
  2. 13
      app/views/locations/toggle_active.html.erb

17
app/helpers/toggle_active_location_helper.rb

@ -0,0 +1,17 @@
module ToggleActiveLocationHelper
def toggle_location_form_path(action, scheme_id, location_id)
if action == "deactivate"
scheme_location_new_deactivation_path(scheme_id:, location_id:)
else
scheme_location_new_reactivation_path(scheme_id:, location_id:)
end
end
def date_type_question(action)
action == "deactivate" ? :deactivation_date_type : :reactivation_date_type
end
def date_question(action)
action == "deactivate" ? :deactivation_date : :reactivation_date
end
end

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

@ -7,29 +7,26 @@
href: scheme_location_path(@location.scheme, @location), href: scheme_location_path(@location.scheme, @location),
) %> ) %>
<% end %> <% end %>
<% form_path = action == "deactivate" ? scheme_location_new_deactivation_path(scheme_id: @location.scheme.id, location_id: @location.id) : scheme_location_new_reactivation_path(scheme_id: @location.scheme.id, location_id: @location.id) %>
<%= form_with model: @location, url: form_path, method: "patch", local: true do |f| %> <%= form_with model: @location, url: toggle_location_form_path(action, @location.scheme.id, @location.id), method: "patch", local: true do |f| %>
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds"> <div class="govuk-grid-column-two-thirds">
<% collection_start_date = FormHandler.instance.current_collection_start_date %> <% collection_start_date = FormHandler.instance.current_collection_start_date %>
<% date_question = action == "deactivate" ? :deactivation_date : :reactivation_date %>
<% date_type_question = action == "deactivate" ? :deactivation_date_type : :reactivation_date_type %>
<%= f.govuk_error_summary %> <%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset date_type_question, <%= f.govuk_radio_buttons_fieldset date_type_question(action),
legend: { text: I18n.t("questions.location.deactivate.apply_from") }, legend: { text: I18n.t("questions.location.deactivate.apply_from") },
caption: { text: title }, caption: { text: title },
hint: { text: I18n.t("hints.location.deactivate", date: collection_start_date.to_formatted_s(:govuk_date)) } do %> hint: { text: I18n.t("hints.location.deactivate", date: collection_start_date.to_formatted_s(:govuk_date)) } do %>
<%= govuk_warning_text text: I18n.t("warnings.location.#{action}.existing_logs") %> <%= govuk_warning_text text: I18n.t("warnings.location.#{action}.existing_logs") %>
<%= f.govuk_radio_button date_type_question, <%= f.govuk_radio_button date_type_question(action),
"default", "default",
label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %>
<%= f.govuk_radio_button date_type_question, <%= f.govuk_radio_button date_type_question(action),
"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, <%= f.govuk_date_field date_question(action),
legend: { text: "Date", size: "m" }, legend: { text: "Date", size: "m" },
hint: { text: "For example, 27 3 2022" }, hint: { text: "For example, 27 3 2022" },
width: 20 %> width: 20 %>

Loading…
Cancel
Save