Files
myep-team/assets/controllers/form_upload_guard_controller.js

41 lines
1.1 KiB
JavaScript

import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = ['submit']
connect () {
this.uploading = false
this.boundOnUploading = this.onUploading.bind(this)
this.boundOnComplete = this.onComplete.bind(this)
window.addEventListener('dropzone:uploading', this.boundOnUploading)
window.addEventListener('dropzone:complete', this.boundOnComplete)
}
onUploading () {
this.uploading = true
this.submitTargets.forEach(button => {
button.disabled = true
})
}
onComplete () {
this.uploading = false
this.submitTargets.forEach(button => {
button.disabled = false
})
}
guard (event) {
if (this.uploading) {
event.preventDefault()
event.stopImmediatePropagation()
}
}
disconnect () {
window.removeEventListener('dropzone:uploading', this.boundOnUploading)
window.removeEventListener('dropzone:complete', this.boundOnComplete)
}
}