diff --git a/app/controllers/donation_sites_controller.rb b/app/controllers/donation_sites_controller.rb index 0c1ffb476a..78e6a5d00c 100644 --- a/app/controllers/donation_sites_controller.rb +++ b/app/controllers/donation_sites_controller.rb @@ -22,17 +22,23 @@ def create redirect_to donation_sites_path, notice: "Donation site #{@donation_site.name} added!" end + format.js else format.html do flash.now[:error] = "Something didn't work quite right -- try again?" render action: :new end + format.js { render template: "donation_sites/new_modal" } end end end def new @donation_site = current_organization.donation_sites.new + respond_to do |format| + format.html + format.js { render template: "donation_sites/new_modal" } + end end def edit diff --git a/app/javascript/utils/donations.js b/app/javascript/utils/donations.js index 9c70c6efc0..1a25d422cd 100644 --- a/app/javascript/utils/donations.js +++ b/app/javascript/utils/donations.js @@ -5,6 +5,7 @@ $(function() { const product_drive_participant_id = "#donation_product_drive_participant_id"; const product_drive_id = "#donation_product_drive_id" const manufacturer_id = "#donation_manufacturer_id"; + const donation_site_id="#donation_donation_site_id" const donation_site_container_id = "div.donation_donation_site"; const product_drive_container_id = "div.donation_product_drive"; @@ -17,8 +18,8 @@ $(function() { const create_new_product_drive_text = "---Create new Product Drive---"; const create_new_product_drive_participant_text = "---Create new Participant---"; - const create_new_manufacturer_text = "---Create new Manufacturer---"; + const create_new_donation_site_text = "---Create New Donation Site---"; $(product_drive_id).append( @@ -32,6 +33,10 @@ $(function() { `` ); + $(donation_site_id).append( + `` + ) + $(document).on("change", product_drive_id, function(evt) { const selection = $(product_drive_id + " option") .filter(":selected") @@ -61,6 +66,17 @@ $(function() { } }); + $(document).on("change", donation_site_id, function(evt) { + const selection = $(donation_site_id + " option") + .filter(":selected") + .text(); + + if (selection === create_new_donation_site_text) { + document.getElementById("new_donation_site").click() + } + }); + + function handleSourceSelection() { const selection = $(control_id + " option") .filter(":selected") diff --git a/app/views/donation_sites/_form.html.erb b/app/views/donation_sites/_form.html.erb index 9e969ec3ef..04f57c5d3a 100644 --- a/app/views/donation_sites/_form.html.erb +++ b/app/views/donation_sites/_form.html.erb @@ -1,4 +1,4 @@ -<%= simple_form_for form, html: {role: "form"} do |f| %> +<%= simple_form_for form, remote: request.xhr?, html: {role: "form"} do |f| %>
@@ -37,7 +37,7 @@
<% end %>
diff --git a/app/views/donation_sites/_new_modal.html.erb b/app/views/donation_sites/_new_modal.html.erb new file mode 100644 index 0000000000..1f60f456c3 --- /dev/null +++ b/app/views/donation_sites/_new_modal.html.erb @@ -0,0 +1,27 @@ + diff --git a/app/views/donation_sites/create.js.erb b/app/views/donation_sites/create.js.erb new file mode 100644 index 0000000000..37311f998f --- /dev/null +++ b/app/views/donation_sites/create.js.erb @@ -0,0 +1,6 @@ +<% flash.discard %> +$("#modal_new").modal("hide"); +$("#donation_donation_site_id").empty(); +$("#donation_donation_site_id").html('<%= j options_from_collection_for_select(current_organization.donation_sites, :id, :name) %>'); +$("#donation_donation_site_id").append(''); +$("#donation_donation_site_id").val('<%= @donation_site[:id] %>'); diff --git a/app/views/donation_sites/new_modal.js.erb b/app/views/donation_sites/new_modal.js.erb new file mode 100644 index 0000000000..8e86b6d5f2 --- /dev/null +++ b/app/views/donation_sites/new_modal.js.erb @@ -0,0 +1,2 @@ +$("#modal_new").html("<%= j (render "donation_sites/new_modal") %>"); +$("#modal_new").modal("show"); diff --git a/app/views/donations/new.html.erb b/app/views/donations/new.html.erb index 8c0d065b25..1f03d4a7be 100644 --- a/app/views/donations/new.html.erb +++ b/app/views/donations/new.html.erb @@ -37,6 +37,7 @@ <%= link_to "Add product drive", new_product_drive_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_product_drive", "style" => "display: none;"} %> <%= link_to "Add product drive participant", new_product_drive_participant_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_participant", "style" => "display: none;"} %> <%= link_to "Add manufacturer", new_manufacturer_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_manufacturer", "style" => "display: none;"} %> + <%= link_to "Add donation site", new_donation_site_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_donation_site", "style" => "display: none;"} %> diff --git a/app/views/manufacturers/_new_modal.html.erb b/app/views/manufacturers/_new_modal.html.erb index bcd112c893..248b1798c5 100644 --- a/app/views/manufacturers/_new_modal.html.erb +++ b/app/views/manufacturers/_new_modal.html.erb @@ -3,10 +3,10 @@