Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ def setup_date_range_picker
@selected_date_range_label = helpers.date_range_label
end

def handle_csv_export
return unless params[:export_csv]

session[:trigger_csv_download] = true
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not just use flash for this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! Fixed

clean_params = request.query_parameters.except("export_csv")
redirect_url = clean_params.any? ? "#{request.path}?#{clean_params.to_query}" : request.path
redirect_to redirect_url
end

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
Expand Down
1 change: 1 addition & 0 deletions app/controllers/distributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class DistributionsController < ApplicationController
include Validatable

before_action :enable_turbo!, only: %i[new show]
before_action :handle_csv_export, only: [:index]
skip_before_action :authenticate_user!, only: %i(calendar)
skip_before_action :authorize_user, only: %i(calendar)
skip_before_action :require_organization, only: %i(calendar)
Expand Down
26 changes: 26 additions & 0 deletions app/javascript/controllers/toast_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="toast"
// Shows a toastr notification when the element is rendered.
//
// Usage:
// <div data-controller="toast" data-toast-message-value="Hello!" data-toast-type-value="info"></div>
//
export default class extends Controller {
static values = {
message: String,
type: { type: String, default: "info" },
timeout: { type: Number, default: 5000 },
position: { type: String, default: "toast-top-center" }
}

connect() {
const previousTimeout = toastr.options.timeOut;
const previousPosition = toastr.options.positionClass;
toastr.options.timeOut = this.timeoutValue;
toastr.options.positionClass = this.positionValue;
toastr[this.typeValue](this.messageValue);
toastr.options.timeOut = previousTimeout;
toastr.options.positionClass = previousPosition;
}
}
4 changes: 3 additions & 1 deletion app/views/distributions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<%=
if @distributions.any?
download_button_to(
distributions_path(format: :csv, filters: filter_params.merge(date_range: date_range_params)),
distributions_path(export_csv: true, filters: filter_params.merge(date_range: date_range_params)),
text: "Export Distributions"
)
end
Expand Down Expand Up @@ -152,3 +152,5 @@
</div>
</div>
</section>

<%= render "shared/csv_download" %>
5 changes: 5 additions & 0 deletions app/views/shared/_csv_download.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if session.delete(:trigger_csv_download) %>
<% csv_url = "#{request.path}.csv#{"?#{request.query_string}" if request.query_string.present?}" %>
<iframe src="<%= csv_url %>" class="d-none"></iframe>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment here as to why we're doing this, because it's kind of weird 😛

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure, added

<div data-controller="toast" data-toast-message-value="Your CSV export is downloading!" class="d-none"></div>
<% end %>