forked from fixin.me/fixin.me
Stimulus controllers were reaching into Turbo.StreamElement.prototype to call disableElement/enableElement — tight coupling to Turbo internals. Extract both functions to app/javascript/element_helpers.js and import from there in application.js (which still assigns them to the Turbo prototype for server-driven Turbo Stream actions), details_controller, and readout_unit_controller. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
38 lines
1.3 KiB
JavaScript
38 lines
1.3 KiB
JavaScript
import { Controller } from "@hotwired/stimulus"
|
|
import { disableElement, enableElement } from "element_helpers"
|
|
|
|
export default class extends Controller {
|
|
static targets = ["select", "button"]
|
|
|
|
unitChanged() {
|
|
if (this.selectTarget.value && this.selectTarget.value !== this.selectTarget.dataset.defaultUnitId) {
|
|
enableElement(this.buttonTarget)
|
|
} else {
|
|
disableElement(this.buttonTarget)
|
|
}
|
|
}
|
|
|
|
setDefault() {
|
|
const select = this.selectTarget
|
|
const form = document.createElement('form')
|
|
form.action = this.buttonTarget.dataset.path
|
|
form.method = 'post'
|
|
form.dataset.turboStream = 'true'
|
|
const methodInput = document.createElement('input')
|
|
methodInput.type = 'hidden'; methodInput.name = '_method'; methodInput.value = 'patch'
|
|
const unitInput = document.createElement('input')
|
|
unitInput.type = 'hidden'; unitInput.name = 'quantity[default_unit_id]'; unitInput.value = select.value
|
|
form.appendChild(methodInput)
|
|
form.appendChild(unitInput)
|
|
form.addEventListener('turbo:submit-end', event => {
|
|
if (event.detail.success) {
|
|
select.dataset.defaultUnitId = select.value
|
|
this.unitChanged()
|
|
}
|
|
form.remove()
|
|
})
|
|
document.body.appendChild(form)
|
|
form.requestSubmit()
|
|
}
|
|
}
|