From abbc8fe803530bd013ca0b26430faee359539904 Mon Sep 17 00:00:00 2001 From: Neenu Chacko <45137335+neenu-chacko@users.noreply.github.com> Date: Fri, 18 Apr 2025 21:12:06 +0530 Subject: [PATCH 1/5] Added functionality to add donation site on the fly --- app/controllers/donation_sites_controller.rb | 7 +++++ app/javascript/utils/donations.js | 18 ++++++++++++- app/views/donation_sites/_form.html.erb | 2 +- app/views/donation_sites/_new_modal.html.erb | 27 ++++++++++++++++++++ app/views/donation_sites/create.js.erb | 6 +++++ app/views/donation_sites/new_modal.js.erb | 2 ++ app/views/donations/new.html.erb | 1 + 7 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 app/views/donation_sites/_new_modal.html.erb create mode 100644 app/views/donation_sites/create.js.erb create mode 100644 app/views/donation_sites/new_modal.js.erb diff --git a/app/controllers/donation_sites_controller.rb b/app/controllers/donation_sites_controller.rb index 0c1ffb476a..5f6aa5becb 100644 --- a/app/controllers/donation_sites_controller.rb +++ b/app/controllers/donation_sites_controller.rb @@ -22,17 +22,24 @@ 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 + if request.xhr? + respond_to do |format| + format.js { render template: "donation_sites/new_modal" } + end + end end def edit diff --git a/app/javascript/utils/donations.js b/app/javascript/utils/donations.js index 9c70c6efc0..6bf97daf56 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 = "---Add 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..d49942f00e 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| %>
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..11341ee469 --- /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..4822f7aff9 --- /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;"} %>
From 654eaf17fd031bcaee9a25321cb83140982753e4 Mon Sep 17 00:00:00 2001 From: Neenu Chacko <45137335+neenu-chacko@users.noreply.github.com> Date: Fri, 18 Apr 2025 21:23:31 +0530 Subject: [PATCH 2/5] Fixed modal header alignment --- app/views/donation_sites/_new_modal.html.erb | 2 +- app/views/manufacturers/_new_modal.html.erb | 2 +- app/views/product_drives/_new_modal.html.erb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/donation_sites/_new_modal.html.erb b/app/views/donation_sites/_new_modal.html.erb index 11341ee469..1f60f456c3 100644 --- a/app/views/donation_sites/_new_modal.html.erb +++ b/app/views/donation_sites/_new_modal.html.erb @@ -3,10 +3,10 @@