Skip to content

Commit 88bafa5

Browse files
committed
Use stimulus for bookmark toggling
1 parent 2b9ff2b commit 88bafa5

File tree

5 files changed

+71
-108
lines changed

5 files changed

+71
-108
lines changed

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

Lines changed: 8 additions & 7 deletions
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

Lines changed: 0 additions & 19 deletions
This file was deleted.

app/javascript/blacklight/checkbox_submit.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

app/javascript/blacklight/index.js

Lines changed: 0 additions & 2 deletions
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,
Lines changed: 63 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)