Skip to content

Commit bd9ba18

Browse files
committed
feat: add modal for creating new donation sites
1 parent adbf9a6 commit bd9ba18

File tree

5 files changed

+82
-5
lines changed

5 files changed

+82
-5
lines changed

app/controllers/donation_sites_controller.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@ def create
1818
@donation_site = current_organization.donation_sites.new(donation_site_params)
1919
respond_to do |format|
2020
if @donation_site.save
21+
format.turbo_stream
2122
format.html do
2223
redirect_to donation_sites_path,
2324
notice: "Donation site #{@donation_site.name} added!"
2425
end
2526
else
27+
flash.now[:error] = "Something didn't work quite right -- try again?"
28+
if request.format.turbo_stream?
29+
format.html { render partial: "donation_sites/new_modal", status: :unprocessable_entity }
30+
end
31+
2632
format.html do
27-
flash.now[:error] = "Something didn't work quite right -- try again?"
2833
render action: :new
2934
end
3035
end
@@ -33,6 +38,11 @@ def create
3338

3439
def new
3540
@donation_site = current_organization.donation_sites.new
41+
if turbo_frame_request?
42+
render partial: "donation_sites/new_modal", layout: false
43+
else
44+
render :new
45+
end
3646
end
3747

3848
def edit
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
connect() {
5+
document.addEventListener("turbo:frame-render", this.openModalHandler)
6+
document.addEventListener("turbo:submit-end", this.closeModalHandler)
7+
}
8+
9+
disconnect() {
10+
document.removeEventListener("turbo:frame-render", this.openModalHandler)
11+
document.removeEventListener("turbo:submit-end", this.closeModalHandler)
12+
}
13+
14+
handleNewSelect(event) {
15+
const value = event.target.value
16+
if (value === "new") {
17+
const url = event.target.dataset.url
18+
Turbo.visit(url, { frame: 'modal-new' })
19+
}
20+
}
21+
22+
openModalHandler = () => {
23+
const modal = document.getElementById("modal-new")
24+
const instance = bootstrap.Modal.getOrCreateInstance(modal)
25+
instance.show()
26+
}
27+
28+
closeModalHandler = (event) => {
29+
if (event.detail.success) {
30+
const modal = document.getElementById("modal-new")
31+
const instance = bootstrap.Modal.getOrCreateInstance(modal)
32+
instance.hide()
33+
}
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<%= turbo_frame_tag "modal-new" do %>
2+
<div class="modal-dialog">
3+
4+
<div class="modal-content">
5+
<div class="modal-header">
6+
<h4 class="modal-title" id="mainModalLabel">New Donation Site</h4>
7+
<button type="button" class="close" data-bs-dismiss="modal">&times;</button>
8+
</div>
9+
<div class="modal-body">
10+
<% flash.each do |key, value| %>
11+
<div class="<%= flash_class(key) %> alert-dismissible">
12+
<a href="#" class="close btn-close" data-bs-dismiss="alert" aria-label="close" style="text-decoration: none;"><%= fa_icon('times') %></a>
13+
<%= value %>
14+
</div>
15+
<% end %>
16+
17+
<%= render partial: "form", object: @donation_site %>
18+
19+
</div>
20+
<div class="modal-footer">
21+
<button type="button" class="btn btn-default" data-bs-dismiss="modal">Close</button>
22+
</div>
23+
</div>
24+
25+
</div>
26+
<% end %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<%= turbo_stream.append "donation_donation_site_id" do %>
2+
<option value="<%= @donation_site.id %>" selected>
3+
<%= @donation_site.name %>
4+
</option>
5+
<% end %>

app/views/donations/_donation_form.html.erb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<%= simple_form_for @donation, data: { controller: "form-input" } do |f| %>
22

3-
<div class="box-body">
3+
<div class="box-body" data-controller="donations-modal">
4+
<%= turbo_frame_tag "modal-new", class: "modal fade", role: 'dialog' %>
45

56
<div class="row">
67
<div class="col-xs-8 col-md-6 col-lg-3">
@@ -16,14 +17,14 @@
1617

1718
<div class="row">
1819
<div class="col-xs-8 col-md-6 col-lg-3">
19-
<%= f.input :donation_site_id,
20-
as: :select,
20+
<%= f.association :donation_site,
2121
collection: options_with_new(@donation_sites),
2222
selected: donation_form.donation_site_id,
2323
include_blank: true,
2424
label: "Donation Site",
2525
error: "Where was this donation dropped off?",
26-
wrapper: :input_group %>
26+
wrapper: :input_group,
27+
input_html: { data: { action: "change->donations-modal#handleNewSelect", url: new_donation_site_path } } %>
2728
</div>
2829
</div>
2930
<div class="row">

0 commit comments

Comments
 (0)