Skip to content

Commit

Permalink
Add Stimulus controller for batch-action triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed Jul 4, 2024
1 parent 84ad2f6 commit 014dc16
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/assets/bundle/trestle/admin.js

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions frontend/js/controllers/batch_action_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import ApplicationController from './application_controller'

export default class extends ApplicationController {
static outlets = ['checkbox-select']

connect () {
if (!this.hasCheckboxSelectOutlet) {
// Set default outlet if none specified
this.element.setAttribute(`data-${this.identifier}-checkbox-select-outlet`, '.trestle-table')
}

this.baseHref = this.element.href

this.update()
}

update () {
this.toggleEnabled()
this.updateHref()
}

toggleEnabled () {
this.element.classList.toggle('disabled', this.checkboxes.length === 0)
}

updateHref () {
this.element.href = this.hrefWithSelectedIds
}

get checkboxes () {
if (this.hasCheckboxSelectOutlet) {
return this.checkboxSelectOutlet.checked
} else {
return []
}
}

get selectedIds () {
return this.checkboxes.map(c => c.value)
}

get hrefWithSelectedIds () {
const ids = this.selectedIds

if (ids.length) {
return `${this.baseHref}?ids=${ids.join(',')}`
} else {
return this.baseHref
}
}

checkboxSelectOutletConnected (outlet, element) {
element.addEventListener('checkbox-select:change', this.update.bind(this))
}

checkboxSelectOutletDisconnected (outlet, element) {
element.removeEventListener('checkbox-select:change', this.update.bind(this))
}
}
3 changes: 3 additions & 0 deletions frontend/js/controllers/checkbox_select_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default class extends ApplicationController {

toggle () {
this.updateSelectAllState()
this.dispatch('change')
}

toggleAll () {
Expand All @@ -13,6 +14,8 @@ export default class extends ApplicationController {
this.checkboxTargets.forEach((checkbox) => {
checkbox.checked = isChecked
})

this.dispatch('change')
}

updateSelectAllState () {
Expand Down
21 changes: 21 additions & 0 deletions sandbox/app/admin/articles_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,25 @@
tag_select :tags
end
end

controller do
def batch_get
ids = params[:ids].split(",")
flash[:message] = { title: "Success!", message: "Performed batch action via GET with #{ids.size} articles." }
redirect_back fallback_location: admin.path
end

def batch_post
ids = params[:ids].split(",")
flash[:message] = { title: "Success!", message: "Performed batch action via POST with #{ids.size} articles." }
redirect_back fallback_location: admin.path
end
end

routes do
collection do
get :batch_get
post :batch_post
end
end
end
32 changes: 32 additions & 0 deletions sandbox/app/views/admin/articles/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<% content_for(:title, admin.t("titles.index", default: "Listing %{pluralized_model_name}")) %>

<% toolbar(:primary) do |t| %>
<%= t.new %>
<% end %>

<% toolbar(:secondary) do |t| %>
<%= t.link "Batch Action (GET)", action: :batch_get, style: :info, data: { controller: "batch-action" } %>
<%= t.link "Batch Action (POST)", action: :batch_post, style: :warning, data: { controller: "confirm batch-action", turbo_method: :post } %>
<% end %>

<% content_for(:utilities) do %>
<%= render "scopes" %>
<% end if admin.scopes.any? %>

<%= render layout: "layout" do %>
<%= index_turbo_frame do %>
<% if hook?("resource.index.header") %>
<header class="main-content-header">
<%= hook("resource.index.header") %>
</header>
<% end %>

<%= render "trestle/table/table", table: admin.table, collection: collection %>

<footer class="main-content-footer">
<%= hook("resource.index.footer") %>

<%= pagination collection: collection, entry_name: admin.model_name, remote: admin.pagination_options[:ajax] %>
</footer>
<% end %>
<% end %>

0 comments on commit 014dc16

Please sign in to comment.