Skip to content

Commit abbc8fe

Browse files
committed
Added functionality to add donation site on the fly
1 parent d8fa236 commit abbc8fe

7 files changed

Lines changed: 61 additions & 2 deletions

File tree

app/controllers/donation_sites_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,24 @@ def create
2222
redirect_to donation_sites_path,
2323
notice: "Donation site #{@donation_site.name} added!"
2424
end
25+
format.js
2526
else
2627
format.html do
2728
flash.now[:error] = "Something didn't work quite right -- try again?"
2829
render action: :new
2930
end
31+
format.js { render template: "donation_sites/new_modal" }
3032
end
3133
end
3234
end
3335

3436
def new
3537
@donation_site = current_organization.donation_sites.new
38+
if request.xhr?
39+
respond_to do |format|
40+
format.js { render template: "donation_sites/new_modal" }
41+
end
42+
end
3643
end
3744

3845
def edit

app/javascript/utils/donations.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ $(function() {
55
const product_drive_participant_id = "#donation_product_drive_participant_id";
66
const product_drive_id = "#donation_product_drive_id"
77
const manufacturer_id = "#donation_manufacturer_id";
8+
const donation_site_id="#donation_donation_site_id"
89

910
const donation_site_container_id = "div.donation_donation_site";
1011
const product_drive_container_id = "div.donation_product_drive";
@@ -17,8 +18,8 @@ $(function() {
1718

1819
const create_new_product_drive_text = "---Create new Product Drive---";
1920
const create_new_product_drive_participant_text = "---Create new Participant---";
20-
2121
const create_new_manufacturer_text = "---Create new Manufacturer---";
22+
const create_new_donation_site_text = "---Add New Donation Site---";
2223

2324
$(product_drive_id).append(
2425

@@ -32,6 +33,10 @@ $(function() {
3233
`<option value="">${create_new_manufacturer_text}</option>`
3334
);
3435

36+
$(donation_site_id).append(
37+
`<option value="">${create_new_donation_site_text}</option>`
38+
)
39+
3540
$(document).on("change", product_drive_id, function(evt) {
3641
const selection = $(product_drive_id + " option")
3742
.filter(":selected")
@@ -61,6 +66,17 @@ $(function() {
6166
}
6267
});
6368

69+
$(document).on("change", donation_site_id, function(evt) {
70+
const selection = $(donation_site_id + " option")
71+
.filter(":selected")
72+
.text();
73+
74+
if (selection === create_new_donation_site_text) {
75+
document.getElementById("new_donation_site").click()
76+
}
77+
});
78+
79+
6480
function handleSourceSelection() {
6581
const selection = $(control_id + " option")
6682
.filter(":selected")

app/views/donation_sites/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%= simple_form_for form, html: {role: "form"} do |f| %>
1+
<%= simple_form_for form, remote: request.xhr?, html: {role: "form"} do |f| %>
22
<section class="content">
33
<div class="container-fluid">
44
<div class="row">
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<div class="modal-dialog">
2+
3+
<!-- Modal content-->
4+
<div class="modal-content">
5+
<div class="modal-header">
6+
<button type="button" class="close" data-bs-dismiss="modal">&times;</button>
7+
<h4 class="modal-title" id="mainModalLabel">
8+
New Donation Site
9+
</h4>
10+
</div>
11+
<div class="modal-body">
12+
<% flash.each do |key, value| %>
13+
<div class="<%= flash_class(key) %> alert-dismissible">
14+
<a href="#" class="close btn-close" data-bs-dismiss="alert" aria-label="close" style="text-decoration: none;"><%= fa_icon('times') %></a>
15+
<%= value %>
16+
</div>
17+
<% end %>
18+
19+
<%= render partial: "form", object: @donation_site %>
20+
21+
</div>
22+
<div class="modal-footer">
23+
<button type="button" class="btn btn-default" data-bs-dismiss="modal">Close</button>
24+
</div>
25+
</div>
26+
27+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<% flash.discard %>
2+
$("#modal_new").modal("hide");
3+
$("#donation_donation_site_id").empty();
4+
$("#donation_donation_site_id").html('<%= j options_from_collection_for_select(current_organization.donation_sites, :id, :name) %>');
5+
$("#donation_donation_site_id").append('<option value="">---Add New Donation Site---</option>');
6+
$("#donation_donation_site_id").val('<%= @donation_site[:id] %>');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$("#modal_new").html("<%= j (render "donation_sites/new_modal") %>");
2+
$("#modal_new").modal("show");

app/views/donations/new.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<%= link_to "Add product drive", new_product_drive_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_product_drive", "style" => "display: none;"} %>
3838
<%= link_to "Add product drive participant", new_product_drive_participant_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_participant", "style" => "display: none;"} %>
3939
<%= link_to "Add manufacturer", new_manufacturer_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_manufacturer", "style" => "display: none;"} %>
40+
<%= link_to "Add donation site", new_donation_site_path, {:remote => true, "data-bs-target" => "#modal-window", id: "new_donation_site", "style" => "display: none;"} %>
4041
</div><!-- /.box -->
4142
</div>
4243
</div>

0 commit comments

Comments
 (0)