Skip to content

Commit 5c717be

Browse files
committed
Show a pop-up toast for downloading in the background
1 parent fe7c4c7 commit 5c717be

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

app/controllers/distributions_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def destroy
4040
end
4141

4242
def index
43+
if params[:export_csv]
44+
session[:trigger_csv_download] = true
45+
redirect_to distributions_path(request.query_parameters.except("export_csv"))
46+
return
47+
end
48+
4349
setup_date_range_picker
4450

4551
@highlight_id = session.delete(:created_distribution_id)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
// Connects to data-controller="toast"
4+
// Shows a toastr notification when the element is rendered.
5+
//
6+
// Usage:
7+
// <div data-controller="toast" data-toast-message-value="Hello!" data-toast-type-value="info"></div>
8+
//
9+
export default class extends Controller {
10+
static values = {
11+
message: String,
12+
type: { type: String, default: "info" },
13+
timeout: { type: Number, default: 5000 },
14+
position: { type: String, default: "toast-top-center" }
15+
}
16+
17+
connect() {
18+
const previousTimeout = toastr.options.timeOut;
19+
const previousPosition = toastr.options.positionClass;
20+
toastr.options.timeOut = this.timeoutValue;
21+
toastr.options.positionClass = this.positionValue;
22+
toastr[this.typeValue](this.messageValue);
23+
toastr.options.timeOut = previousTimeout;
24+
toastr.options.positionClass = previousPosition;
25+
}
26+
}

app/views/distributions/index.html.erb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<%=
7272
if @distributions.any?
7373
download_button_to(
74-
distributions_path(format: :csv, filters: filter_params.merge(date_range: date_range_params)),
74+
distributions_path(export_csv: true, filters: filter_params.merge(date_range: date_range_params)),
7575
text: "Export Distributions"
7676
)
7777
end
@@ -152,3 +152,8 @@
152152
</div>
153153
</div>
154154
</section>
155+
156+
<% if session.delete(:trigger_csv_download) %>
157+
<iframe src="<%= distributions_path(format: :csv, filters: filter_params.merge(date_range: date_range_params)) %>" class="d-none"></iframe>
158+
<div data-controller="toast" data-toast-message-value="Your CSV export is downloading!" class="d-none"></div>
159+
<% end %>

0 commit comments

Comments
 (0)