Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stimulus controllers to manage bookmark toggling behavior #2999

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Use stimulus for bookmark toggling
  • Loading branch information
cbeer committed Feb 6, 2023
commit ee56b583a988d0e882876c0a228d1d46fbc388fc
15 changes: 8 additions & 7 deletions app/components/blacklight/document/bookmark_component.html.erb
Original file line number Diff line number Diff line change
@@ -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>

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,
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;
}
}
}