Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c372237

Browse files
committedFeb 6, 2023
Use stimulus for bookmark toggling
1 parent a6473d6 commit c372237

File tree

8 files changed

+88
-108
lines changed

8 files changed

+88
-108
lines changed
 

‎app/components/blacklight/document/bookmark_component.html.erb

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
method: bookmarked? ? :delete : :put,
88
class: "bookmark-toggle",
99
data: {
10-
'doc-id' => @document.id,
11-
present: t('blacklight.search.bookmarks.present'),
12-
absent: t('blacklight.search.bookmarks.absent'),
13-
inprogress: t('blacklight.search.bookmarks.inprogress')
10+
controller: 'blacklight--checkbox-submit',
11+
'blacklight--checkbox-submit-present-value': t('blacklight.search.bookmarks.present'),
12+
'blacklight--checkbox-submit-absent-value': t('blacklight.search.bookmarks.absent'),
13+
'blacklight--checkbox-submit-inprogress-value': t('blacklight.search.bookmarks.inprogress'),
14+
'doc-id' => @document.id
1415
}) do %>
1516
<div class="checkbox toggle-bookmark">
16-
<label class="toggle-bookmark" data-checkboxsubmit-target="label">
17-
<input type="checkbox" class="toggle-bookmark" data-checkboxsubmit-target="checkbox" <%= 'checked="checked"' if bookmarked? %>>
18-
<span data-checkboxsubmit-target="span"><%= bookmarked? ? t('blacklight.search.bookmarks.present') : t('blacklight.search.bookmarks.absent') %></span>
17+
<label class="toggle-bookmark" data-blacklight--checkbox-submit-target="label">
18+
<input type="checkbox" class="toggle-bookmark" data-action="change->blacklight--checkbox-submit#change" <%= 'checked="checked"' if bookmarked? %> data-blacklight--checkbox-submit-target="checkbox">
19+
<span data-blacklight--checkbox-submit-target="span"><%= bookmarked? ? t('blacklight.search.bookmarks.present') : t('blacklight.search.bookmarks.absent') %></span>
1920
</label>
2021
</div>
2122

‎app/javascript/blacklight/bookmark_toggle.js

-19
This file was deleted.

‎app/javascript/blacklight/checkbox_submit.js

-80
This file was deleted.

‎app/javascript/blacklight/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import BookmarkToggle from 'blacklight/bookmark_toggle'
21
import ButtonFocus from 'blacklight/button_focus'
32
import Modal from 'blacklight/modal'
43
import SearchContext from 'blacklight/search_context'
54
import Core from 'blacklight/core'
65

76
export default {
8-
BookmarkToggle,
97
ButtonFocus,
108
Modal,
119
SearchContext,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
static targets = ['checkbox', 'label', 'span']
5+
static values = { present: String, absent: String, inprogress: String }
6+
7+
async change() {
8+
this.spanTarget.innerHTML = this.inprogressValue;
9+
this.labelTarget.setAttribute('disabled', 'disabled');
10+
this.checkboxTarget.setAttribute('disabled', 'disabled');
11+
12+
const response = await this.submit();
13+
14+
this.labelTarget.removeAttribute('disabled')
15+
this.checkboxTarget.removeAttribute('disabled')
16+
17+
if (response.ok) {
18+
const json = await response.json()
19+
this.updateStateTo(this.checked)
20+
this.updateGlobalBookmarkCounter(json.bookmarks.count)
21+
} else {
22+
alert('Error')
23+
}
24+
}
25+
26+
get checked() {
27+
return this.checkboxTarget.checked;
28+
}
29+
30+
async submit() {
31+
const method = this.checked ? 'put' : 'delete';
32+
33+
//Set the Rails hidden field that fakes an HTTP verb
34+
//properly for current state action.
35+
this.element.querySelector('input[name=_method]').value = method;
36+
37+
const response = await fetch(this.element.getAttribute('action'), {
38+
body: new FormData(this.element),
39+
method: method.toUpperCase(),
40+
headers: {
41+
'Accept': 'application/json',
42+
'X-Requested-With': 'XMLHttpRequest',
43+
'X-CSRF-Token': document.querySelector('meta[name=csrf-token]')?.content
44+
}
45+
})
46+
47+
return response;
48+
}
49+
50+
updateGlobalBookmarkCounter(value) {
51+
document.querySelector('[data-role=bookmark-counter]').innerHTML = value;
52+
}
53+
54+
updateStateTo(state) {
55+
if (state) {
56+
this.labelTarget.classList.add('checked')
57+
this.spanTarget.innerHTML = this.presentValue;
58+
} else {
59+
this.labelTarget.classList.remove('checked')
60+
this.spanTarget.innerHTML = this.absentValue;
61+
}
62+
}
63+
}

‎config/importmap.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# frozen_string_literal: true
22

33
pin_all_from File.expand_path("../app/javascript/blacklight", __dir__), under: "blacklight"
4+
pin_all_from File.expand_path("../app/javascript/controllers", __dir__), under: "controllers"

‎lib/railties/blacklight.rake

+15
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,18 @@ namespace :blacklight do
119119
end
120120
end
121121
end
122+
123+
if Rake::Task.task_defined?('stimulus:manifest:display')
124+
Rake::Task['stimulus:manifest:display'].enhance do
125+
puts Stimulus::Manifest.generate_from(Blacklight::Engine.root.join("app/javascript/controllers")).join("\n").gsub('./blacklight/', 'blacklight-frontend/app/javascript/controllers/blacklight/')
126+
end
127+
end
128+
129+
if Rake::Task.task_defined?('stimulus:manifest:update')
130+
Rake::Task['stimulus:manifest:update'].enhance do
131+
manifest = Stimulus::Manifest.generate_from(Blacklight::Engine.root.join("app/javascript/controllers")).join("\n").gsub('./blacklight/', 'blacklight-frontend/app/javascript/controllers/blacklight/')
132+
File.open(Rails.root.join("app/javascript/controllers/index.js"), "a+") do |index|
133+
index.puts manifest
134+
end
135+
end
136+
end

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"not IE 11"
3131
],
3232
"dependencies": {
33+
"@hotwired/stimulus": "^3.2.1",
3334
"bootstrap": ">=4.3.1 <6.0.0"
3435
}
3536
}

0 commit comments

Comments
 (0)
Please sign in to comment.