Files
fizzy/app/javascript/controllers/auto_save_controller.js
T
Jorge Manrubia 257c0902f4 Patch for cards missing description
The problem was that after redirecting back with morphing, the autosave mechanism
would trigger an autosave call with an empty content.

This is a first patch. The editor shouldn't be triggering these "change" events
in the first place when loading the editor.
2025-05-30 17:35:25 +02:00

53 lines
801 B
JavaScript

import { Controller } from "@hotwired/stimulus"
import { submitForm } from "helpers/form_helpers"
const AUTOSAVE_INTERVAL = 3000
export default class extends Controller {
#timer
// Lifecycle
disconnect() {
this.submit()
}
// Actions
async submit() {
if (this.#dirty) {
await this.#save()
}
}
change(event) {
if (event.target.form === this.element && !this.#dirty) {
this.#scheduleSave()
}
}
reset() {
this.#timer = null
}
// Private
#scheduleSave() {
this.#timer = setTimeout(() => this.#save(), AUTOSAVE_INTERVAL)
}
async #save() {
this.#resetTimer()
await submitForm(this.element)
}
#resetTimer() {
clearTimeout(this.#timer)
this.#timer = null
}
get #dirty() {
return !!this.#timer
}
}