Merge pull request #1149 from basecamp/save-filter-toggle
Fixes for the saving filter toggle
This commit is contained in:
@@ -1,27 +1,30 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { debounce } from "helpers/timing_helpers";
|
||||
import { post } from "@rails/request.js"
|
||||
|
||||
export default class extends Controller {
|
||||
static classes = ["filtersSet"]
|
||||
static targets = ["field"]
|
||||
static targets = ["field", "form"]
|
||||
static values = { refreshUrl: String }
|
||||
|
||||
initialize() {
|
||||
this.debouncedChange = debounce(this.change.bind(this), 50)
|
||||
this.debouncedToggle = debounce(this.#toggle.bind(this), 50)
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.change()
|
||||
this.#toggle()
|
||||
}
|
||||
|
||||
change() {
|
||||
this.#toggleFiltersSetClass()
|
||||
this.#toggle()
|
||||
this.#refreshSaveToggleButton()
|
||||
}
|
||||
|
||||
async fieldTargetConnected(field) {
|
||||
this.debouncedChange()
|
||||
this.debouncedToggle()
|
||||
}
|
||||
|
||||
#toggleFiltersSetClass(shouldAdd) {
|
||||
#toggle() {
|
||||
this.element.classList.toggle(this.filtersSetClass, this.#hasFiltersSet)
|
||||
}
|
||||
|
||||
@@ -42,4 +45,24 @@ export default class extends Controller {
|
||||
const comboboxContainer = field.closest("[data-combobox-default-value-value]")
|
||||
return comboboxContainer?.dataset?.comboboxDefaultValueValue
|
||||
}
|
||||
|
||||
#refreshSaveToggleButton() {
|
||||
post(this.refreshUrlValue, {
|
||||
body: this.#collectFilterFormData(),
|
||||
responseKind: "turbo-stream"
|
||||
})
|
||||
}
|
||||
|
||||
#collectFilterFormData() {
|
||||
const formData = new FormData()
|
||||
|
||||
this.formTargets.forEach(form => {
|
||||
const hiddenFields = form.querySelectorAll('input[type="hidden"]:not([disabled])[name]')
|
||||
hiddenFields.forEach(field => {
|
||||
formData.append(field.name, field.value)
|
||||
})
|
||||
})
|
||||
|
||||
return formData
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user