-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/javascript/controllers/blacklight/checkbox_submit_controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |