Skip to content

Commit

Permalink
Use stimulus for bookmark toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 6, 2023
1 parent a6473d6 commit c372237
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 108 deletions.
15 changes: 8 additions & 7 deletions app/components/blacklight/document/bookmark_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
method: bookmarked? ? :delete : :put,
class: "bookmark-toggle",
data: {
'doc-id' => @document.id,
present: t('blacklight.search.bookmarks.present'),
absent: t('blacklight.search.bookmarks.absent'),
inprogress: t('blacklight.search.bookmarks.inprogress')
controller: 'blacklight--checkbox-submit',
'blacklight--checkbox-submit-present-value': t('blacklight.search.bookmarks.present'),
'blacklight--checkbox-submit-absent-value': t('blacklight.search.bookmarks.absent'),
'blacklight--checkbox-submit-inprogress-value': t('blacklight.search.bookmarks.inprogress'),
'doc-id' => @document.id
}) do %>
<div class="checkbox toggle-bookmark">
<label class="toggle-bookmark" data-checkboxsubmit-target="label">
<input type="checkbox" class="toggle-bookmark" data-checkboxsubmit-target="checkbox" <%= 'checked="checked"' if bookmarked? %>>
<span data-checkboxsubmit-target="span"><%= bookmarked? ? t('blacklight.search.bookmarks.present') : t('blacklight.search.bookmarks.absent') %></span>
<label class="toggle-bookmark" data-blacklight--checkbox-submit-target="label">
<input type="checkbox" class="toggle-bookmark" data-action="change->blacklight--checkbox-submit#change" <%= 'checked="checked"' if bookmarked? %> data-blacklight--checkbox-submit-target="checkbox">
<span data-blacklight--checkbox-submit-target="span"><%= bookmarked? ? t('blacklight.search.bookmarks.present') : t('blacklight.search.bookmarks.absent') %></span>
</label>
</div>

Expand Down
19 changes: 0 additions & 19 deletions app/javascript/blacklight/bookmark_toggle.js

This file was deleted.

80 changes: 0 additions & 80 deletions app/javascript/blacklight/checkbox_submit.js

This file was deleted.

2 changes: 0 additions & 2 deletions app/javascript/blacklight/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import BookmarkToggle from 'blacklight/bookmark_toggle'
import ButtonFocus from 'blacklight/button_focus'
import Modal from 'blacklight/modal'
import SearchContext from 'blacklight/search_context'
import Core from 'blacklight/core'

export default {
BookmarkToggle,
ButtonFocus,
Modal,
SearchContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ['checkbox', 'label', 'span']
static values = { present: String, absent: String, inprogress: String }

async change() {
this.spanTarget.innerHTML = this.inprogressValue;
this.labelTarget.setAttribute('disabled', 'disabled');
this.checkboxTarget.setAttribute('disabled', 'disabled');

const response = await this.submit();

this.labelTarget.removeAttribute('disabled')
this.checkboxTarget.removeAttribute('disabled')

if (response.ok) {
const json = await response.json()
this.updateStateTo(this.checked)
this.updateGlobalBookmarkCounter(json.bookmarks.count)
} else {
alert('Error')
}
}

get checked() {
return this.checkboxTarget.checked;
}

async submit() {
const method = this.checked ? 'put' : 'delete';

//Set the Rails hidden field that fakes an HTTP verb
//properly for current state action.
this.element.querySelector('input[name=_method]').value = method;

const response = await fetch(this.element.getAttribute('action'), {
body: new FormData(this.element),
method: method.toUpperCase(),
headers: {
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': document.querySelector('meta[name=csrf-token]')?.content
}
})

return response;
}

updateGlobalBookmarkCounter(value) {
document.querySelector('[data-role=bookmark-counter]').innerHTML = value;
}

updateStateTo(state) {
if (state) {
this.labelTarget.classList.add('checked')
this.spanTarget.innerHTML = this.presentValue;
} else {
this.labelTarget.classList.remove('checked')
this.spanTarget.innerHTML = this.absentValue;
}
}
}
1 change: 1 addition & 0 deletions config/importmap.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

pin_all_from File.expand_path("../app/javascript/blacklight", __dir__), under: "blacklight"
pin_all_from File.expand_path("../app/javascript/controllers", __dir__), under: "controllers"
15 changes: 15 additions & 0 deletions lib/railties/blacklight.rake
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,18 @@ namespace :blacklight do
end
end
end

if Rake::Task.task_defined?('stimulus:manifest:display')
Rake::Task['stimulus:manifest:display'].enhance do
puts Stimulus::Manifest.generate_from(Blacklight::Engine.root.join("app/javascript/controllers")).join("\n").gsub('./blacklight/', 'blacklight-frontend/app/javascript/controllers/blacklight/')
end
end

if Rake::Task.task_defined?('stimulus:manifest:update')
Rake::Task['stimulus:manifest:update'].enhance do
manifest = Stimulus::Manifest.generate_from(Blacklight::Engine.root.join("app/javascript/controllers")).join("\n").gsub('./blacklight/', 'blacklight-frontend/app/javascript/controllers/blacklight/')
File.open(Rails.root.join("app/javascript/controllers/index.js"), "a+") do |index|
index.puts manifest
end
end
end
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"not IE 11"
],
"dependencies": {
"@hotwired/stimulus": "^3.2.1",
"bootstrap": ">=4.3.1 <6.0.0"
}
}

0 comments on commit c372237

Please sign in to comment.